Skip to content

Commit cb44dff

Browse files
authored
turn off spellcheck when having preedit (#35)
1 parent 937017e commit cb44dff

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

page/client.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import getCaretCoordinates from 'textarea-caret'
2-
import { getInputElement } from './focus'
2+
import { getInputElement, setSpellCheck } from './focus'
33

44
let x = 0
55
let y = 0
@@ -72,6 +72,7 @@ ____ commit pre|edit ____
7272
input.dispatchEvent(new Event('change'))
7373
preedit = preeditText
7474
preeditIndex = i
75+
setSpellCheck(!preedit)
7576
}
7677

7778
export function setPreedit(text: string, index: number) {

page/focus.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ type Input = HTMLInputElement | HTMLTextAreaElement
55

66
let input: Input | null = null
77
let userClick = false
8+
let originalSpellCheck = true
9+
10+
// false means disable, true means respect the original value.
11+
export function setSpellCheck(spellCheck: boolean) {
12+
if (!input) {
13+
return
14+
}
15+
input.spellcheck = spellCheck ? originalSpellCheck : false
16+
}
817

918
export function clickPanel() {
1019
userClick = true
@@ -20,6 +29,7 @@ export function focus() {
2029
}
2130
input = <Input>document.activeElement
2231
input.addEventListener('mousedown', resetInput)
32+
originalSpellCheck = input.spellcheck
2333
Module.ccall('focus_in', 'void', [], [])
2434
}
2535

@@ -33,6 +43,7 @@ export function blur() {
3343
return
3444
}
3545
input.removeEventListener('mousedown', resetInput)
46+
input.spellcheck = originalSpellCheck
3647
input = null
3748
Module.ccall('focus_out', 'void', [], [])
3849
resetPreedit()

tests/test-preedit.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import type { Locator } from '@playwright/test'
2+
import { expect, test } from '@playwright/test'
3+
import { init } from './util'
4+
5+
function getSpellCheck(locator: Locator) {
6+
return locator.evaluate((el: HTMLElement) => el.spellcheck)
7+
}
8+
9+
test('Disable spellcheck', async ({ page }) => {
10+
await init(page)
11+
12+
const textarea = page.locator('textarea')
13+
expect(await getSpellCheck(textarea), 'Original value set by browser is true').toBe(true)
14+
15+
await textarea.click()
16+
await page.evaluate(() => {
17+
window.fcitx.setPreedit('pin xie', 7)
18+
})
19+
expect(await getSpellCheck(textarea), 'Spellcheck is turned off when there is preedit').toBe(false)
20+
21+
await page.evaluate(() => {
22+
window.fcitx.setPreedit('', 0)
23+
})
24+
expect(await getSpellCheck(textarea), 'Original spellcheck value is restored').toBe(true)
25+
})
26+
27+
test('Respect original spellcheck value (manually set false)', async ({ page }) => {
28+
await init(page)
29+
30+
const textarea = page.locator('textarea')
31+
await textarea.evaluate((el: HTMLElement) => {
32+
el.spellcheck = false
33+
})
34+
35+
await page.evaluate(() => {
36+
window.fcitx.setPreedit('pin xie', 7)
37+
})
38+
expect(await getSpellCheck(textarea), 'Spellcheck is turned off when there is preedit').toBe(false)
39+
40+
await page.evaluate(() => {
41+
window.fcitx.setPreedit('', 0)
42+
})
43+
expect(await getSpellCheck(textarea), 'Original spellcheck value is restored').toBe(false)
44+
})

0 commit comments

Comments
 (0)