From 6c1138d4ba4380167795ec2face8aee88800053e Mon Sep 17 00:00:00 2001 From: SamTV12345 Date: Tue, 24 Nov 2026 19:18:43 +0100 Subject: [PATCH 1/2] fix(editor): a composition always ends (iOS) While a composition is open, local changes are not incorporated and etherpad's collab client holds back NEW_CHANGES from the server. iOS Safari does not always send compositionend (keyboard dismissed, focus lost, inline prediction accepted), so the pad kept the typed text locally and sent nothing - and applied nothing from others. A composition now also ends on blur, on an input event with isComposing === false, and after 3 s without composition activity. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012RMvvMtnCwj6ycD9fwLYVu --- src/editor/AceEditor.ts | 64 +++++++++++++++++++++++++++++++------ stories/EpEditor.stories.ts | 59 ++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 10 deletions(-) diff --git a/src/editor/AceEditor.ts b/src/editor/AceEditor.ts index c7fd1b0..ffba74a 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,32 @@ 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); + } + + /** 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 +3172,13 @@ 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) => { + if (this.inInternationalComposition && (e as InputEvent).isComposing === false) { + this.endComposition(); + } this.idleWorkTimer.atMost(0); }); @@ -3191,17 +3224,28 @@ export class AceEditor { this.idleWorkTimer.atMost(0); }); - // Composition events + // 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. this.targetBody.addEventListener('compositionstart', () => { - if (this.inInternationalComposition) return; + if (this.inInternationalComposition) { + this.armCompositionWatchdog(); + 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}); + this.endCompositionNow = resolve; }); + this.armCompositionWatchdog(); }); + this.targetBody.addEventListener('compositionupdate', () => this.armCompositionWatchdog()); + 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..6022a43 100644 --- a/stories/EpEditor.stories.ts +++ b/stories/EpEditor.stories.ts @@ -211,3 +211,62 @@ 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); + } 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); + }, +}; From 154d48e6a7f05794f8761e4a8dde7a225cf569b5 Mon Sep 17 00:00:00 2001 From: SamTV12345 Date: Tue, 24 Nov 2026 19:56:35 +0100 Subject: [PATCH 2/2] fix(editor): composition activity re-opens a composition the watchdog closed A pause longer than the watchdog timeout does not prove the composition is over. compositionupdate and composing input events now re-open it, so text that is still being composed is not incorporated before the real compositionend. Addresses the Qodo review on #19. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012RMvvMtnCwj6ycD9fwLYVu --- src/editor/AceEditor.ts | 35 ++++++++++++++++++++++------------- stories/EpEditor.stories.ts | 12 ++++++++++++ 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/editor/AceEditor.ts b/src/editor/AceEditor.ts index ffba74a..5b49017 100644 --- a/src/editor/AceEditor.ts +++ b/src/editor/AceEditor.ts @@ -3146,6 +3146,19 @@ export class AceEditor { 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); @@ -3176,7 +3189,10 @@ export class AceEditor { // longer part of a composition also ends one that never got its // compositionend (see below). this.targetBody.addEventListener('input', (e) => { - if (this.inInternationalComposition && (e as InputEvent).isComposing === false) { + const composing = (e as InputEvent).isComposing; + if (composing === true) { + this.beginComposition(); + } else if (this.inInternationalComposition && composing === false) { this.endComposition(); } this.idleWorkTimer.atMost(0); @@ -3233,18 +3249,11 @@ export class AceEditor { // 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. - this.targetBody.addEventListener('compositionstart', () => { - if (this.inInternationalComposition) { - this.armCompositionWatchdog(); - return; - } - this.inInternationalComposition = new Promise((resolve) => { - this.endCompositionNow = resolve; - }); - this.armCompositionWatchdog(); - }); - this.targetBody.addEventListener('compositionupdate', () => this.armCompositionWatchdog()); + // 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 6022a43..e44c0e8 100644 --- a/stories/EpEditor.stories.ts +++ b/stories/EpEditor.stories.ts @@ -258,6 +258,18 @@ export const CompositionAlwaysEnds: Story = { 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; }