diff --git a/.changeset/830-ios-keyboard.md b/.changeset/830-ios-keyboard.md index d7bb1c98..f5464ffa 100644 --- a/.changeset/830-ios-keyboard.md +++ b/.changeset/830-ios-keyboard.md @@ -1,5 +1,6 @@ --- 'aicodeman': patch +'xterm-zerolag-input': patch --- Fix iOS keyboard suggestions, phrase replacement, dictation and hold-delete by retaining native input ownership in each terminal pane. Preserve local-echo Enter ordering across session switches, reset native editing at toolbar and programmatic input boundaries, and show the native trackpad caret against the visible input, including emoji-safe deletion. diff --git a/packages/xterm-zerolag-input/README.md b/packages/xterm-zerolag-input/README.md index 135bfa09..12e535dd 100644 --- a/packages/xterm-zerolag-input/README.md +++ b/packages/xterm-zerolag-input/README.md @@ -203,7 +203,7 @@ Implements the xterm.js `ITerminalAddon` interface. It deliberately does **not** |--------|---------|-------------| | `addChar(char)` | `void` | Add a single printable character. Auto-detects existing buffer text on the first keystroke. | | `appendText(text)` | `void` | Append multiple characters (paste). | -| `removeChar()` | `'pending'` \| `'flushed'` \| `false` | Remove the last character and drop any IME composition. See [backspace handling](#backspace-handling). | +| `removeChar()` | `'pending'` \| `'flushed'` \| `false` | Remove the last grapheme cluster (one user-perceived character: a red heart, a skin-tone emoji, a flag or a ZWJ family is one) and drop any IME composition. See [backspace handling](#backspace-handling). | | `clear()` | `void` | Clear all state, the composition included, and hide the overlay. Call on Enter, Ctrl+C, Escape. | | `setComposition(text)` | `void` | Show text an IME is still composing as an underlined tail after the typed text. Pass `''` to remove it. See [IME composition](#ime-composition). | | `setRowLimit(limit)` | `void` | Narrow the row bound the overlay keeps itself on screen with (normally `terminal.rows`) to the first `limit` screen rows, so it grows upward from the last of them; `null` restores all rows, and a limit above `terminal.rows` changes nothing. For hosts that keep the terminal taller than the box showing it, e.g. while an on-screen keyboard covers the bottom. | @@ -220,6 +220,8 @@ Implements the xterm.js `ITerminalAddon` interface. It deliberately does **not** The cascade order is pending text, then flushed text, then auto-detected buffer text (which is what makes backspace work after tab completion). Backspace "just works" across any combination of typed, in-flight and completed text. +One call removes one grapheme cluster: the unit a soft-keyboard Backspace removes, and what Claude Code and Codex delete per `\x7f`. So when it returns `'flushed'`, one `\x7f` removes the same character in those CLIs; a line editor that deletes by code point (bash readline, for a skin tone or a flag) removes less. For `'pending'` or `false`, send nothing. Grapheme boundaries come from `Intl.Segmenter`; where it is missing, one code point is removed instead. + ### IME composition While an input method (Japanese kana, Chinese pinyin, Korean) is still composing, the text is not committed yet, so it is not in `pendingText` either. `setComposition(text)` draws it as an underlined, `aria-hidden` tail right after the pending and flushed text, using the same wrapping and on-screen layout as the rest of the overlay. @@ -260,6 +262,8 @@ Finds text that exists after the prompt but was never typed through the overlay. |--------|-------------| | `rerender()` | Force a re-render. Call after buffer reloads, screen redraws, resizes and reconnects. A no-op when there is nothing to draw, so it needs no guard. | | `refreshFont()` | Re-cache font and color properties from the terminal. Call after a font size or theme change. | +| `getInputEndAnchor()` | Where the overlay's visible text ends, from its last layout: `{ col, row }` in viewport cells (`col` equals the column count at a full line's right edge), or `null` while the overlay is hidden. For placing a caret marker against the drawn input. | +| `onLayout(callback)` | Call `callback` after each layout, a hide included. Returns an unsubscribe function. | ### Prompt diff --git a/src/web/public/terminal-keycode229-recovery.js b/src/web/public/terminal-keycode229-recovery.js index d3fd3dfc..0e86ad90 100644 --- a/src/web/public/terminal-keycode229-recovery.js +++ b/src/web/public/terminal-keycode229-recovery.js @@ -60,15 +60,32 @@ (function (global) { 'use strict'; + // Grapheme clusters, the unit a soft-keyboard Backspace removes, Claude Code and + // Codex delete per DEL, and the local-echo overlay drops per DEL. Code points + // stand in where Intl.Segmenter is missing (same rule as terminal-ios-native-input.js). + const graphemeSegmenter = (() => { + try { + return typeof Intl !== 'undefined' && typeof Intl.Segmenter === 'function' + ? new Intl.Segmenter(undefined, { granularity: 'grapheme' }) + : null; + } catch { + return null; + } + })(); + + function graphemes(text) { + return graphemeSegmenter ? Array.from(graphemeSegmenter.segment(text), (part) => part.segment) : Array.from(text); + } + /** * What turns `previous` into `next`, as a terminal sees it: how many characters to delete from * the END of the line, then what to type. Everything after the common prefix is treated as - * replaced, because a terminal can only edit at its cursor. Counts are code points, so an - * emoji is one DEL, as it is one backspace. + * replaced, because a terminal can only edit at its cursor. Counts are grapheme clusters, so a + * red heart, a skin-tone emoji, a flag or a ZWJ family is one DEL, as it is one backspace. */ function editBetween(previous, next) { - const a = Array.from(previous); - const b = Array.from(next); + const a = graphemes(previous); + const b = graphemes(next); let prefix = 0; const max = Math.min(a.length, b.length); while (prefix < max && a[prefix] === b[prefix]) prefix += 1; diff --git a/test/terminal-keycode229-recovery.test.ts b/test/terminal-keycode229-recovery.test.ts index bf029e36..2e0f1f3c 100644 --- a/test/terminal-keycode229-recovery.test.ts +++ b/test/terminal-keycode229-recovery.test.ts @@ -13,6 +13,14 @@ import { readFileSync } from 'node:fs'; import vm from 'node:vm'; import { describe, expect, it } from 'vitest'; +import { ZerolagInputAddon } from '../packages/xterm-zerolag-input/src/zerolag-input-addon.js'; + +const MULTI_CODE_POINT_EMOJI = [ + ['red heart', '\u2764\uFE0F'], + ['skin tone', '\u{1F44D}\u{1F3FD}'], + ['flag', '\u{1F1EB}\u{1F1F7}'], + ['ZWJ family', '\u{1F468}\u200D\u{1F469}\u200D\u{1F467}\u200D\u{1F466}'], +] as const; type Listener = (event: Record) => void; @@ -741,6 +749,45 @@ describe('edit-based sync of the helper textarea (autocorrect replacements)', () expect(e.sent.slice(2)).toEqual(['\x7f']); }); + it.each(MULTI_CODE_POINT_EMOJI)('deletes a whole %s with one DEL', (_name, emoji) => { + const e = editSyncHarness(); + e.create(true); + typeKeys(e, 'ok'); + e.keydown(); + e.edit(`ok${emoji}`); + e.flush(); + const before = e.sent.length; + e.keydown(); + e.edit('ok'); + e.flush(); + expect(e.sent.slice(before)).toEqual(['\x7f']); + }); + + it.each(MULTI_CODE_POINT_EMOJI)( + 'keeps the local-echo draft at ok when the %s is deleted (real overlay)', + (_name, emoji) => { + const e = editSyncHarness(); + e.create(true); + typeKeys(e, 'ok'); + e.keydown(); + e.edit(`ok${emoji}`); + e.flush(); + // Local echo buffers what the recovery emits; each DEL drops one overlay grapheme. + const overlay = new ZerolagInputAddon(); + overlay.appendText(e.sent.join('')); + expect(overlay.pendingText).toBe(`ok${emoji}`); + const before = e.sent.length; + e.keydown(); + e.edit('ok'); + e.flush(); + for (const data of e.sent.slice(before)) { + if (data === '\x7f') expect(overlay.removeChar()).toBe('pending'); + else overlay.appendText(data); + } + expect(overlay.pendingText).toBe('ok'); + } + ); + it('stays out of the way while composing, and leaves xterm alone without its internals', () => { const h = editSyncHarness(); h.create(true); @@ -770,12 +817,13 @@ describe('edit-based sync of the helper textarea (autocorrect replacements)', () expect(terminalSource).toMatch(/getCompositionHelper:\s*\(\)\s*=>\s*this\.terminal\?\._core\?\._compositionHelper/); }); - it('editBetween counts code points and replaces everything after the common prefix', () => { + it('editBetween counts grapheme clusters and replaces everything after the common prefix', () => { const { editBetween } = editSyncHarness().exposed.CodemanKeyCode229Recovery; expect(editBetween('abc', 'abcd')).toEqual({ deleted: 0, inserted: 'd' }); expect(editBetween('abc', 'ab')).toEqual({ deleted: 1, inserted: '' }); expect(editBetween('testing the peompt', 'testing the prompt ')).toEqual({ deleted: 5, inserted: 'rompt ' }); expect(editBetween('x😀', 'x')).toEqual({ deleted: 1, inserted: '' }); + expect(editBetween('x\u{1F44D}\u{1F3FD}', 'x')).toEqual({ deleted: 1, inserted: '' }); expect(editBetween('', '')).toEqual({ deleted: 0, inserted: '' }); }); });