From c4f764e4ce90a2822aeed2fa0011285a443af434 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Wed, 12 Nov 2025 17:50:58 +0100 Subject: [PATCH] website example --- README.md | 2 - additional/env.js | 27 ++- examples/Website/frontend/main.cpp | 131 +++++------ .../Crafter.CppDOM-HtmlElement.cpp | 208 +++++++++--------- interfaces/Crafter.CppDOM-BindingsImport.cppm | 1 + interfaces/Crafter.CppDOM-HtmlElement.cppm | 11 +- 6 files changed, 198 insertions(+), 182 deletions(-) diff --git a/README.md b/README.md index 0b867b1..c7bcc99 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,5 @@ # About -![alt text](https://github.com/Catcrafts/Crafter.Web/blob/master/hello.png?raw=true) - Crafter.CppDOM is a C++ library that exposes the browser DOM api's to C++ WebAssembly. # HtmlElement vs HtmlElementView diff --git a/additional/env.js b/additional/env.js index 776e159..8e84382 100644 --- a/additional/env.js +++ b/additional/env.js @@ -42,10 +42,29 @@ function writeStringToWasm(str) { return ptr; } -function getElementById(id, idLenght) { - const obj = document.getElementById(decoder.decode(new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, id, idLenght))); - jsmemory.set(++memorycounter, obj); - return memorycounter; +function getElementById(id, idLength) { + try { + // Decode the id from WASM memory + const elementId = decoder.decode( + new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, id, idLength) + ); + + // Attempt to get the element + const obj = document.getElementById(elementId); + + // Check if element exists + if (!obj) { + throw new Error(`Element with id "${elementId}" does not exist.`); + } + + // Store in jsmemory + jsmemory.set(++memorycounter, obj); + + return memorycounter; + } catch (err) { + console.error(err); + return 0; + } } function setInnerHTML(ptr, html, htmlLenght) { diff --git a/examples/Website/frontend/main.cpp b/examples/Website/frontend/main.cpp index 80e70ca..6eaaca7 100644 --- a/examples/Website/frontend/main.cpp +++ b/examples/Website/frontend/main.cpp @@ -12,7 +12,8 @@ struct Note { void RenderNotes(); // Global vector to store notes -std::vector notes; +std::vector* notes = new std::vector(); +std::vector* noteButtons = new std::vector(); // Create the head section HtmlElementView* head = new HtmlElementView("head", R"( @@ -20,16 +21,22 @@ HtmlElementView* head = new HtmlElementView("head", R"(