Skip to content

Commit

Permalink
dispatch change event to vue-based input (#10)
Browse files Browse the repository at this point in the history
Co-authored-by: rocka <i@rocka.me>
  • Loading branch information
eagleoflqj and rocka authored Aug 23, 2024
1 parent 6db24e6 commit d73378e
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions page/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,34 @@ export function placePanel(dx: number, dy: number, anchorTop: number, anchorLeft
let preedit = ''
let preeditIndex = 0

export function setPreedit(text: string, index: number) {
function changeInput(commitText: string, preeditText: string, index: number) {
/*
____ pre|edit ____
^ ^
start end
____ commit pre|edit ____
*/
const input = getInputElement()
if (!input) {
return
}
const { length } = preedit
const start = input.selectionStart! - preeditIndex
const end = preedit ? start + length : input.selectionEnd!
input.value = input.value.slice(0, start) + text + input.value.slice(end)
const end = preedit ? start + preedit.length : input.selectionEnd!
input.value = input.value.slice(0, start) + commitText + preeditText + input.value.slice(end)
// This may be triggered by user clicking panel. Focus to ensure setting selectionEnd works.
input.focus()
input.selectionEnd = start + index
preedit = text
input.selectionEnd = start + commitText.length + index
// For vue-based input, this is needed to synchronize state.
input.dispatchEvent(new Event('change'))
preedit = preeditText
preeditIndex = index
}

export function setPreedit(text: string, index: number) {
changeInput('', text, index)
}

export function commit(text: string) {
const input = getInputElement()
if (!input) {
return
}
const { selectionStart, selectionEnd } = input
input.value = input.value.slice(0, selectionStart!) + text + input.value.slice(selectionEnd!)
input.selectionEnd = selectionStart! + text.length
changeInput(text, '', 0)
}

0 comments on commit d73378e

Please sign in to comment.