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

highlight words when arrow up down in modal input

parent ec6d6d22
......@@ -17,21 +17,33 @@ type StateMachine struct {
}
func (s *StateMachine) HandleArrowUp() {
if s.CurrentIndex >= 0 && s.CurrentIndex < len(s.Words) {
UnhighlightWord(s.CurrentIndex, s.Words[s.CurrentIndex])
}
s.CurrentIndex--
if s.CurrentIndex == -1 {
if s.CurrentIndex < 0 {
s.CurrentIndex = 0
}
s.CurrentWord = s.Words[s.CurrentIndex]
SetInputValue(s.CurrentWord)
if s.CurrentIndex >= 0 && s.CurrentIndex < len(s.Words) {
HighlightWord(s.CurrentIndex, s.Words[s.CurrentIndex])
}
}
func (s *StateMachine) HandleArrowDown() {
if s.CurrentIndex >= 0 && s.CurrentIndex < len(s.Words) {
UnhighlightWord(s.CurrentIndex, s.Words[s.CurrentIndex])
}
s.CurrentIndex++
if s.CurrentIndex == len(s.Words) {
s.CurrentIndex = len(s.Words) - 1
}
s.CurrentWord = s.Words[s.CurrentIndex]
SetInputValue(s.CurrentWord)
if s.CurrentIndex >= 0 && s.CurrentIndex < len(s.Words) {
HighlightWord(s.CurrentIndex, s.Words[s.CurrentIndex])
}
}
func (s *StateMachine) HandleEnter() {
......@@ -55,7 +67,7 @@ func (s *StateMachine) HandleDefault() {
}
func ResetStateMachine(word string) {
st.CurrentIndex = 0
st.CurrentIndex = -1
st.CurrentWord = word
st.Words = dicmgr.GetSuggestedWords(word, 7)
SetModalWords(GetSuggestedWordsHtml(word, 7))
......
......@@ -3,6 +3,7 @@ package main
import (
"bytes"
"html/template"
"strconv"
. "github.com/siongui/godom"
"github.com/siongui/gopalilib/lib"
......@@ -20,15 +21,17 @@ const pwt = `
color: GoldenRod;
}
div.is-possible-word:hover {
.word-highlight {
color: red;
background-color: #F0F8FF;
cursor: pointer;
}
</style>
{{range $i, $possibleWord := .}}
<div class="is-possible-word is-size-5"
<div id="word-index-{{$i}}"
class="is-possible-word is-size-5"
onmouseenter="pwmeh({{$i}}, '{{$possibleWord}}')"
onmouseleave="pwmlh({{$i}}, '{{$possibleWord}}')"
onclick="pwh('{{$possibleWord}}')">
{{$possibleWord}}
</div>
......@@ -52,13 +55,27 @@ func possibleWordClickHandler(word string) {
}()
}
func HighlightWord(i int, word string) {
Document.QuerySelector("#word-index-" + strconv.Itoa(i)).ClassList().Add("word-highlight")
}
func UnhighlightWord(i int, word string) {
Document.QuerySelector("#word-index-" + strconv.Itoa(i)).ClassList().Remove("word-highlight")
}
func possibleWordMouseenterHandler(i int, word string) {
SetStateMachineCurrentIndexAndWord(i, word)
HighlightWord(i, word)
}
func possibleWordMouseleaveHandler(i int, word string) {
UnhighlightWord(i, word)
}
func GetSuggestedWordsHtml(word string, limit int) string {
Document.Set("pwh", possibleWordClickHandler)
Document.Set("pwmeh", possibleWordMouseenterHandler)
Document.Set("pwmlh", possibleWordMouseleaveHandler)
t, err := template.New("pwt").Parse(pwt)
if err != nil {
......
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