This commit is contained in:
parent
09fceca2f9
commit
40e5379eb3
11 changed files with 250 additions and 2 deletions
40
cmds/golines/main.go
Normal file
40
cmds/golines/main.go
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
//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 {}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue