41 lines
1.2 KiB
Go
41 lines
1.2 KiB
Go
|
|
//go:build js && wasm
|
||
|
|
|
||
|
|
package main
|
||
|
|
|
||
|
|
import "syscall/js"
|
||
|
|
|
||
|
|
func main() {
|
||
|
|
document := js.Global().Get("document")
|
||
|
|
element := func(id string) js.Value { return document.Call("getElementById", id) }
|
||
|
|
source, width := element("source"), element("max-len")
|
||
|
|
output, message := element("output"), element("message")
|
||
|
|
render := func() {
|
||
|
|
maxLen := width.Get("valueAsNumber").Int()
|
||
|
|
element("width-value").Set("textContent", maxLen)
|
||
|
|
tabLen := 4
|
||
|
|
if maxLen < 80 {
|
||
|
|
tabLen = 2
|
||
|
|
}
|
||
|
|
element("tab-value").Set("textContent", tabLen)
|
||
|
|
source.Get("style").Set("tabSize", tabLen)
|
||
|
|
output.Get("style").Set("tabSize", tabLen)
|
||
|
|
formatted, err := formatSource(source.Get("value").String(), maxLen)
|
||
|
|
if err != nil {
|
||
|
|
output.Set("value", "")
|
||
|
|
message.Set("textContent", err.Error())
|
||
|
|
source.Call("setAttribute", "aria-invalid", "true")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
source.Call("removeAttribute", "aria-invalid")
|
||
|
|
output.Set("value", string(formatted))
|
||
|
|
message.Set("textContent", "")
|
||
|
|
}
|
||
|
|
listener := js.FuncOf(func(this js.Value, args []js.Value) any { render(); return nil })
|
||
|
|
source.Call("addEventListener", "input", listener)
|
||
|
|
width.Call("addEventListener", "input", listener)
|
||
|
|
source.Set("disabled", false)
|
||
|
|
width.Set("disabled", false)
|
||
|
|
render()
|
||
|
|
select {}
|
||
|
|
}
|