diff --git a/README.md b/README.md index 096032f..20a4f00 100644 --- a/README.md +++ b/README.md @@ -4,28 +4,6 @@ Crafter.CppDOM is a C++ library that exposes the browser DOM api's to C++ WebAssembly. -# HtmlElement vs HtmlElementView - -The library provides two main classes for working with HTML elements: - -## HtmlElementView -`HtmlElementView` is a base class that provides read and write access to HTML element properties and methods, but does **not** own the underlying DOM element. It's designed to be used when you want to interact with existing elements in the DOM without managing their lifecycle. - -Key characteristics: -- Provides access to element properties and methods like `SetInnerHTML`, `SetStyle`, `AddClass`, etc. -- Supports event handling through various `Add*Listener` methods -- Does not delete the underlying DOM element when destroyed -- Used when you're working with elements that already exist in the DOM - -## HtmlElement -`HtmlElement` is a derived class from `HtmlElementView` that adds ownership semantics. It creates a new DOM element when instantiated and properly manages its lifecycle. - -Key characteristics: -- Inherits all functionality from `HtmlElementView` -- Creates and owns a new DOM element when constructed -- Automatically deletes the DOM element when the `HtmlElement` object is destroyed -- Used when you want to create new elements programmatically - # How to use Please view the examples folder, this is a snippit from the HelloElement example: @@ -34,7 +12,7 @@ import Crafter.CppDOM; using namespace Crafter::CppDOM; int main(){ - HtmlElementView body("body"); + HtmlElement body("body"); body.SetInnerHTML("Hello World!"); } ``` @@ -71,7 +49,7 @@ import Crafter.CppDOM; using namespace Crafter::CppDOM; int main(){ - HtmlElementView body("body"); + HtmlElement body("body"); body.SetInnerHTML("Hello World!"); } ``` diff --git a/additional/env.js b/additional/env.js index c5645c5..a379805 100644 --- a/additional/env.js +++ b/additional/env.js @@ -624,12 +624,6 @@ let env = { const cls = decoder.decode(new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, className, classNameLength)); return jsmemory.get(ptr).classList.contains(cls); }, - deleteElement: function(ptr) { - const element = jsmemory.get(ptr); - if(element && element.parentNode) { - element.parentNode.removeChild(element); - } - }, addClickListener: addClickListener, removeClickListener: removeClickListener, addMouseOverListener: addMouseOverListener, diff --git a/examples/AllEventHandling/main.cpp b/examples/AllEventHandling/main.cpp index 584caf5..b477bc7 100644 --- a/examples/AllEventHandling/main.cpp +++ b/examples/AllEventHandling/main.cpp @@ -8,7 +8,7 @@ using namespace Crafter; import std; // Create the main container element -HtmlElementView* body = new HtmlElementView("body", "
" +HtmlElement* body = new HtmlElement("body", "
" "

Enhanced Event Handling Demo

