2019-03-31 05:58:26 +02:00
|
|
|
// +build js,wasm
|
|
|
|
|
2019-03-26 04:32:44 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/dennwc/dom/js"
|
2019-03-30 20:23:54 +01:00
|
|
|
"github.com/zorchenhimer/MovieNight/common"
|
2019-03-26 04:32:44 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-03-30 20:23:54 +01:00
|
|
|
keyTab = 9
|
|
|
|
keyEnter = 13
|
2019-03-31 22:00:36 +02:00
|
|
|
keyEsc = 27
|
2019-03-30 20:23:54 +01:00
|
|
|
keyUp = 38
|
|
|
|
keyDown = 40
|
|
|
|
suggestionName = '@'
|
|
|
|
suggestionEmote = ':'
|
2019-03-26 04:32:44 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-03-30 20:23:54 +01:00
|
|
|
currentSugType rune
|
|
|
|
currentSug string
|
|
|
|
filteredSug []string
|
|
|
|
names []string
|
|
|
|
emoteNames []string
|
|
|
|
emotes map[string]string
|
2019-03-26 04:32:44 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// The returned value is a bool deciding to prevent the event from propagating
|
|
|
|
func processMessageKey(this js.Value, v []js.Value) interface{} {
|
2019-03-30 20:23:54 +01:00
|
|
|
if len(filteredSug) == 0 || currentSug == "" {
|
2019-03-26 04:32:44 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
startIdx := v[0].Get("target").Get("selectionStart").Int()
|
|
|
|
keyCode := v[0].Get("keyCode").Int()
|
|
|
|
switch keyCode {
|
2019-03-31 22:00:36 +02:00
|
|
|
case keyEsc:
|
|
|
|
filteredSug = nil
|
|
|
|
currentSug = ""
|
|
|
|
currentSugType = 0
|
2019-03-26 04:32:44 +01:00
|
|
|
case keyUp, keyDown:
|
|
|
|
newidx := 0
|
2019-03-30 20:23:54 +01:00
|
|
|
for i, n := range filteredSug {
|
|
|
|
if n == currentSug {
|
2019-03-26 04:32:44 +01:00
|
|
|
newidx = i
|
|
|
|
if keyCode == keyDown {
|
|
|
|
newidx = i + 1
|
2019-03-30 20:23:54 +01:00
|
|
|
if newidx == len(filteredSug) {
|
2019-03-26 04:32:44 +01:00
|
|
|
newidx--
|
|
|
|
}
|
|
|
|
} else if keyCode == keyUp {
|
|
|
|
newidx = i - 1
|
|
|
|
if newidx < 0 {
|
|
|
|
newidx = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-03-30 20:23:54 +01:00
|
|
|
currentSug = filteredSug[newidx]
|
2019-03-26 04:32:44 +01:00
|
|
|
case keyTab, keyEnter:
|
|
|
|
msg := js.Get("msg")
|
|
|
|
val := msg.Get("value").String()
|
|
|
|
newval := val[:startIdx]
|
|
|
|
|
2019-03-30 20:23:54 +01:00
|
|
|
if i := strings.LastIndex(newval, string(currentSugType)); i != -1 {
|
|
|
|
var offset int
|
|
|
|
if currentSugType == suggestionName {
|
|
|
|
offset = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
newval = newval[:i+offset] + currentSug
|
2019-03-26 04:32:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
endVal := val[startIdx:]
|
|
|
|
if len(val) == startIdx || val[startIdx:][0] != ' ' {
|
|
|
|
// insert a space into val so selection indexing can be one line
|
|
|
|
endVal = " " + endVal
|
|
|
|
}
|
|
|
|
msg.Set("value", newval+endVal)
|
|
|
|
msg.Set("selectionStart", len(newval)+1)
|
|
|
|
msg.Set("selectionEnd", len(newval)+1)
|
|
|
|
|
|
|
|
// Clear out filtered names since it is no longer needed
|
2019-03-30 20:23:54 +01:00
|
|
|
filteredSug = nil
|
2019-03-26 04:32:44 +01:00
|
|
|
default:
|
|
|
|
// We only want to handle the caught keys, so return early
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
updateSuggestionDiv()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func processMessage(v []js.Value) {
|
|
|
|
msg := js.Get("msg")
|
|
|
|
text := strings.ToLower(msg.Get("value").String())
|
|
|
|
startIdx := msg.Get("selectionStart").Int()
|
|
|
|
|
2019-03-30 20:23:54 +01:00
|
|
|
filteredSug = nil
|
2019-03-26 04:32:44 +01:00
|
|
|
if len(text) != 0 {
|
2019-03-30 20:23:54 +01:00
|
|
|
if len(names) > 0 || len(emoteNames) > 0 {
|
2019-03-26 04:32:44 +01:00
|
|
|
var caretIdx int
|
|
|
|
textParts := strings.Split(text, " ")
|
|
|
|
|
|
|
|
for i, word := range textParts {
|
|
|
|
// Increase caret index at beginning if not first word to account for spaces
|
|
|
|
if i != 0 {
|
|
|
|
caretIdx++
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is possible to have a double space " ", which will lead to an
|
|
|
|
// empty string element in the slice. Also check that the index of the
|
|
|
|
// cursor is between the start of the word and the end
|
2019-03-30 20:23:54 +01:00
|
|
|
if len(word) > 0 && caretIdx <= startIdx && startIdx <= caretIdx+len(word) {
|
|
|
|
var suggestions []string
|
|
|
|
if word[0] == suggestionName {
|
|
|
|
currentSugType = suggestionName
|
|
|
|
suggestions = names
|
|
|
|
} else if word[0] == suggestionEmote {
|
|
|
|
suggestions = emoteNames
|
|
|
|
currentSugType = suggestionEmote
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range suggestions {
|
|
|
|
if len(word) == 1 || strings.Contains(strings.ToLower(s), word[1:]) {
|
|
|
|
filteredSug = append(filteredSug, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(filteredSug) > 10 {
|
|
|
|
break
|
2019-03-26 04:32:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-30 20:23:54 +01:00
|
|
|
if len(filteredSug) > 0 {
|
|
|
|
currentSug = ""
|
2019-03-26 04:32:44 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
caretIdx += len(word)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateSuggestionDiv()
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateSuggestionDiv() {
|
|
|
|
const selectedClass = ` class="selectedName"`
|
|
|
|
|
|
|
|
var divs []string
|
2019-03-30 20:23:54 +01:00
|
|
|
if len(filteredSug) > 0 {
|
2019-03-26 04:32:44 +01:00
|
|
|
// set current name to first if not set already
|
2019-03-30 20:23:54 +01:00
|
|
|
if currentSug == "" {
|
|
|
|
currentSug = filteredSug[0]
|
2019-03-26 04:32:44 +01:00
|
|
|
}
|
|
|
|
|
2019-03-30 20:23:54 +01:00
|
|
|
var hascurrentSuggestion bool
|
|
|
|
divs = make([]string, len(filteredSug))
|
2019-03-26 04:32:44 +01:00
|
|
|
|
|
|
|
// Create inner body of html
|
2019-03-30 20:23:54 +01:00
|
|
|
for i := range filteredSug {
|
2019-03-26 04:32:44 +01:00
|
|
|
divs[i] = "<div"
|
2019-03-30 20:23:54 +01:00
|
|
|
|
|
|
|
sug := filteredSug[i]
|
|
|
|
if sug == currentSug {
|
|
|
|
hascurrentSuggestion = true
|
2019-03-26 04:32:44 +01:00
|
|
|
divs[i] += selectedClass
|
|
|
|
}
|
2019-03-30 20:23:54 +01:00
|
|
|
divs[i] += ">"
|
|
|
|
|
|
|
|
if currentSugType == suggestionEmote {
|
|
|
|
divs[i] += common.EmoteToHtml(emotes[sug], sug)
|
|
|
|
}
|
|
|
|
|
|
|
|
divs[i] += sug + "</div>"
|
2019-03-26 04:32:44 +01:00
|
|
|
}
|
|
|
|
|
2019-03-30 20:23:54 +01:00
|
|
|
if !hascurrentSuggestion {
|
2019-03-26 04:32:44 +01:00
|
|
|
divs[0] = divs[0][:4] + selectedClass + divs[0][4:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// The \n is so it's easier to read th source in web browsers for the dev
|
|
|
|
js.Get("suggestions").Set("innerHTML", strings.Join(divs, "\n"))
|
|
|
|
}
|