Crafter.CppDOM/README.md

57 lines
2.4 KiB
Markdown
Raw Normal View History

2025-11-10 22:08:08 +01:00
# About
2025-01-02 02:48:44 +01:00
2025-11-10 22:08:08 +01:00
Crafter.CppDOM is a C++ library that exposes the browser DOM api's to C++ WebAssembly.
2025-01-02 02:48:44 +01:00
2025-11-15 15:29:09 +01:00
# HtmlElementPtr, HtmlElementView, HtmlElement
2025-11-10 22:46:05 +01:00
2025-11-15 15:29:09 +01:00
The library provides three main classes for working with HTML elements, all representing a diffrent ownership model:
2025-11-10 22:46:05 +01:00
2025-11-15 15:29:09 +01:00
## HtmlElementPtr
2025-11-15 15:29:40 +01:00
`HtmlElementPtr` is the base class that provides read and write access to HTML element properties and methods, but does **not** own **anything**. Its the same as a C++ * and will not peform any cleanups.
2025-11-10 22:46:05 +01:00
2025-11-15 15:29:09 +01:00
## HtmlElementView
`HtmlElementView` is a derived class from `HtmlElementPtr` that adds ownership over the event handlers. Upon destructing it will unregister the eventhandlers.
2025-11-10 22:46:05 +01:00
## HtmlElement
2025-11-15 15:29:09 +01:00
`HtmlElement` is a derived class from `HtmlElementView` that adds ownership over the element istelf. Upon destructing it will unregister the eventhandlers and destroy the element in the DOM.
2025-11-10 22:46:05 +01:00
2025-11-10 22:08:08 +01:00
# How to use
2025-11-15 15:29:09 +01:00
Extensive examples can be found in the examples folder, but this is the simplest example:
2025-02-21 03:52:46 +01:00
2025-01-02 02:48:44 +01:00
```cpp
2025-11-10 22:08:08 +01:00
import Crafter.CppDOM;
2025-11-15 15:29:09 +01:00
using namespace Crafter;
2025-11-10 22:06:27 +01:00
2025-11-10 22:08:08 +01:00
int main(){
2025-11-15 15:29:09 +01:00
HtmlElementPtr body("body", "Hello World!");
2025-11-10 22:08:08 +01:00
}
```
It is highly recommended to use this with [Crafter.Build](https://forgejo.catcrafts.net/Catcrafts/Crafter.Build), but it is not strictly required if the same way of injecting the env is followed. The following instructions will be for Crafter.Build.
## Quickstart
create a ``project.json`` in an empty folder, open it in your preferred text editor.
Create a basic project file, that describes your web project.
```JSON
{
"name": "main",
"configurations": [
{
"name": "executable",
"implementations": ["main"],
"target": "wasm32-wasi",
"dependencies": [
{
"path":"https://forgejo.catcrafts.net/Catcrafts/Crafter.CppDOM.git",
2025-11-15 15:29:09 +01:00
"configuration":"lib"
2025-11-10 22:08:08 +01:00
}
],
}
]
}
2025-01-02 02:48:44 +01:00
```
2025-02-21 03:52:46 +01:00
2025-11-15 15:29:09 +01:00
Save and close the file, create a ``main.cpp`` and copy the C++ code we wrote above into it.
2025-11-10 22:06:27 +01:00
2025-11-10 22:08:08 +01:00
Save and close, then run ``crafter-build build executable && caddy file-server --listen :8080 --root bin/executable``. if you have caddy installed, if not use your favorite static file server instead. Now you can open the browser at ``http://localhost:8080`` and ``Hello World!`` will appear in the browser.
2025-02-12 22:22:06 +01:00
2025-11-15 15:29:09 +01:00
This sample can also be viewed in the [HelloElement example](https://forgejo.catcrafts.net/Catcrafts/Crafter.CppDOM/src/branch/master/examples/HelloElement)