" "
" "
" @@ -58,22 +58,22 @@ HtmlElementView* body = new HtmlElementView("body", "
" "
"); // Get references to elements -HtmlElementView* mouseButton = new HtmlElementView("mouseButton"); -HtmlElementView* mouseOutput = new HtmlElementView("mouseOutput"); -HtmlElementView* keyInput = new HtmlElementView("keyInput"); -HtmlElementView* keyOutput = new HtmlElementView("keyOutput"); -HtmlElementView* focusInput = new HtmlElementView("focusInput"); -HtmlElementView* focusOutput = new HtmlElementView("focusOutput"); -HtmlElementView* formInput = new HtmlElementView("formInput"); -HtmlElementView* formSelect = new HtmlElementView("formSelect"); -HtmlElementView* formElement = new HtmlElementView("formElement"); -HtmlElementView* formOutput = new HtmlElementView("formOutput"); -HtmlElementView* windowOutput = new HtmlElementView("windowOutput"); -HtmlElementView* dragSource = new HtmlElementView("dragSource"); -HtmlElementView* dropTarget = new HtmlElementView("dropTarget"); -HtmlElementView* dragOutput = new HtmlElementView("dragOutput"); -HtmlElementView* wheelContainer = new HtmlElementView("wheelContainer"); -HtmlElementView* wheelOutput = new HtmlElementView("wheelOutput"); +HtmlElement* mouseButton = new HtmlElement("mouseButton"); +HtmlElement* mouseOutput = new HtmlElement("mouseOutput"); +HtmlElement* keyInput = new HtmlElement("keyInput"); +HtmlElement* keyOutput = new HtmlElement("keyOutput"); +HtmlElement* focusInput = new HtmlElement("focusInput"); +HtmlElement* focusOutput = new HtmlElement("focusOutput"); +HtmlElement* formInput = new HtmlElement("formInput"); +HtmlElement* formSelect = new HtmlElement("formSelect"); +HtmlElement* formElement = new HtmlElement("formElement"); +HtmlElement* formOutput = new HtmlElement("formOutput"); +HtmlElement* windowOutput = new HtmlElement("windowOutput"); +HtmlElement* dragSource = new HtmlElement("dragSource"); +HtmlElement* dropTarget = new HtmlElement("dropTarget"); +HtmlElement* dragOutput = new HtmlElement("dragOutput"); +HtmlElement* wheelContainer = new HtmlElement("wheelContainer"); +HtmlElement* wheelOutput = new HtmlElement("wheelOutput"); int main() { // Mouse Events diff --git a/examples/HelloElement/README.md b/examples/HelloElement/README.md index 1a4ec5a..52b9dde 100644 --- a/examples/HelloElement/README.md +++ b/examples/HelloElement/README.md @@ -15,7 +15,7 @@ import Crafter.CppDOM; using namespace Crafter::CppDOM; int main(){ - HtmlElementView body("body"); + HtmlElement body("body"); body.SetInnerHTML("Hello World!"); } ``` diff --git a/examples/HelloElement/main.cpp b/examples/HelloElement/main.cpp index 366f298..fce4639 100644 --- a/examples/HelloElement/main.cpp +++ b/examples/HelloElement/main.cpp @@ -2,7 +2,7 @@ import Crafter.CppDOM; using namespace Crafter; int main(){ - HtmlElementView body("body"); + HtmlElement body("body"); body.SetInnerHTML("Hello World!"); - //No need to call FreeJs, this is done in the destructor of HtmlElementView. + //No need to call FreeJs, this is done in the destructor of HtmlElement. } \ No newline at end of file diff --git a/examples/InteractiveElement/main.cpp b/examples/InteractiveElement/main.cpp index 9a7691f..371ca9b 100644 --- a/examples/InteractiveElement/main.cpp +++ b/examples/InteractiveElement/main.cpp @@ -2,15 +2,15 @@ import Crafter.CppDOM; import std; using namespace Crafter; -HtmlElementView body("body","

Interactive Element Demo

" +HtmlElement body("body","

Interactive Element Demo

" "" "

Click the button above

"); -HtmlElementView* button = new HtmlElement("myButton"); //prevent destruction +HtmlElement* button = new HtmlElement("myButton"); //prevent destruction int main() { button->AddClickListener([](Crafter::MouseEvent event){ - auto output = HtmlElementView("output"); + auto output = HtmlElement("output"); output.SetInnerHTML("Button was clicked at (" + std::to_string(event.clientX) + ", " + std::to_string(event.clientY) + ")!"); }); } \ No newline at end of file diff --git a/examples/StyleExample/main.cpp b/examples/StyleExample/main.cpp index 5fa2db2..251fe24 100644 --- a/examples/StyleExample/main.cpp +++ b/examples/StyleExample/main.cpp @@ -2,9 +2,9 @@ import Crafter.CppDOM; using namespace Crafter; int main(){ - HtmlElementView body("body","
"); + HtmlElement body("body","
"); // Create a div element - HtmlElementView div("myDiv"); + HtmlElement div("myDiv"); // Set some initial content div.SetInnerHTML("

