added HtmlElement class
This commit is contained in:
parent
3174f75e21
commit
9ee52266b5
8 changed files with 55 additions and 38 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
|
@ -1,4 +1,2 @@
|
|||
build/
|
||||
bin/
|
||||
samples/HelloWorld/bin
|
||||
samples/HelloWorld/build
|
||||
bin/
|
||||
16
Crafter.CppDOM-Bindings.cppm
Normal file
16
Crafter.CppDOM-Bindings.cppm
Normal 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);
|
||||
}
|
||||
26
Crafter.CppDOM-HtmlElement.cppm
Normal file
26
Crafter.CppDOM-HtmlElement.cppm
Normal 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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -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
|
||||
*/
|
||||
|
||||
module;
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
export module Crafter.CppDOM;
|
||||
export import :Bindings;
|
||||
export import :HtmlElement;
|
||||
|
||||
extern "C" {
|
||||
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));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -38,13 +38,13 @@ Create a basic project file, that describes your web project.
|
|||
Save and close the file, create a ``main.cpp``
|
||||
```cpp
|
||||
import Crafter.CppDOM;
|
||||
using namespace Crafter::CppDOM;
|
||||
|
||||
int main(){
|
||||
void* body = Crafter::CppDOM::Bindings::GetElementById("body");
|
||||
Crafter::CppDOM::Bindings::SetInnerHTML(body, "Hello World!");
|
||||
Crafter::CppDOM::Bindings::FreeJs(body);
|
||||
HtmlElement body("body");
|
||||
body.SetInnerHTML("Hello World!");
|
||||
}
|
||||
```
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
"name": "base",
|
||||
"standard": "c++26",
|
||||
"source_files": [],
|
||||
"module_files": ["Crafter.CppDOM"],
|
||||
"module_files": ["Crafter.CppDOM-HtmlElement", "Crafter.CppDOM", "Crafter.CppDOM-Bindings"],
|
||||
"additional_files": ["Crafter.CppDOM.js"],
|
||||
"build_dir": "./build",
|
||||
"output_dir": "./bin",
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
import Crafter.CppDOM;
|
||||
using namespace Crafter::CppDOM::Bindings;
|
||||
|
||||
int main(){
|
||||
void* body = Crafter::CppDOM::Bindings::GetElementById("body");
|
||||
Crafter::CppDOM::Bindings::SetInnerHTML(body, "Hello World!");
|
||||
Crafter::CppDOM::Bindings::FreeJs(body);
|
||||
void* body = GetElementById("body");
|
||||
SetInnerHTML(body, "Hello World!");
|
||||
FreeJs(body);
|
||||
}
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
"type": "executable",
|
||||
"dependencies": [
|
||||
{
|
||||
"path":"/home/jorijn/repos/Crafter.CppDOM/project.json",
|
||||
"path":"../../project.json",
|
||||
"configuration":"debug"
|
||||
}
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue