68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
//go:build js && wasm
|
|
|
|
package main
|
|
|
|
import (
|
|
"syscall/js"
|
|
|
|
html "dappco.re/go/core/html"
|
|
)
|
|
|
|
// Keep the callback alive for the lifetime of the WASM module.
|
|
var renderToStringFunc js.Func
|
|
|
|
// renderToString builds an HLCRF layout from JS arguments and returns HTML.
|
|
// Slot content is injected via Raw() — the caller is responsible for sanitisation.
|
|
// This is intentional: the WASM module is a rendering engine for trusted content
|
|
// produced server-side or by the application's own templates.
|
|
func renderToString(_ js.Value, args []js.Value) any {
|
|
if len(args) < 1 || args[0].Type() != js.TypeString {
|
|
return ""
|
|
}
|
|
|
|
variant := args[0].String()
|
|
if variant == "" {
|
|
return ""
|
|
}
|
|
|
|
ctx := html.NewContext()
|
|
|
|
if len(args) >= 2 && args[1].Type() == js.TypeString {
|
|
ctx.SetLocale(args[1].String())
|
|
}
|
|
|
|
layout := html.NewLayout(variant)
|
|
|
|
if len(args) >= 3 && args[2].Type() == js.TypeObject {
|
|
slots := args[2]
|
|
for _, slot := range []string{"H", "L", "C", "R", "F"} {
|
|
content := slots.Get(slot)
|
|
if content.Type() == js.TypeString && content.String() != "" {
|
|
switch slot {
|
|
case "H":
|
|
layout.H(html.Raw(content.String()))
|
|
case "L":
|
|
layout.L(html.Raw(content.String()))
|
|
case "C":
|
|
layout.C(html.Raw(content.String()))
|
|
case "R":
|
|
layout.R(html.Raw(content.String()))
|
|
case "F":
|
|
layout.F(html.Raw(content.String()))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return layout.Render(ctx)
|
|
}
|
|
|
|
func main() {
|
|
renderToStringFunc = js.FuncOf(renderToString)
|
|
|
|
api := js.Global().Get("Object").New()
|
|
api.Set("renderToString", renderToStringFunc)
|
|
js.Global().Set("gohtml", api)
|
|
|
|
select {}
|
|
}
|