diff --git a/src/editor/AceEditor.ts b/src/editor/AceEditor.ts index c7fd1b0..5b49017 100644 --- a/src/editor/AceEditor.ts +++ b/src/editor/AceEditor.ts @@ -108,6 +108,8 @@ export class AceEditor { private _nextId: number; private idleWorkTimer: any; private inInternationalComposition: any; + private endCompositionNow: (() => void) | null = null; + private compositionWatchdog: ReturnType | null = null; private thisKeyDoesntTriggerNormalize: boolean; private authorInfos: Record; private changesetTracker: ReturnType | null = null; @@ -3130,6 +3132,45 @@ export class AceEditor { // Private - Event Binding // ----------------------------------------------------------------------- + /** Closes an open composition and incorporates what was typed. */ + private endComposition(): void { + if (this.compositionWatchdog) { + clearTimeout(this.compositionWatchdog); + this.compositionWatchdog = null; + } + if (!this.inInternationalComposition) return; + const resolve = this.endCompositionNow; + this.inInternationalComposition = null; + this.endCompositionNow = null; + resolve?.(); + this.idleWorkTimer.atMost(0); + } + + /** + * Opens a composition if none is open (or re-opens one the watchdog closed + * while the browser was still composing) and re-arms the watchdog. + */ + private beginComposition(): void { + if (!this.inInternationalComposition) { + this.inInternationalComposition = new Promise((resolve) => { + this.endCompositionNow = resolve; + }); + } + this.armCompositionWatchdog(); + } + + /** Ends a composition that has been silent for too long (no compositionend). */ + private armCompositionWatchdog(): void { + if (this.compositionWatchdog) clearTimeout(this.compositionWatchdog); + this.compositionWatchdog = setTimeout(() => { + this.compositionWatchdog = null; + this.endComposition(); + }, AceEditor.COMPOSITION_TIMEOUT_MS); + } + + /** Longest pause inside a composition before it counts as abandoned. */ + static COMPOSITION_TIMEOUT_MS = 3000; + private bindTheEventHandlers(): void { // Key events this.targetBody.addEventListener('keydown', (e) => this.handleKeyEvent(e)); @@ -3144,8 +3185,16 @@ export class AceEditor { }); }); - // Input event - schedule incorporateUserChanges - this.targetBody.addEventListener('input', () => { + // Input event - schedule incorporateUserChanges. An input that is no + // longer part of a composition also ends one that never got its + // compositionend (see below). + this.targetBody.addEventListener('input', (e) => { + const composing = (e as InputEvent).isComposing; + if (composing === true) { + this.beginComposition(); + } else if (this.inInternationalComposition && composing === false) { + this.endComposition(); + } this.idleWorkTimer.atMost(0); }); @@ -3191,17 +3240,21 @@ export class AceEditor { this.idleWorkTimer.atMost(0); }); - // Composition events - this.targetBody.addEventListener('compositionstart', () => { - if (this.inInternationalComposition) return; - this.inInternationalComposition = new Promise((resolve) => { - this.targetBody.addEventListener('compositionend', () => { - this.inInternationalComposition = null; - resolve(); - // After composition ends, incorporate changes - this.idleWorkTimer.atMost(0); - }, {once: true}); - }); - }); + // Composition events (IME input, dictation, and iOS inline predictions). + // + // While a composition is open, local changes are not incorporated and + // NEW_CHANGES from the server wait (collab_client awaits + // getInInternationalComposition). iOS Safari does not always send + // compositionend - e.g. when the keyboard is dismissed, the field loses + // focus or a prediction is accepted - which left the pad "typing" locally + // while nothing reached the server. So a composition also ends on blur, on + // an input event that is no longer composing, and after a few seconds + // without any composition activity. A pause alone does not prove the + // composition is over, so composition activity after the watchdog fired + // (compositionupdate or a composing input event) opens it again. + this.targetBody.addEventListener('compositionstart', () => this.beginComposition()); + this.targetBody.addEventListener('compositionupdate', () => this.beginComposition()); + this.targetBody.addEventListener('compositionend', () => this.endComposition()); + this.targetBody.addEventListener('blur', () => this.endComposition()); } } diff --git a/stories/EpEditor.stories.ts b/stories/EpEditor.stories.ts index 17fecfe..e44c0e8 100644 --- a/stories/EpEditor.stories.ts +++ b/stories/EpEditor.stories.ts @@ -211,3 +211,74 @@ export const Monospace: Story = { > `, }; + +/** + * iOS Safari does not always send compositionend (keyboard dismissed, + * focus lost, prediction accepted). A composition that never ends used to + * hold back every local change and all server changes. + */ +export const CompositionAlwaysEnds: Story = { + args: { content: 'Hello' }, + render: (args: EpEditorArgs) => html` + + `, + play: async ({ canvasElement }) => { + const host = await getEditor(canvasElement); + const ace = host.editor!; + const AceClass = ace.constructor as unknown as { COMPOSITION_TIMEOUT_MS: number }; + const body = (ace as unknown as { targetBody: HTMLElement }).targetBody; + const composing = () => ace.getInInternationalComposition() != null; + const start = () => body.dispatchEvent(new CompositionEvent('compositionstart', { bubbles: true })); + const settle = () => new Promise((r) => setTimeout(r, 80)); + + // Ends on blur. + start(); + await expect(composing()).toBe(true); + body.dispatchEvent(new FocusEvent('blur')); + await expect(composing()).toBe(false); + + // Ends with an input event that is no longer composing; text typed + // during the composition is incorporated afterwards. + start(); + const line = body.querySelector('div')!; + line.append(document.createTextNode(' world')); + body.dispatchEvent(new InputEvent('input', { bubbles: true, isComposing: true, inputType: 'insertCompositionText', data: ' world' })); + await settle(); + await expect(composing()).toBe(true); + body.dispatchEvent(new InputEvent('input', { bubbles: true, isComposing: false, inputType: 'insertText', data: '' })); + await expect(composing()).toBe(false); + await settle(); + await expect(ace.exportText()).toContain('Hello world'); + + // Ends by itself when nothing happens for a while. + const saved = AceClass.COMPOSITION_TIMEOUT_MS; + AceClass.COMPOSITION_TIMEOUT_MS = 100; + try { + start(); + await expect(composing()).toBe(true); + await new Promise((r) => setTimeout(r, 200)); + await expect(composing()).toBe(false); + + // A pause longer than the timeout does not end a composition that is + // still going: further composition activity opens it again until the + // real compositionend. + body.dispatchEvent(new CompositionEvent('compositionupdate', { bubbles: true, data: 'x' })); + await expect(composing()).toBe(true); + await new Promise((r) => setTimeout(r, 200)); + await expect(composing()).toBe(false); + body.dispatchEvent(new InputEvent('input', { bubbles: true, isComposing: true, inputType: 'insertCompositionText', data: 'y' })); + await expect(composing()).toBe(true); + body.dispatchEvent(new CompositionEvent('compositionend', { bubbles: true })); + await expect(composing()).toBe(false); + } finally { + AceClass.COMPOSITION_TIMEOUT_MS = saved; + } + + // A normal compositionend still works, and the waiting promise resolves. + start(); + const waiting = ace.getInInternationalComposition() as Promise; + body.dispatchEvent(new CompositionEvent('compositionend', { bubbles: true })); + await waiting; + await expect(composing()).toBe(false); + }, +};