aboutsummaryrefslogtreecommitdiff
path: root/go/ui/textInput_wasm.go
blob: 0ced3ac483141594e54b24f6d041a7ebcfbe3487 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package ui

import (
	"log"
	"syscall/js"
	"unicode/utf8"
)

// rmueller's version from:
// ://stackoverflow.com/questions/1752414/how-to-reverse-a-string-in-go?page=1&tab=scoredesc#tab-top
func reverseString(s string) string {
	size := len(s)
	buf := make([]byte, size)
	for start := 0; start < size; {
		r, n := utf8.DecodeRuneInString(s[start:])
		start += n
		utf8.EncodeRune(buf[size-start:], r)
	}
	return string(buf)
}

func (ti *TextInput) Focus(focus bool) {
	ti.focused = focus
	log.Printf("%v set focus: %v\n", ti, focus)
	if focus {
		d := js.Global().Get("document")
		i := d.Call("getElementById", "hiddenInput")
		// i.Call("focus", map[string]any{"preventScroll": true, "focusVisible": false})
		i.Call("focus")
		log.Println("open keyboard")

		log.Printf("set hidden input value to %s", ti.Text())
		// i.Set("value", ti.Text())
		i.Set("value", reverseString(ti.Text()))
	}
}

func (ti *TextInput) Update() error {
	if ti.focused {
		d := js.Global().Get("document")
		i := d.Call("getElementById", "hiddenInput")
		// t := i.Get("value").String()
		t := reverseString(i.Get("value").String())
		ti.SetInput(t)
	}
	return nil
}

func (*TextInput) IsUpdatable() bool { return true }