Commit f643bf2b authored by Siong-Ui Te's avatar Siong-Ui Te

state machine handle keyup

parent 470dba87
......@@ -7,45 +7,67 @@ import (
"github.com/siongui/gopalilib/lib/dicmgr"
)
var input *Object
var st *StateMachine
type StateMachine struct {
Input *Object
CurrentIndex int
}
func (s *StateMachine) HandleArrowUp() {
println("ArrowUp")
}
func (s *StateMachine) HandleArrowDown() {
println("ArrowDown")
}
func (s *StateMachine) HandleEnter() {
word := GetInputValue()
if dicmgr.Lookup(word) {
SetModalTitle(wordLinkHtml(word))
go showWordDefinitionInModal(word)
}
}
func (s *StateMachine) HandleDefault() {
word := GetInputValue()
SetModalWords(GetSuggestedWordsHtml(word, 7))
}
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key
func inputKeyupEventHandler(key string) {
switch key {
case "ArrowUp", "Up":
println("ArrowUp")
st.HandleArrowUp()
case "ArrowDown", "Down":
println("ArrowDown")
st.HandleArrowDown()
case "Enter":
word := GetInputValue()
if dicmgr.Lookup(word) {
SetModalTitle(wordLinkHtml(word))
go showWordDefinitionInModal(word)
}
st.HandleEnter()
default:
word := GetInputValue()
SetModalWords(GetSuggestedWordsHtml(word, 7))
st.HandleDefault()
}
}
func FocusInput() {
input.Focus()
st.Input.Focus()
}
func SetInputValue(v string) {
input.SetValue(v)
st.Input.SetValue(v)
}
func GetInputValue() string {
s := input.Value()
s := st.Input.Value()
s = strings.TrimSpace(s)
s = strings.ToLower(s)
return s
}
func SetupModalInput(selector string) {
input = Document.QuerySelector(selector)
input.AddEventListener("keyup", func(e Event) {
st = &StateMachine{}
st.Input = Document.QuerySelector(selector)
st.Input.AddEventListener("keyup", func(e Event) {
inputKeyupEventHandler(e.Key())
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment