diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/app/components/hello/index.css | 0 | ||||
| -rw-r--r-- | src/app/components/hello/index.html | 0 | ||||
| -rw-r--r-- | src/app/components/hello/root.zig | 17 | ||||
| -rw-r--r-- | src/app/root.zig | 48 | ||||
| -rw-r--r-- | src/html/Element.zig | 103 | ||||
| -rw-r--r-- | src/html/root.zig | 7 | ||||
| -rw-r--r-- | src/z/App/main.js | 2 | ||||
| -rw-r--r-- | src/z/App/root.zig | 22 | ||||
| -rw-r--r-- | src/z/Component.zig | 96 | ||||
| -rw-r--r-- | src/z/html.zig | 105 | ||||
| -rw-r--r-- | src/z/root.zig | 2 |
11 files changed, 246 insertions, 156 deletions
diff --git a/src/app/components/hello/index.css b/src/app/components/hello/index.css new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/app/components/hello/index.css diff --git a/src/app/components/hello/index.html b/src/app/components/hello/index.html new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/app/components/hello/index.html diff --git a/src/app/components/hello/root.zig b/src/app/components/hello/root.zig new file mode 100644 index 0000000..6f68550 --- /dev/null +++ b/src/app/components/hello/root.zig @@ -0,0 +1,17 @@ +const std = @import("std"); +const z = @import("z"); + +pub const component: z.Component = .{ + .name = "hello", + .html = .native( + .div, + .{ .onclick = "@clicked" }, + .content("click me"), + ), + .css = @embedFile("index.css"), + .link = @This(), +}; + +pub fn clicked() void { + z.log.js.debug("hi"); +} diff --git a/src/app/root.zig b/src/app/root.zig index 6de9216..f587cfb 100644 --- a/src/app/root.zig +++ b/src/app/root.zig @@ -13,50 +13,12 @@ pub const app: z.App(.{ z.Object.global(.window), z.Object.global(.Object), }, + .components = &.{ + @import("components/hello/root.zig").component, + }, + .body = .native(.@"app-hello", .{}, .content("")), }) = .init; -fn run_wrapped() !void { - var env: z.Env = .init(std.heap.wasm_allocator); - defer env.deinit(); - - // Set and delete properties - _ = app.globals.window.set(&env, "my_prop", 45); - _ = app.globals.window.delete(&env, "my_prop"); - - // Get properties - const console = try app.globals.window.get(&env, "console").await(&env, .object); - const location = try app.globals.window.get(&env, "location").await(&env, .object); - - // Create new object - const obj = app.globals.Object.new(&env); - - // Get string, needs to be freed. - const hostname = try location.get(&env, "hostname").await(&env, .string); - defer std.heap.wasm_allocator.free(hostname); - - // Call method of an object - try console.call( - &env, - .log, - .{ - "hello, wasm!", - try obj.await(&env), - hostname, - }, - ).await(&env, .undefined); - - // Unlink Object from global store. - _ = env.free(console); -} - -fn run() callconv(.c) void { - run_wrapped() catch |e| { - std.log.err("{}", .{e}); - }; -} - comptime { - if (builtin.target.cpu.arch.isWasm()) { - @export(&run, .{ .name = "run" }); - } + _ = app; } diff --git a/src/html/Element.zig b/src/html/Element.zig deleted file mode 100644 index a9e5897..0000000 --- a/src/html/Element.zig +++ /dev/null @@ -1,103 +0,0 @@ -const Self = @This(); - -pub const Content = union(enum) { - elements: []const Self, - string: []const u8, - void: void, - - pub fn content(string: []const u8) @This() { - return .{ .string = string }; - } - - pub fn children(elements: []const Self) @This() { - return .{ .elements = elements }; - } -}; - -pub const Attribute = struct { - name: []const u8, - value: []const u8, -}; - -tag: []const u8, -attributes: []const Attribute, -content: Content, - -fn attributesFrom(attributes: anytype) []const Attribute { - const T = @TypeOf(attributes); - const info = @typeInfo(T); - - if (info != .@"struct") { - @compileError("struct needed"); - } - - const fields = info.@"struct".fields; - - var attrs: []const Attribute = &.{}; - - for (fields) |field| { - const name = field.name; - const value = @field(attributes, name); - - const next_attr: []const Attribute = &.{.{ .name = name, .value = value }}; - attrs = attrs ++ next_attr; - } - - return attrs; -} - -pub fn native(name: @EnumLiteral(), attrs: anytype, content: Content) Self { - return .{ - .tag = @tagName(name), - .attributes = attributesFrom(attrs), - .content = content, - }; -} - -pub fn transparent(content: Content) Self { - return .{ - .tag = "", - .attributes = &.{}, - .content = content, - }; -} - -pub fn toHtml(comptime self: Self) []const u8 { - var string: []const u8 = string: { - if (self.tag.len == 0) { - break :string ""; - } - - var string: []const u8 = "<" ++ self.tag; - - inline for (self.attributes) |attr| { - string = string ++ " " ++ attr.name ++ "='" ++ attr.value ++ "'"; - } - - string = string ++ ">"; - - break :string string; - }; - - switch (self.content) { - .void => {}, - .string => |s| { - string = string ++ s ++ "</" ++ self.tag ++ ">"; - }, - .elements => |els| { - for (els) |el| { - string = string ++ el.toHtml(); - } - - string = string ++ "</" ++ self.tag ++ ">"; - }, - } - - string = string ++ if (self.tag.len == 0) "" else "</" ++ self.tag ++ ">"; - - return string; -} - -pub fn toDocument(comptime self: Self) []const u8 { - return "<!DOCTYPE html>" ++ self.toHtml(); -} diff --git a/src/html/root.zig b/src/html/root.zig deleted file mode 100644 index 14a9b3c..0000000 --- a/src/html/root.zig +++ /dev/null @@ -1,7 +0,0 @@ -const std = @import("std"); - -pub const Element = @import("Element.zig"); - -test { - _ = std.testing.refAllDecls(@This()); -} diff --git a/src/z/App/main.js b/src/z/App/main.js index a875c1d..34df302 100644 --- a/src/z/App/main.js +++ b/src/z/App/main.js @@ -24,7 +24,5 @@ WebAssembly.instantiateStreaming(fetch("index.wasm"), imports).then( exports: instance.exports, memory: instance.exports.memory, } - - wasm.exports.run(); }, ); diff --git a/src/z/App/root.zig b/src/z/App/root.zig index 326f3c0..a3d0916 100644 --- a/src/z/App/root.zig +++ b/src/z/App/root.zig @@ -1,10 +1,13 @@ const builtin = @import("builtin"); const std = @import("std"); -const h = @import("html"); +const h = @import("../html.zig"); +const Component = @import("../root.zig").Component; pub const Options = struct { name: []const u8, globals: []const Global, + components: []const Component, + body: h.Element, pub const Global = struct { name: []const u8, @@ -37,6 +40,12 @@ pub fn App(comptime options: Options) type { }, pub const init: @This() = .{}; + + comptime { + for (options.components) |component| { + component.exportLinks(); + } + } }; } else { return struct { @@ -63,6 +72,17 @@ pub fn App(comptime options: Options) type { break :reg register_code; })), .native(.script, .{}, .content(@embedFile("main.js"))), + .transparent(.children(comp: { + var cs: []const h.Element = &.{}; + + for (options.components) |component| { + const c: []const h.Element = &.{component.toElement()}; + cs = cs ++ c; + } + + break :comp cs; + })), + options.body, })), })).toDocument(), diff --git a/src/z/Component.zig b/src/z/Component.zig new file mode 100644 index 0000000..2bc293e --- /dev/null +++ b/src/z/Component.zig @@ -0,0 +1,96 @@ +const std = @import("std"); +const Element = @import("html.zig").Element; + +const Self = @This(); + +name: []const u8, +html: Element, +css: []const u8, +link: type, + +fn exportFromHtml(self: *const Self, element: Element) void { + comptime { + for (element.attributes) |attr| { + if (attr.value.len > 0 and attr.value[0] == '@') { + const func = struct { + pub fn func() callconv(.c) void { + @field(self.link, attr.value[1..])(); + } + }.func; + + @export( + &func, + .{ + .name = std.fmt.comptimePrint( + "export::{s}::{s}", + .{ self.name, attr.value[1..] }, + ), + }, + ); + } + } + } +} + +pub fn exportLinks(self: *const Self) void { + comptime self.exportFromHtml(self.html); +} + +fn linkHtmlRecursive(self: *const Self, element: Element) Element { + comptime { + return .{ + .tag = element.tag, + .attributes = attrs: { + var attributes: []const Element.Attribute = &.{}; + + for (element.attributes) |*attr| { + if (attr.value.len > 0 and attr.value[0] == '@') { + const attribute: []const Element.Attribute = &.{.{ + .name = attr.name, + .value = std.fmt.comptimePrint( + "wasm.exports[\"export::{s}::{s}\"]()", + .{ self.name, attr.value[1..] }, + ), + }}; + attributes = attributes ++ attribute; + } + } + break :attrs attributes; + }, + .content = element.content, + }; + } +} + +pub fn toElement(self: *const Self) Element { + return .transparent( + .children( + &.{ + .native( + .template, + .{ .id = "@" ++ self.name }, + .children(&.{self.linkHtmlRecursive(self.html)}), + ), + .native( + .script, + .{}, + .content( + std.fmt.comptimePrint( + \\customElements.define("app-{s}", class extends HTMLElement {{ + \\ constructor() {{ + \\ super(); + \\ this.attachShadow({{ mode: 'open' }}); + \\ const template = document.getElementById("@{s}"); + \\ const node = document.importNode(template.content, true); + \\ this.shadowRoot.appendChild(node); + \\ }} + \\}}) + , + .{self.name, self.name} + ), + ), + ), + }, + ), + ); +} diff --git a/src/z/html.zig b/src/z/html.zig new file mode 100644 index 0000000..9a5b3a2 --- /dev/null +++ b/src/z/html.zig @@ -0,0 +1,105 @@ +pub const Element = struct { + const Self = @This(); + + pub const Content = union(enum) { + elements: []const Self, + string: []const u8, + void: void, + + pub fn content(string: []const u8) @This() { + return .{ .string = string }; + } + + pub fn children(elements: []const Self) @This() { + return .{ .elements = elements }; + } + }; + + pub const Attribute = struct { + name: []const u8, + value: []const u8, + }; + + tag: []const u8, + attributes: []const Attribute, + content: Content, + + fn attributesFrom(attributes: anytype) []const Attribute { + const T = @TypeOf(attributes); + const info = @typeInfo(T); + + if (info != .@"struct") { + @compileError("struct needed"); + } + + const fields = info.@"struct".fields; + + var attrs: []const Attribute = &.{}; + + for (fields) |field| { + const name = field.name; + const value = @field(attributes, name); + + const next_attr: []const Attribute = &.{.{ .name = name, .value = value }}; + attrs = attrs ++ next_attr; + } + + return attrs; + } + + pub fn native(name: @EnumLiteral(), attrs: anytype, content: Content) Self { + return .{ + .tag = @tagName(name), + .attributes = attributesFrom(attrs), + .content = content, + }; + } + + pub fn transparent(content: Content) Self { + return .{ + .tag = "", + .attributes = &.{}, + .content = content, + }; + } + + pub fn toHtml(comptime self: Self) []const u8 { + var string: []const u8 = string: { + if (self.tag.len == 0) { + break :string ""; + } + + var string: []const u8 = "<" ++ self.tag; + + inline for (self.attributes) |attr| { + string = string ++ " " ++ attr.name ++ "='" ++ attr.value ++ "'"; + } + + string = string ++ ">"; + + break :string string; + }; + + switch (self.content) { + .void => {}, + .string => |s| { + string = string ++ s ++ "</" ++ self.tag ++ ">"; + }, + .elements => |els| { + for (els) |el| { + string = string ++ el.toHtml(); + } + + string = string ++ "</" ++ self.tag ++ ">"; + }, + } + + string = string ++ if (self.tag.len == 0) "" else "</" ++ self.tag ++ ">"; + + return string; + } + + pub fn toDocument(comptime self: Self) []const u8 { + return "<!DOCTYPE html>" ++ self.toHtml(); + } +}; diff --git a/src/z/root.zig b/src/z/root.zig index e18193b..b61f3ba 100644 --- a/src/z/root.zig +++ b/src/z/root.zig @@ -4,9 +4,11 @@ const std = @import("std"); pub const log = @import("log/root.zig"); pub const Env = @import("Env/root.zig"); pub const Object = @import("Object.zig"); +pub const html = @import("html.zig"); pub const App = @import("App/root.zig").App; pub const Options = @import("App/root.zig").Options; +pub const Component = @import("Component.zig"); fn defaultPanic(msg: []const u8, _: ?usize) noreturn { log.js.err(msg); |