added HtmlElement class

This commit is contained in:
Jorijn van der Graaf 2025-02-12 23:06:56 +01:00
commit 9ee52266b5
8 changed files with 55 additions and 38 deletions

4
.gitignore vendored
View file

@ -1,4 +1,2 @@
build/ build/
bin/ bin/
samples/HelloWorld/bin
samples/HelloWorld/build

View file

@ -0,0 +1,16 @@
module;
#include <string>
export module Crafter.CppDOM:Bindings;
export namespace Crafter::CppDOM::Bindings {
__attribute__((import_module("env"), import_name("getElementById"))) void* GetElementById(const char* id, std::size_t idLenght);
inline void* GetElementById(const std::string& id) {
return GetElementById(id.c_str(), id.size());
}
__attribute__((import_module("env"), import_name("setInnerHTML"))) void SetInnerHTML(void* ptr, const char* html, std::size_t htmlLenght);
inline void SetInnerHTML(void* ptr, const std::string& html) {
SetInnerHTML(ptr, html.c_str(), html.size());
}
__attribute__((import_module("env"), import_name("freeJs"))) void FreeJs(void* ptr);
}

View file

@ -0,0 +1,26 @@
module;
#include <string>
export module Crafter.CppDOM:HtmlElement;
import :Bindings;
namespace Crafter::CppDOM {
export class HtmlElement {
public:
void* const ptr;
inline HtmlElement(const char* id, std::size_t idLenght): ptr(Bindings::GetElementById(id, idLenght)) {
}
inline HtmlElement(const std::string& id): ptr(Bindings::GetElementById(id)) {
}
inline void SetInnerHTML(const char* html, std::size_t htmlLenght) {
Bindings::SetInnerHTML(ptr, html, htmlLenght);
}
inline void SetInnerHTML(const std::string& html) {
Bindings::SetInnerHTML(ptr, html);
}
inline ~HtmlElement(){
Bindings::FreeJs(ptr);
}
};
}

View file

@ -18,12 +18,9 @@ License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
module;
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <string>
export module Crafter.CppDOM; export module Crafter.CppDOM;
export import :Bindings;
export import :HtmlElement;
extern "C" { extern "C" {
void __cxa_allocate_exception() { void __cxa_allocate_exception() {
@ -34,24 +31,3 @@ extern "C" {
} }
} }
export namespace Crafter::CppDOM::Bindings {
__attribute__((import_module("env"), import_name("getElementById"))) void* GetElementById(const char* id, std::size_t idLenght);
inline void* GetElementById(const std::string& id){
return GetElementById(id.c_str(), id.size());
}
__attribute__((import_module("env"), import_name("setInnerHTML"))) void SetInnerHTML(void* ptr, const char* html, std::size_t htmlLenght);
inline void SetInnerHTML(void* ptr, const std::string& html){
SetInnerHTML(ptr, html.c_str(), html.size());
}
__attribute__((import_module("env"), import_name("freeJs"))) void FreeJs(void* ptr);
// __attribute__((import_module("env"), import_name("strokeRect"))) void StrokeRect(std::uint64_t id, std::uint32_t x, std::uint32_t y, std::uint32_t width, std::uint32_t height, const char* color, std::size_t colorLenght);
// __attribute__((import_module("env"), import_name("putImageData"))) void PutImageData(std::uint64_t id, Crafter::Web::Pixel* buffer, std::uint32_t width, std::uint32_t height);
// __attribute__((import_module("env"), import_name("fetch"))) void Fetch(void* obj, const char* url, std::size_t urlCallback, char* buffer, void (*listener)(void* obj, std::uint32_t, char*));
// __attribute__((import_module("env"), import_name("setTimeout"))) void SetTimeout(void* obj, std::uint32_t time, void (*listener)(void* obj));
// __attribute__((import_module("env"), import_name("draw"))) void Draw(std::uint64_t id, std::uint32_t x, std::uint32_t y, const char* text, std::uint32_t textLenght);
//__attribute__((import_module("env"), import_name("addEventListener"))) void AddEventListener(std::string id, std::string eventId, void (*listener)(void));
}

View file

@ -38,13 +38,13 @@ Create a basic project file, that describes your web project.
Save and close the file, create a ``main.cpp`` Save and close the file, create a ``main.cpp``
```cpp ```cpp
import Crafter.CppDOM; import Crafter.CppDOM;
using namespace Crafter::CppDOM;
int main(){ int main(){
void* body = Crafter::CppDOM::Bindings::GetElementById("body"); HtmlElement body("body");
Crafter::CppDOM::Bindings::SetInnerHTML(body, "Hello World!"); body.SetInnerHTML("Hello World!");
Crafter::CppDOM::Bindings::FreeJs(body);
} }
``` ```
Save and close, then run ``crafter-webbuild serve -c debug``. Now you can open the browser at ``http://localhost:8080/`` and ``Hello World!`` will appear in the browser. Save and close, then run ``crafter-webbuild serve -c debug``. Now you can open the browser at ``http://localhost:8080/`` and ``Hello World!`` will appear in the browser.
This sample can also be viewed in the [Hello World sample](https://github.com/Catcrafts/Crafter.CppDOM/tree/master/samples/HelloWorld) This sample can also be viewed in the [HelloElement sample](https://github.com/Catcrafts/Crafter.CppDOM/tree/master/samples/HelloElement)

View file

@ -5,7 +5,7 @@
"name": "base", "name": "base",
"standard": "c++26", "standard": "c++26",
"source_files": [], "source_files": [],
"module_files": ["Crafter.CppDOM"], "module_files": ["Crafter.CppDOM-HtmlElement", "Crafter.CppDOM", "Crafter.CppDOM-Bindings"],
"additional_files": ["Crafter.CppDOM.js"], "additional_files": ["Crafter.CppDOM.js"],
"build_dir": "./build", "build_dir": "./build",
"output_dir": "./bin", "output_dir": "./bin",

View file

@ -1,7 +1,8 @@
import Crafter.CppDOM; import Crafter.CppDOM;
using namespace Crafter::CppDOM::Bindings;
int main(){ int main(){
void* body = Crafter::CppDOM::Bindings::GetElementById("body"); void* body = GetElementById("body");
Crafter::CppDOM::Bindings::SetInnerHTML(body, "Hello World!"); SetInnerHTML(body, "Hello World!");
Crafter::CppDOM::Bindings::FreeJs(body); FreeJs(body);
} }

View file

@ -14,7 +14,7 @@
"type": "executable", "type": "executable",
"dependencies": [ "dependencies": [
{ {
"path":"/home/jorijn/repos/Crafter.CppDOM/project.json", "path":"../../project.json",
"configuration":"debug" "configuration":"debug"
} }
] ]