This is a styled paragraph

"); diff --git a/implementations/Crafter.CppDOM-HtmlElement.cpp b/implementations/Crafter.CppDOM-HtmlElement.cpp index 921f277..01ed75a 100644 --- a/implementations/Crafter.CppDOM-HtmlElement.cpp +++ b/implementations/Crafter.CppDOM-HtmlElement.cpp @@ -26,43 +26,43 @@ import :BindingsImport; import :EventTypes; namespace Crafter { - HtmlElementView::HtmlElementView(const std::string_view id): ptr(CppDOMBindings::GetElementById(id)) { + HtmlElement::HtmlElement(const std::string_view id): ptr(CppDOMBindings::GetElementById(id)) { } - HtmlElementView::HtmlElementView(const std::string_view id, const std::string_view html): ptr(CppDOMBindings::GetElementById(id)) { + HtmlElement::HtmlElement(const std::string_view id, const std::string_view html): ptr(CppDOMBindings::GetElementById(id)) { CppDOMBindings::SetInnerHTML(ptr, html); } - void HtmlElementView::SetInnerHTML(const std::string_view html) { + void HtmlElement::SetInnerHTML(const std::string_view html) { CppDOMBindings::SetInnerHTML(ptr, html); } - void HtmlElementView::SetStyle(const std::string_view style) { + void HtmlElement::SetStyle(const std::string_view style) { CppDOMBindings::SetStyle(ptr, style); } - void HtmlElementView::SetProperty(const std::string_view property, const std::string_view value) { + void HtmlElement::SetProperty(const std::string_view property, const std::string_view value) { CppDOMBindings::SetProperty(ptr, property, value); } - void HtmlElementView::AddClass(const std::string_view className) { + void HtmlElement::AddClass(const std::string_view className) { CppDOMBindings::AddClass(ptr, className); } - void HtmlElementView::RemoveClass(const std::string_view className) { + void HtmlElement::RemoveClass(const std::string_view className) { CppDOMBindings::RemoveClass(ptr, className); } - void HtmlElementView::ToggleClass(const std::string_view className) { + void HtmlElement::ToggleClass(const std::string_view className) { CppDOMBindings::ToggleClass(ptr, className); } - bool HtmlElementView::HasClass(const std::string_view className) { + bool HtmlElement::HasClass(const std::string_view className) { return CppDOMBindings::HasClass(ptr, className); } - std::int32_t HtmlElementView::AddClickListener(std::function callback) { + std::int32_t HtmlElement::AddClickListener(std::function callback) { std::int32_t id = CppDOMBindings::clickHandlerMaxId++; clickHandlers.push_back(id); CppDOMBindings::clickHandlers->insert({id, callback}); @@ -70,13 +70,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveClickListener(std::int32_t id) { + void HtmlElement::RemoveClickListener(std::int32_t id) { clickHandlers.erase(std::remove(clickHandlers.begin(), clickHandlers.end(), id), clickHandlers.end()); CppDOMBindings::clickHandlers->erase(id); CppDOMBindings::RemoveClickListener(ptr, id); } - std::int32_t HtmlElementView::AddMouseOverListener(std::function callback) { + std::int32_t HtmlElement::AddMouseOverListener(std::function callback) { std::int32_t id = CppDOMBindings::mouseOverHandlerMaxId++; mouseOverHandlers.push_back(id); CppDOMBindings::mouseOverHandlers->insert({id, callback}); @@ -84,13 +84,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveMouseOverListener(std::int32_t id) { + void HtmlElement::RemoveMouseOverListener(std::int32_t id) { mouseOverHandlers.erase(std::remove(mouseOverHandlers.begin(), mouseOverHandlers.end(), id), mouseOverHandlers.end()); CppDOMBindings::mouseOverHandlers->erase(id); CppDOMBindings::RemoveMouseOverListener(ptr, id); } - std::int32_t HtmlElementView::AddMouseOutListener(std::function callback) { + std::int32_t HtmlElement::AddMouseOutListener(std::function callback) { std::int32_t id = CppDOMBindings::mouseOutHandlerMaxId++; mouseOutHandlers.push_back(id); CppDOMBindings::mouseOutHandlers->insert({id, callback}); @@ -98,13 +98,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveMouseOutListener(std::int32_t id) { + void HtmlElement::RemoveMouseOutListener(std::int32_t id) { mouseOutHandlers.erase(std::remove(mouseOutHandlers.begin(), mouseOutHandlers.end(), id), mouseOutHandlers.end()); CppDOMBindings::mouseOutHandlers->erase(id); CppDOMBindings::RemoveMouseOutListener(ptr, id); } - std::int32_t HtmlElementView::AddMouseMoveListener(std::function callback) { + std::int32_t HtmlElement::AddMouseMoveListener(std::function callback) { std::int32_t id = CppDOMBindings::mouseMoveHandlerMaxId++; mouseMoveHandlers.push_back(id); CppDOMBindings::mouseMoveHandlers->insert({id, callback}); @@ -112,13 +112,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveMouseMoveListener(std::int32_t id) { + void HtmlElement::RemoveMouseMoveListener(std::int32_t id) { mouseMoveHandlers.erase(std::remove(mouseMoveHandlers.begin(), mouseMoveHandlers.end(), id), mouseMoveHandlers.end()); CppDOMBindings::mouseMoveHandlers->erase(id); CppDOMBindings::RemoveMouseMoveListener(ptr, id); } - std::int32_t HtmlElementView::AddMouseDownListener(std::function callback) { + std::int32_t HtmlElement::AddMouseDownListener(std::function callback) { std::int32_t id = CppDOMBindings::mouseDownHandlerMaxId++; mouseDownHandlers.push_back(id); CppDOMBindings::mouseDownHandlers->insert({id, callback}); @@ -126,13 +126,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveMouseDownListener(std::int32_t id) { + void HtmlElement::RemoveMouseDownListener(std::int32_t id) { mouseDownHandlers.erase(std::remove(mouseDownHandlers.begin(), mouseDownHandlers.end(), id), mouseDownHandlers.end()); CppDOMBindings::mouseDownHandlers->erase(id); CppDOMBindings::RemoveMouseDownListener(ptr, id); } - std::int32_t HtmlElementView::AddMouseUpListener(std::function callback) { + std::int32_t HtmlElement::AddMouseUpListener(std::function callback) { std::int32_t id = CppDOMBindings::mouseUpHandlerMaxId++; mouseUpHandlers.push_back(id); CppDOMBindings::mouseUpHandlers->insert({id, callback}); @@ -140,13 +140,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveMouseUpListener(std::int32_t id) { + void HtmlElement::RemoveMouseUpListener(std::int32_t id) { mouseUpHandlers.erase(std::remove(mouseUpHandlers.begin(), mouseUpHandlers.end(), id), mouseUpHandlers.end()); CppDOMBindings::mouseUpHandlers->erase(id); CppDOMBindings::RemoveMouseUpListener(ptr, id); } - std::int32_t HtmlElementView::AddFocusListener(std::function callback) { + std::int32_t HtmlElement::AddFocusListener(std::function callback) { std::int32_t id = CppDOMBindings::focusHandlerMaxId++; focusHandlers.push_back(id); CppDOMBindings::focusHandlers->insert({id, callback}); @@ -154,13 +154,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveFocusListener(std::int32_t id) { + void HtmlElement::RemoveFocusListener(std::int32_t id) { focusHandlers.erase(std::remove(focusHandlers.begin(), focusHandlers.end(), id), focusHandlers.end()); CppDOMBindings::focusHandlers->erase(id); CppDOMBindings::RemoveFocusListener(ptr, id); } - std::int32_t HtmlElementView::AddBlurListener(std::function callback) { + std::int32_t HtmlElement::AddBlurListener(std::function callback) { std::int32_t id = CppDOMBindings::blurHandlerMaxId++; blurHandlers.push_back(id); CppDOMBindings::blurHandlers->insert({id, callback}); @@ -168,13 +168,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveBlurListener(std::int32_t id) { + void HtmlElement::RemoveBlurListener(std::int32_t id) { blurHandlers.erase(std::remove(blurHandlers.begin(), blurHandlers.end(), id), blurHandlers.end()); CppDOMBindings::blurHandlers->erase(id); CppDOMBindings::RemoveBlurListener(ptr, id); } - std::int32_t HtmlElementView::AddKeyDownListener(std::function callback) { + std::int32_t HtmlElement::AddKeyDownListener(std::function callback) { std::int32_t id = CppDOMBindings::keyDownHandlerMaxId++; keyDownHandlers.push_back(id); CppDOMBindings::keyDownHandlers->insert({id, callback}); @@ -182,13 +182,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveKeyDownListener(std::int32_t id) { + void HtmlElement::RemoveKeyDownListener(std::int32_t id) { keyDownHandlers.erase(std::remove(keyDownHandlers.begin(), keyDownHandlers.end(), id), keyDownHandlers.end()); CppDOMBindings::keyDownHandlers->erase(id); CppDOMBindings::RemoveKeyDownListener(ptr, id); } - std::int32_t HtmlElementView::AddKeyUpListener(std::function callback) { + std::int32_t HtmlElement::AddKeyUpListener(std::function callback) { std::int32_t id = CppDOMBindings::keyUpHandlerMaxId++; keyUpHandlers.push_back(id); CppDOMBindings::keyUpHandlers->insert({id, callback}); @@ -196,13 +196,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveKeyUpListener(std::int32_t id) { + void HtmlElement::RemoveKeyUpListener(std::int32_t id) { keyUpHandlers.erase(std::remove(keyUpHandlers.begin(), keyUpHandlers.end(), id), keyUpHandlers.end()); CppDOMBindings::keyUpHandlers->erase(id); CppDOMBindings::RemoveKeyUpListener(ptr, id); } - std::int32_t HtmlElementView::AddKeyPressListener(std::function callback) { + std::int32_t HtmlElement::AddKeyPressListener(std::function callback) { std::int32_t id = CppDOMBindings::keyPressHandlerMaxId++; keyPressHandlers.push_back(id); CppDOMBindings::keyPressHandlers->insert({id, callback}); @@ -210,13 +210,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveKeyPressListener(std::int32_t id) { + void HtmlElement::RemoveKeyPressListener(std::int32_t id) { keyPressHandlers.erase(std::remove(keyPressHandlers.begin(), keyPressHandlers.end(), id), keyPressHandlers.end()); CppDOMBindings::keyPressHandlers->erase(id); CppDOMBindings::RemoveKeyPressListener(ptr, id); } - std::int32_t HtmlElementView::AddChangeListener(std::function callback) { + std::int32_t HtmlElement::AddChangeListener(std::function callback) { std::int32_t id = CppDOMBindings::changeHandlerMaxId++; changeHandlers.push_back(id); CppDOMBindings::changeHandlers->insert({id, callback}); @@ -224,13 +224,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveChangeListener(std::int32_t id) { + void HtmlElement::RemoveChangeListener(std::int32_t id) { changeHandlers.erase(std::remove(changeHandlers.begin(), changeHandlers.end(), id), changeHandlers.end()); CppDOMBindings::changeHandlers->erase(id); CppDOMBindings::RemoveChangeListener(ptr, id); } - std::int32_t HtmlElementView::AddSubmitListener(std::function callback) { + std::int32_t HtmlElement::AddSubmitListener(std::function callback) { std::int32_t id = CppDOMBindings::submitHandlerMaxId++; submitHandlers.push_back(id); CppDOMBindings::submitHandlers->insert({id, callback}); @@ -238,13 +238,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveSubmitListener(std::int32_t id) { + void HtmlElement::RemoveSubmitListener(std::int32_t id) { submitHandlers.erase(std::remove(submitHandlers.begin(), submitHandlers.end(), id), submitHandlers.end()); CppDOMBindings::submitHandlers->erase(id); CppDOMBindings::RemoveSubmitListener(ptr, id); } - std::int32_t HtmlElementView::AddInputListener(std::function callback) { + std::int32_t HtmlElement::AddInputListener(std::function callback) { std::int32_t id = CppDOMBindings::inputHandlerMaxId++; inputHandlers.push_back(id); CppDOMBindings::inputHandlers->insert({id, callback}); @@ -252,13 +252,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveInputListener(std::int32_t id) { + void HtmlElement::RemoveInputListener(std::int32_t id) { inputHandlers.erase(std::remove(inputHandlers.begin(), inputHandlers.end(), id), inputHandlers.end()); CppDOMBindings::inputHandlers->erase(id); CppDOMBindings::RemoveInputListener(ptr, id); } - std::int32_t HtmlElementView::AddResizeListener(std::function callback) { + std::int32_t HtmlElement::AddResizeListener(std::function callback) { std::int32_t id = CppDOMBindings::resizeHandlerMaxId++; resizeHandlers.push_back(id); CppDOMBindings::resizeHandlers->insert({id, callback}); @@ -266,13 +266,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveResizeListener(std::int32_t id) { + void HtmlElement::RemoveResizeListener(std::int32_t id) { resizeHandlers.erase(std::remove(resizeHandlers.begin(), resizeHandlers.end(), id), resizeHandlers.end()); CppDOMBindings::resizeHandlers->erase(id); CppDOMBindings::RemoveResizeListener(ptr, id); } - std::int32_t HtmlElementView::AddScrollListener(std::function callback) { + std::int32_t HtmlElement::AddScrollListener(std::function callback) { std::int32_t id = CppDOMBindings::scrollHandlerMaxId++; scrollHandlers.push_back(id); CppDOMBindings::scrollHandlers->insert({id, callback}); @@ -280,13 +280,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveScrollListener(std::int32_t id) { + void HtmlElement::RemoveScrollListener(std::int32_t id) { scrollHandlers.erase(std::remove(scrollHandlers.begin(), scrollHandlers.end(), id), scrollHandlers.end()); CppDOMBindings::scrollHandlers->erase(id); CppDOMBindings::RemoveScrollListener(ptr, id); } - std::int32_t HtmlElementView::AddContextMenuListener(std::function callback) { + std::int32_t HtmlElement::AddContextMenuListener(std::function callback) { std::int32_t id = CppDOMBindings::contextMenuHandlerMaxId++; contextMenuHandlers.push_back(id); CppDOMBindings::contextMenuHandlers->insert({id, callback}); @@ -294,13 +294,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveContextMenuListener(std::int32_t id) { + void HtmlElement::RemoveContextMenuListener(std::int32_t id) { contextMenuHandlers.erase(std::remove(contextMenuHandlers.begin(), contextMenuHandlers.end(), id), contextMenuHandlers.end()); CppDOMBindings::contextMenuHandlers->erase(id); CppDOMBindings::RemoveContextMenuListener(ptr, id); } - std::int32_t HtmlElementView::AddDragStartListener(std::function callback) { + std::int32_t HtmlElement::AddDragStartListener(std::function callback) { std::int32_t id = CppDOMBindings::dragStartHandlerMaxId++; dragStartHandlers.push_back(id); CppDOMBindings::dragStartHandlers->insert({id, callback}); @@ -308,13 +308,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveDragStartListener(std::int32_t id) { + void HtmlElement::RemoveDragStartListener(std::int32_t id) { dragStartHandlers.erase(std::remove(dragStartHandlers.begin(), dragStartHandlers.end(), id), dragStartHandlers.end()); CppDOMBindings::dragStartHandlers->erase(id); CppDOMBindings::RemoveDragStartListener(ptr, id); } - std::int32_t HtmlElementView::AddDragEndListener(std::function callback) { + std::int32_t HtmlElement::AddDragEndListener(std::function callback) { std::int32_t id = CppDOMBindings::dragEndHandlerMaxId++; dragEndHandlers.push_back(id); CppDOMBindings::dragEndHandlers->insert({id, callback}); @@ -322,13 +322,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveDragEndListener(std::int32_t id) { + void HtmlElement::RemoveDragEndListener(std::int32_t id) { dragEndHandlers.erase(std::remove(dragEndHandlers.begin(), dragEndHandlers.end(), id), dragEndHandlers.end()); CppDOMBindings::dragEndHandlers->erase(id); CppDOMBindings::RemoveDragEndListener(ptr, id); } - std::int32_t HtmlElementView::AddDropListener(std::function callback) { + std::int32_t HtmlElement::AddDropListener(std::function callback) { std::int32_t id = CppDOMBindings::dropHandlerMaxId++; dropHandlers.push_back(id); CppDOMBindings::dropHandlers->insert({id, callback}); @@ -336,13 +336,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveDropListener(std::int32_t id) { + void HtmlElement::RemoveDropListener(std::int32_t id) { dropHandlers.erase(std::remove(dropHandlers.begin(), dropHandlers.end(), id), dropHandlers.end()); CppDOMBindings::dropHandlers->erase(id); CppDOMBindings::RemoveDropListener(ptr, id); } - std::int32_t HtmlElementView::AddDragOverListener(std::function callback) { + std::int32_t HtmlElement::AddDragOverListener(std::function callback) { std::int32_t id = CppDOMBindings::dragOverHandlerMaxId++; dragOverHandlers.push_back(id); CppDOMBindings::dragOverHandlers->insert({id, callback}); @@ -350,13 +350,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveDragOverListener(std::int32_t id) { + void HtmlElement::RemoveDragOverListener(std::int32_t id) { dragOverHandlers.erase(std::remove(dragOverHandlers.begin(), dragOverHandlers.end(), id), dragOverHandlers.end()); CppDOMBindings::dragOverHandlers->erase(id); CppDOMBindings::RemoveDragOverListener(ptr, id); } - std::int32_t HtmlElementView::AddDragEnterListener(std::function callback) { + std::int32_t HtmlElement::AddDragEnterListener(std::function callback) { std::int32_t id = CppDOMBindings::dragEnterHandlerMaxId++; dragEnterHandlers.push_back(id); CppDOMBindings::dragEnterHandlers->insert({id, callback}); @@ -364,13 +364,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveDragEnterListener(std::int32_t id) { + void HtmlElement::RemoveDragEnterListener(std::int32_t id) { dragEnterHandlers.erase(std::remove(dragEnterHandlers.begin(), dragEnterHandlers.end(), id), dragEnterHandlers.end()); CppDOMBindings::dragEnterHandlers->erase(id); CppDOMBindings::RemoveDragEnterListener(ptr, id); } - std::int32_t HtmlElementView::AddDragLeaveListener(std::function callback) { + std::int32_t HtmlElement::AddDragLeaveListener(std::function callback) { std::int32_t id = CppDOMBindings::dragLeaveHandlerMaxId++; dragLeaveHandlers.push_back(id); CppDOMBindings::dragLeaveHandlers->insert({id, callback}); @@ -378,13 +378,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveDragLeaveListener(std::int32_t id) { + void HtmlElement::RemoveDragLeaveListener(std::int32_t id) { dragLeaveHandlers.erase(std::remove(dragLeaveHandlers.begin(), dragLeaveHandlers.end(), id), dragLeaveHandlers.end()); CppDOMBindings::dragLeaveHandlers->erase(id); CppDOMBindings::RemoveDragLeaveListener(ptr, id); } - std::int32_t HtmlElementView::AddWheelListener(std::function callback) { + std::int32_t HtmlElement::AddWheelListener(std::function callback) { std::int32_t id = CppDOMBindings::wheelHandlerMaxId++; wheelHandlers.push_back(id); CppDOMBindings::wheelHandlers->insert({id, callback}); @@ -392,13 +392,13 @@ namespace Crafter { return id; } - void HtmlElementView::RemoveWheelListener(std::int32_t id) { + void HtmlElement::RemoveWheelListener(std::int32_t id) { wheelHandlers.erase(std::remove(wheelHandlers.begin(), wheelHandlers.end(), id), wheelHandlers.end()); CppDOMBindings::wheelHandlers->erase(id); CppDOMBindings::RemoveWheelListener(ptr, id); } - HtmlElementView::~HtmlElementView(){ + HtmlElement::~HtmlElement(){ for(std::int32_t handler : clickHandlers) { CppDOMBindings::clickHandlers->erase(handler); CppDOMBindings::RemoveClickListener(ptr, handler); @@ -497,17 +497,4 @@ namespace Crafter { } CppDOMBindings::FreeJs(ptr); } - - HtmlElement::HtmlElement(const std::string_view id): HtmlElementView(id) { - - } - - HtmlElement::HtmlElement(const std::string_view id, const std::string_view html): HtmlElementView(id, html) { - - } - - HtmlElement::~HtmlElement() { - CppDOMBindings::DeleteElement(ptr); - } -} - +} \ No newline at end of file diff --git a/interfaces/Crafter.CppDOM-BindingsExport.cppm b/interfaces/Crafter.CppDOM-BindingsExport.cppm index 02d45f2..f6647da 100644 --- a/interfaces/Crafter.CppDOM-BindingsExport.cppm +++ b/interfaces/Crafter.CppDOM-BindingsExport.cppm @@ -105,10 +105,6 @@ extern "C" { std::free(ptr); } - __attribute__((export_name("deleteElement"))) void DeleteElement(void* ptr) { - // This will be implemented in JavaScript - } - __attribute__((export_name("ExecuteClickHandler"))) void ExecuteClickHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::clickHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } diff --git a/interfaces/Crafter.CppDOM-BindingsImport.cppm b/interfaces/Crafter.CppDOM-BindingsImport.cppm index 07d5efb..56db1fb 100644 --- a/interfaces/Crafter.CppDOM-BindingsImport.cppm +++ b/interfaces/Crafter.CppDOM-BindingsImport.cppm @@ -136,6 +136,4 @@ export namespace Crafter::CppDOMBindings { bool HasClass(void* ptr, const std::string_view className) { return HasClass(ptr, className.data(), className.size()); } - - __attribute__((import_module("env"), import_name("deleteElement"))) void DeleteElement(void* ptr); } \ No newline at end of file diff --git a/interfaces/Crafter.CppDOM-HtmlElement.cppm b/interfaces/Crafter.CppDOM-HtmlElement.cppm index 71b64d0..fe7bf8f 100644 --- a/interfaces/Crafter.CppDOM-HtmlElement.cppm +++ b/interfaces/Crafter.CppDOM-HtmlElement.cppm @@ -25,7 +25,7 @@ import :BindingsImport; import :EventTypes; namespace Crafter { - export class HtmlElementView { + export class HtmlElement { public: void* const ptr; std::vector clickHandlers; @@ -53,8 +53,8 @@ namespace Crafter { std::vector dragLeaveHandlers; std::vector wheelHandlers; - HtmlElementView(const std::string_view id); - HtmlElementView(const std::string_view id, const std::string_view html); + HtmlElement(const std::string_view id); + HtmlElement(const std::string_view id, const std::string_view html); void SetInnerHTML(const std::string_view html); void SetStyle(const std::string_view style); void SetProperty(const std::string_view property, const std::string_view value); @@ -135,13 +135,6 @@ namespace Crafter { std::int32_t AddWheelListener(std::function callback); void RemoveWheelListener(std::int32_t id); - ~HtmlElementView(); - }; - - export class HtmlElement : public HtmlElementView { - public: - HtmlElement(const std::string_view id); - HtmlElement(const std::string_view id, const std::string_view html); ~HtmlElement(); }; } \ No newline at end of file