//go:build js && wasm package ultraviolet import ( "syscall/js" ) func WebWindow() js.Value { return js.Global().Get("window") } func WebDocument() js.Value { return js.Global().Get("document") } func WebContent() js.Value { return WebDocument(). Call( "getElementById", "content", ) } func WebBuildDiv(oldNode js.Value, parent Component, view View, text string, html string, handlers AbstractHandlers) js.Value { return WebBuildDivInternal( oldNode, parent.(JS).Node(), view, text, html, handlers, ) } func WebBuildDivInternal(oldNode js.Value, parent js.Value, view View, text string, html string, handlers AbstractHandlers) js.Value { newNode := WebDocument().Call("createElement", "div") newNode.Call("setAttribute", "style", ViewToString(view)) if text != "" { newNode.Set("innerText", text) } if html != "" { newNode.Set("innerHTML", html) } for key, handler := range handlers { switch key { case HANDLER_CLICK: newNode.Call( "addEventListener", "click", js.FuncOf( func(current js.Value, args []js.Value) interface{} { handler(nil) return nil }, ), ) } } if parent.Call("contains", oldNode).Bool() { parent.Call( "replaceChild", newNode, oldNode, ) } else { parent.Call( "appendChild", newNode, ) } return newNode } func WebBuildImg(oldNode js.Value, parentComponent Component, view View, path string, width int) js.Value { parent := parentComponent.(JS).Node() newNode := WebDocument().Call("createElement", "img") newNode.Call("setAttribute", "src", path) newNode.Call("setAttribute", "style", ViewToString(view)) newNode.Call("setAttribute", "width", width) if parent.Call("contains", oldNode).Bool() { parent.Call( "replaceChild", newNode, oldNode, ) } else { parent.Call( "appendChild", newNode, ) } return newNode } func WebBuildFrame(oldNode js.Value, parentComponent Component, view View, path string) js.Value { parent := parentComponent.(JS).Node() newNode := WebDocument().Call("createElement", "iframe") newNode.Call("setAttribute", "src", path) newNode.Call("setAttribute", "style", ViewToString(view)) if parent.Call("contains", oldNode).Bool() { parent.Call( "replaceChild", newNode, oldNode, ) } else { parent.Call( "appendChild", newNode, ) } return newNode } func WebBuildCode(oldNode js.Value, parentComponent Component, view View, language string, content string) js.Value { parent := parentComponent.(JS).Node() codeNode := WebDocument().Call("createElement", "code") codeNode.Call("setAttribute", "class", "language-"+language) codeNode.Set("textContent", content) newNode := WebDocument().Call("createElement", "pre") newNode.Call("setAttribute", "style", ViewToString(view)) newNode.Call( "appendChild", codeNode, ) if parent.Call("contains", oldNode).Bool() { parent.Call( "replaceChild", newNode, oldNode, ) } else { parent.Call( "appendChild", newNode, ) } WebWindow().Call("HighlightCode") return newNode } func WebCleanChildren(node js.Value) { children := node.Get("children") for i := range children.Length() { node.Call("removeChild", children.Index(i)) } }