From c55af9ec39520a87028af8da8816f276a858948a Mon Sep 17 00:00:00 2001 From: Teigen Date: Wed, 17 Jun 2026 17:40:50 +0800 Subject: [PATCH] fix(iPad): CJK input positioning, paste dialog, and voice dictation duplication MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three iPad-specific issues fixed: 1. CJK input hidden behind keyboard: updateLayoutForKeyboard() gate changed from screen-size to touch-device detection. On iPad, CJK textarea (always position:fixed) gets bottom offset computed from keyboard HEIGHT directly instead of keyboardOffset (which depends on visualViewport.offsetTop that iOS adjusts when the CJK textarea receives focus). Toolbar/accessory bar transforms remain phone-only (they're normal-flow on iPad). 2. Paste dialog invisible on iPad: paste overlay CSS was inside @media (max-width: 430px) phone breakpoint — iPad (≥768px) had no styling. Extracted to universal section alongside keyboard accessory bar styles. 3. Voice dictation character duplication (Doubao/third-party IME): iOS voice dictation does NOT fire composition events (WebKit Bug 261764). Text arrives as bare input events; refinement is a delete→reinsert cycle. Rewrote CJK input handler with two-tier debounce: - Keyboard typing (no delete/replacement events): 150ms debounce - Dictation mode (deleteContentBackward or insertReplacementText detected): 1500ms debounce, persists 3s to cover multi-word dictation - Composition path (compositionend): immediate flush, unchanged - Keydown singles/Enter/Esc/Ctrl: immediate, unchanged Also: keep cjkActive=true on blur while CJK is visible (prevents xterm from processing duplicate input when iOS dictation UI steals focus); keydown single-char sends tracked via timestamp to suppress the echo input event that third-party IMEs fire despite preventDefault. --- src/web/public/input-cjk.js | 157 +++++++++++++++++++--------- src/web/public/mobile-handlers.js | 80 +++++++-------- src/web/public/mobile.css | 163 +++++++++++++++--------------- 3 files changed, 231 insertions(+), 169 deletions(-) diff --git a/src/web/public/input-cjk.js b/src/web/public/input-cjk.js index cb398548..9390a039 100644 --- a/src/web/public/input-cjk.js +++ b/src/web/public/input-cjk.js @@ -14,13 +14,21 @@ * This means compositionstart fires even for English text, and compositionend * may not fire until the user explicitly confirms (space, candidate tap). * - * We use InputEvent.inputType to distinguish: - * - `insertCompositionText`: tentative text, may change (CJK candidates, pinyin) - * - `insertText`: final committed text (confirmed word, punctuation, space) + * During composition, all input events are ignored — only compositionend + * triggers a flush (CJK candidate selection). * - * During composition, `insertText` events are flushed immediately (punctuation, - * English words confirmed by IME). `insertCompositionText` waits for - * compositionend (CJK candidate selection). + * ## iOS dictation challenge (WebKit Bug 261764) + * + * iOS/iPadOS voice dictation does NOT fire composition events. Text arrives + * as bare input events with isComposing === false. Dictation refinement is + * a delete→reinsert cycle (deleteContentBackward + insertReplacementText), + * all within a few ms. Flushing on every input event would send irrevocable + * provisional text to the PTY, causing duplication when the IME replaces it. + * + * Solution: outside composition, flush is DEBOUNCED (200ms). The entire + * delete→reinsert cycle collapses into one flush of the final textarea value. + * Keyboard typing of single printable characters still goes through the + * keydown handler (immediate, no debounce). * * ## Phantom character for Android backspace * @@ -44,11 +52,27 @@ const CjkInput = (() => { let _send = null; let _initialized = false; let _composing = false; + let _flushTimer = null; + let _dictationActive = false; + let _dictationDecayTimer = null; + let _keydownSentAt = 0; const _listeners = {}; - // Zero-width space: always present in textarea so Android backspace has - // something to delete, triggering the `input` event we need to detect it. - const PHANTOM = '\u200B'; + const PHANTOM = '​'; + + // Two-tier debounce for non-composition input: + // - KEYBOARD: short debounce (third-party IMEs like Doubao may not fire + // composition events even for keyboard CJK typing) + // - DICTATION: long debounce (iOS voice dictation sends delete→reinsert + // refinement cycles without composition events — WebKit Bug 261764) + // + // Dictation is detected by deleteContentBackward on non-empty text or + // insertReplacementText — signals that the IME is rewriting provisional + // text. Once detected, dictation mode persists for 3s (covers multi-word + // dictation with natural pauses between words). + const DEBOUNCE_KEYBOARD_MS = 150; + const DEBOUNCE_DICTATION_MS = 1500; + const DICTATION_DECAY_MS = 3000; const PASSTHROUGH_KEYS = { ArrowUp: '\x1b[A', @@ -64,18 +88,15 @@ const CjkInput = (() => { c: '\x03', d: '\x04', l: '\x0c', z: '\x1a', a: '\x01', e: '\x05', }; - /** Strip phantom characters from a string */ function _strip(str) { - return str.replace(/\u200B/g, ''); + return str.replace(/​/g, ''); } - /** Reset textarea to phantom-only state with cursor at end */ function _resetToPhantom() { _textarea.value = PHANTOM; _textarea.setSelectionRange(1, 1); } - /** Check if textarea contains only phantom(s) or is empty — no real user text */ function _isEffectivelyEmpty() { return !_strip(_textarea.value); } @@ -89,12 +110,41 @@ const CjkInput = (() => { _resetToPhantom(); } + /** Cancel any pending debounced flush */ + function _cancelDebouncedFlush() { + if (_flushTimer) { + clearTimeout(_flushTimer); + _flushTimer = null; + } + } + + /** Mark that dictation rewriting is in progress */ + function _enterDictationMode() { + _dictationActive = true; + clearTimeout(_dictationDecayTimer); + _dictationDecayTimer = setTimeout(() => { + _dictationActive = false; + _dictationDecayTimer = null; + }, DICTATION_DECAY_MS); + } + + /** Schedule a flush after input settles */ + function _debouncedFlush() { + _cancelDebouncedFlush(); + const delay = _dictationActive ? DEBOUNCE_DICTATION_MS : DEBOUNCE_KEYBOARD_MS; + _flushTimer = setTimeout(() => { + _flushTimer = null; + _flush(); + }, delay); + } + return { init({ send }) { if (_initialized) this.destroy(); _send = send; _composing = false; + _flushTimer = null; _textarea = document.getElementById('cjkInput'); if (!_textarea) return this; @@ -105,20 +155,28 @@ const CjkInput = (() => { window.cjkActive = true; if (!_textarea.value) _resetToPhantom(); }; - _listeners.blur = () => { window.cjkActive = false; }; + _listeners.blur = () => { + // Keep cjkActive while CJK input is visible — iOS dictation and system + // UI may steal focus temporarily, and clearing the flag during that + // window lets xterm's onData process duplicated input. + if (!_textarea.classList.contains('cjk-input-visible')) { + window.cjkActive = false; + } + }; _textarea.addEventListener('mousedown', _listeners.mousedown); _textarea.addEventListener('focus', _listeners.focus); _textarea.addEventListener('blur', _listeners.blur); - // ── Composition tracking ── + // ── Composition tracking (keyboard IME — works for CJK typing) ── _listeners.compositionstart = () => { _composing = true; + _cancelDebouncedFlush(); // Leave textarea.value untouched — programmatic changes during // compositionstart cancel the IME composition on iOS Safari. - // The phantom (U+200B) is invisible and _strip() removes it on flush. }; _listeners.compositionend = () => { _composing = false; + _cancelDebouncedFlush(); // Defer flush: some Android IMEs haven't committed text to textarea // when compositionend fires. setTimeout(0) ensures we read the final value. setTimeout(_flush, 0); @@ -128,13 +186,10 @@ const CjkInput = (() => { // ── Keydown: special keys work REGARDLESS of composition state ── _listeners.keydown = (e) => { - // Enter: flush accumulated text (or bare Enter if empty). - // No isComposing guard — Android IMEs set isComposing=true for English - // prediction, but Enter should ALWAYS send. We preventDefault to stop - // the IME from also handling Enter (which could double-send or do nothing). if (e.key === 'Enter') { e.preventDefault(); _composing = false; + _cancelDebouncedFlush(); const val = _strip(_textarea.value); if (val) { _send(val + '\r'); @@ -145,15 +200,14 @@ const CjkInput = (() => { return; } - // Escape: clear textarea (always works) if (e.key === 'Escape') { e.preventDefault(); _composing = false; + _cancelDebouncedFlush(); _resetToPhantom(); return; } - // Ctrl combos: forward to PTY (always works) if (e.ctrlKey && CTRL_KEYS[e.key]) { e.preventDefault(); _send(CTRL_KEYS[e.key]); @@ -164,7 +218,6 @@ const CjkInput = (() => { if (_composing) return; // Backspace: forward to PTY when no real text in textarea - // (Desktop path — Android uses the input event + phantom approach) if (e.key === 'Backspace' && _isEffectivelyEmpty()) { e.preventDefault(); _send('\x7f'); @@ -179,55 +232,62 @@ const CjkInput = (() => { return; } - // Single printable character: send immediately to PTY - // (Desktop keyboards with physical keys — Android sends 'Unidentified') + // Single printable character: send immediately to PTY. + // Third-party IMEs on iOS may ignore preventDefault, so the char + // still enters the textarea and fires an input event — _keydownSentAt + // tells the input handler to skip that echo. if (e.key.length === 1 && !e.ctrlKey && !e.altKey && !e.metaKey && _isEffectivelyEmpty()) { e.preventDefault(); _send(e.key); + _keydownSentAt = performance.now(); + _resetToPhantom(); return; } }; _textarea.addEventListener('keydown', _listeners.keydown); - // ── Input event: the primary path for Android virtual keyboards ── - // Android sends keyCode 229 + key "Unidentified" for virtual key presses, - // making keydown unreliable. input fires AFTER character insertion and - // carries inputType which tells us whether the text is final or tentative. + // ── Input event: primary path for virtual keyboards + dictation ── _listeners.input = (e) => { // ── Backspace / delete detection ── - // Android long-press backspace generates rapid deleteContentBackward events. - // The phantom character ensures the textarea is never truly empty, so each - // press/repeat fires an input event that we can catch here. if (e.inputType === 'deleteContentBackward' || e.inputType === 'deleteWordBackward') { + if (_composing) return; if (_isEffectivelyEmpty()) { - // No real text left — forward backspace to PTY + _cancelDebouncedFlush(); _send('\x7f'); _resetToPhantom(); return; } - // User is editing their own text in the textarea — let it be. - // Ensure phantom is still present for the NEXT backspace. + // Delete on non-empty text outside composition = dictation rewrite. + // The IME is revising provisional text — switch to long debounce. + _enterDictationMode(); if (!_textarea.value.startsWith(PHANTOM)) { _textarea.value = PHANTOM + _textarea.value; _textarea.setSelectionRange(1, 1); } + _debouncedFlush(); return; } - if (_composing) { - // insertText during composition = IME committed final text - // (e.g., punctuation key inserts 。directly, or IME confirms a word). - // Flush immediately — this text won't change. - if (e.inputType === 'insertText') { - _flush(); - return; - } - // insertCompositionText = IME is still working (pinyin, candidates, - // English prediction). Wait for compositionend to flush. + // insertReplacementText = dictation/autocorrect refinement + if (e.inputType === 'insertReplacementText') { + _enterDictationMode(); + _debouncedFlush(); return; } - // Outside composition: send immediately - _flush(); + + if (_composing) return; + + // Keydown handler already sent this character — just clear the + // textarea echo that the IME inserted despite preventDefault. + if (performance.now() - _keydownSentAt < 100) { + _resetToPhantom(); + return; + } + + // Outside composition: keyboard typing or voice dictation. + // If dictation mode was detected (delete/replacement events seen + // recently), use long debounce. Otherwise short debounce for keyboard. + _debouncedFlush(); }; _textarea.addEventListener('input', _listeners.input); @@ -236,6 +296,9 @@ const CjkInput = (() => { }, destroy() { + _cancelDebouncedFlush(); + clearTimeout(_dictationDecayTimer); + _dictationActive = false; if (_textarea) { for (const [event, handler] of Object.entries(_listeners)) { if (handler) _textarea.removeEventListener(event, handler); diff --git a/src/web/public/mobile-handlers.js b/src/web/public/mobile-handlers.js index f55bd2e1..be88a5a4 100644 --- a/src/web/public/mobile-handlers.js +++ b/src/web/public/mobile-handlers.js @@ -291,57 +291,50 @@ const KeyboardHandler = { updateLayoutForKeyboard() { if (!window.visualViewport) return; - // Only adjust on mobile - if (!MobileDetection.isSmallScreen() && !MobileDetection.isMediumScreen()) { + if (!MobileDetection.isTouchDevice()) { this.resetLayout(); return; } - const toolbar = document.querySelector('.toolbar'); - const accessoryBar = document.querySelector('.keyboard-accessory-bar'); const cjkInput = document.getElementById('cjkInput'); - const main = document.querySelector('.main'); + const isSmallMedium = MobileDetection.isSmallScreen() || MobileDetection.isMediumScreen(); if (this.keyboardVisible) { - // Calculate how far the toolbar (position:fixed, bottom:0) needs to - // translate up so it sits at the bottom of the visual viewport. - // This formula accounts for iOS scrolling the visual viewport (offsetTop) - // when the user types in xterm's hidden textarea. - // - // MUST measure against the LAYOUT viewport (window.innerHeight): the - // bars are position:fixed, which anchors to the layout viewport — on - // iOS that keeps its full height while the keyboard is open. Measuring - // the shrunken .app instead (its height tracks --app-height = visual - // viewport) made the offset compute to 0 on iOS, leaving the toolbar - // and accessory bar behind the OS keyboard (0.9.8 regression). On - // Android the layout viewport itself shrinks with the keyboard, so - // innerHeight === visualBottom and the offset is naturally 0 there. - const layoutHeight = window.innerHeight; - const visualBottom = window.visualViewport.offsetTop + window.visualViewport.height; - const keyboardOffset = Math.max(0, layoutHeight - visualBottom); - - // Move toolbar and accessory bar above keyboard. - // When keyboardOffset is 0 (viewport scrolled to layout bottom), - // the bars are naturally positioned via their CSS bottom values — - // just clear the transforms. Never dismiss keyboard state here; - // that's handleViewportResize's job. - if (toolbar) { - toolbar.style.transform = keyboardOffset > 0 ? `translateY(${-keyboardOffset}px)` : ''; - } - if (accessoryBar) { - accessoryBar.style.transform = keyboardOffset > 0 ? `translateY(${-keyboardOffset}px)` : ''; - } - if (cjkInput?.classList.contains('cjk-input-visible')) { - cjkInput.style.transform = keyboardOffset > 0 ? `translateY(${-keyboardOffset}px)` : ''; - } - - // Reserve only Codeman's visible controls. The OS keyboard is outside - // the visual viewport; adding its height here creates a large blank area - // above the mobile toolbar on iPhone. const keyboardHeight = this.initialViewportHeight - (window.visualViewport.height || window.innerHeight); - if (main && keyboardHeight > 0) { - const cjkInputHeight = cjkInput?.classList.contains('cjk-input-visible') ? 44 : 0; - main.style.paddingBottom = `${84 + cjkInputHeight}px`; + + // Toolbar and accessory bar: only transform on phones/small tablets where + // they are position:fixed. On iPad (≥768px) they're in normal flow inside + // the shrunk .app container and already sit above the keyboard. + if (isSmallMedium) { + const toolbar = document.querySelector('.toolbar'); + const accessoryBar = document.querySelector('.keyboard-accessory-bar'); + const main = document.querySelector('.main'); + + const layoutHeight = window.innerHeight; + const visualBottom = window.visualViewport.offsetTop + window.visualViewport.height; + const keyboardOffset = Math.max(0, layoutHeight - visualBottom); + + if (toolbar) { + toolbar.style.transform = keyboardOffset > 0 ? `translateY(${-keyboardOffset}px)` : ''; + } + if (accessoryBar) { + accessoryBar.style.transform = keyboardOffset > 0 ? `translateY(${-keyboardOffset}px)` : ''; + } + if (main && keyboardHeight > 0) { + const cjkInputHeight = cjkInput?.classList.contains('cjk-input-visible') ? 44 : 0; + main.style.paddingBottom = `${84 + cjkInputHeight}px`; + } + } + + // CJK textarea: position on ALL touch devices (it's always position:fixed). + // Uses keyboard HEIGHT directly — independent of visualViewport.offsetTop + // which iOS adjusts when the CJK textarea receives focus/dictation. + if (cjkInput?.classList.contains('cjk-input-visible') && keyboardHeight > 0) { + // On phones, sit above toolbar+accessory (84px). On tablets in normal + // flow, the toolbar/accessory are in-flow so use a smaller offset. + const bottomOffset = isSmallMedium ? 84 : 44; + cjkInput.style.bottom = `${keyboardHeight + bottomOffset}px`; + cjkInput.style.transform = ''; } } else { this.resetLayout(); @@ -363,6 +356,7 @@ const KeyboardHandler = { } if (cjkInput) { cjkInput.style.transform = ''; + cjkInput.style.bottom = ''; } if (main) { main.style.paddingBottom = ''; diff --git a/src/web/public/mobile.css b/src/web/public/mobile.css index b12c16e1..3838a9b9 100644 --- a/src/web/public/mobile.css +++ b/src/web/public/mobile.css @@ -1065,85 +1065,7 @@ html.mobile-init .file-browser-panel { } /* Paste overlay for iOS clipboard access */ - .paste-overlay { - position: fixed; - inset: 0; - background: rgba(0, 0, 0, 0.6); - z-index: 10000; - display: flex; - align-items: flex-start; - justify-content: center; - padding-top: 15vh; - } - - .paste-dialog { - background: var(--bg-secondary, #1e1e2e); - border: 1px solid var(--border-color, #444); - border-radius: 12px; - padding: 12px; - width: calc(100% - 24px); - max-width: 400px; - } - - .paste-textarea { - width: 100%; - min-height: 80px; - max-height: 200px; - background: var(--bg-primary, #0d0d14); - color: var(--text-primary, #e0e0e0); - border: 1px solid var(--border-color, #444); - border-radius: 8px; - padding: 8px; - font-family: inherit; - font-size: 16px; - resize: none; - box-sizing: border-box; - } - - .paste-textarea:focus { - outline: none; - border-color: var(--accent-color, #7aa2f7); - } - - .paste-actions { - display: flex; - justify-content: flex-end; - gap: 8px; - margin-top: 10px; - } - - .paste-cancel, .paste-new, .paste-send, .paste-image { - padding: 8px 18px; - border: none; - border-radius: 8px; - font-size: 14px; - cursor: pointer; - } - - /* Image attach button — left-aligned, accent outline */ - .paste-image { - margin-right: auto; - background: var(--bg-tertiary, #333); - color: var(--accent-color, #7aa2f7); - border: 1px solid var(--accent-color, #7aa2f7); - } - - .paste-cancel { - background: var(--bg-tertiary, #333); - color: var(--text-secondary, #aaa); - } - - .paste-new { - background: var(--bg-tertiary, #333); - color: var(--accent-color, #7aa2f7); - border: 1px solid var(--accent-color, #7aa2f7); - } - - .paste-send { - background: var(--accent-color, #7aa2f7); - color: #fff; - font-weight: 600; - } + /* Paste overlay styles extracted to universal section below (line ~2293+) */ /* LEGACY: Hide old toolbar select (no longer used on mobile) */ .toolbar-select { @@ -2267,6 +2189,89 @@ html.mobile-init .file-browser-panel { background: #3d5f85; } +/* ============================================================================ + Paste Overlay — all mobile/tablet sizes + Extracted from phone breakpoint so it applies on iPad too. + ============================================================================ */ +.paste-overlay { + position: fixed; + inset: 0; + background: rgba(0, 0, 0, 0.6); + z-index: 10000; + display: flex; + align-items: flex-start; + justify-content: center; + padding-top: 15vh; +} + +.paste-dialog { + background: var(--bg-secondary, #1e1e2e); + border: 1px solid var(--border-color, #444); + border-radius: 12px; + padding: 12px; + width: calc(100% - 24px); + max-width: 400px; +} + +.paste-textarea { + width: 100%; + min-height: 80px; + max-height: 200px; + background: var(--bg-primary, #0d0d14); + color: var(--text-primary, #e0e0e0); + border: 1px solid var(--border-color, #444); + border-radius: 8px; + padding: 8px; + font-family: inherit; + font-size: 16px; + resize: none; + box-sizing: border-box; +} + +.paste-textarea:focus { + outline: none; + border-color: var(--accent-color, #7aa2f7); +} + +.paste-actions { + display: flex; + justify-content: flex-end; + gap: 8px; + margin-top: 10px; +} + +.paste-cancel, .paste-new, .paste-send, .paste-image { + padding: 8px 18px; + border: none; + border-radius: 8px; + font-size: 14px; + cursor: pointer; +} + +.paste-image { + margin-right: auto; + background: var(--bg-tertiary, #333); + color: var(--accent-color, #7aa2f7); + border: 1px solid var(--accent-color, #7aa2f7); +} + +.paste-cancel { + background: var(--bg-tertiary, #333); + color: var(--text-secondary, #aaa); +} + +.paste-new { + background: var(--bg-tertiary, #333); + color: var(--accent-color, #7aa2f7); + border: 1px solid var(--accent-color, #7aa2f7); +} + +.paste-send { + background: var(--accent-color, #7aa2f7); + color: #fff; + font-weight: 600; +} + /* ============================================================================ iOS Safari Specific Fixes ============================================================================ */