diff --git a/docs/architecture-invariants.md b/docs/architecture-invariants.md index e17a089b..a02ae950 100644 --- a/docs/architecture-invariants.md +++ b/docs/architecture-invariants.md @@ -1269,7 +1269,7 @@ The age stamps are refreshed **in place** by a 20s clock (`_tickHomeSessionsTime ⚠️ It must disarm on ALL of: use, second tap, any other accessory key, session switch, keyboard dismissal, and a layout swap; a modifier left armed turns the next innocent keystroke into a control byte. -⚠️ **onData is not the only input path** — with `cjkInputEnabled` on, the CJK textarea owns the keyboard (onData returns early for everything it swallows, and the focus router sends `terminal.focus()` there, which is where the bar refocuses after every key), so `_handleCjkInput()` applies the modifier too. It is that module's single choke point to the PTY, so one call covers typed characters, IME flushes, Enter, backspace and arrows. Without it an armed modifier could neither fire NOR be spent, and survived to a later keystroke. +⚠️ **onData is not the only input path** — with `cjkInputEnabled` on, the CJK textarea owns the keyboard (onData returns early for everything it swallows, and the focus router sends `terminal.focus()` there, which is where the bar refocuses after every key), so `_handleCjkInput()` applies the modifier too. It is that module's single choke point to the PTY in live mode, so one call covers typed characters, IME flushes, Enter, backspace and arrows; in Send on Enter a held answer leaves through `_sendCjkPrompt()` instead, which spends the modifier, while an armed next character still goes through `_handleCjkInput()`. Without it an armed modifier could neither fire NOR be spent, and survived to a later keystroke. Mapping is `ctrlByteFor()` (`code & 0x1f` over @A-Z[\]^_ and a-z, plus Ctrl+Space=NUL / Ctrl+?=DEL); characters with no control equivalent pass through unchanged, like a hardware keyboard. diff --git a/src/web/public/input-cjk.js b/src/web/public/input-cjk.js index ebbe8d0b..fd4c0c31 100644 --- a/src/web/public/input-cjk.js +++ b/src/web/public/input-cjk.js @@ -672,9 +672,18 @@ const CjkInput = (() => { // unchanged, leave the field alone, since rewriting its attributes while // it has focus can disturb a composition in progress. if (_holdApplied && _textarea && _hold === !!on) return; + const wasHolding = _hold; _hold = !!on; if (!_textarea) return; _holdApplied = true; + // Turning the mode off: live mode's next flush would send everything in + // the field, a held multi-line draft included (its newline runs the first + // line in a shell), and stashed drafts would come back if the mode is + // switched on again. Drop them all. + if (wasHolding && !_hold) { + _drafts.clear(); + _resetToPhantom(); + } _heldBase = _strip(_textarea.value); const v = _hold ? 'on' : 'off'; _textarea.setAttribute('autocorrect', v); diff --git a/src/web/public/terminal-ui.js b/src/web/public/terminal-ui.js index 577f8102..eeda8402 100644 --- a/src/web/public/terminal-ui.js +++ b/src/web/public/terminal-ui.js @@ -4555,8 +4555,10 @@ Object.assign(CodemanApp.prototype, { // after every key. So the onData hook never sees these keystrokes, and an // armed modifier could neither fire NOR be spent: it survived until a // session switch and then turned an innocent keystroke into a control byte. - // This is the module's single choke point to the PTY, so applying it here - // covers typed characters, IME flushes, Enter, backspace and arrows at once. + // This is the module's single choke point to the PTY in live mode, so applying + // it here covers typed characters, IME flushes, Enter, backspace and arrows at + // once. In Send on Enter a held answer leaves through _sendCjkPrompt instead, + // which spends the modifier; an armed next character still comes here. // Same policy as the onData hook: the next single character is modified, // anything longer merely spends the modifier. if (typeof KeyboardAccessoryBar !== 'undefined' && KeyboardAccessoryBar.isCtrlArmed?.()) { diff --git a/test/input-cjk-hold.test.ts b/test/input-cjk-hold.test.ts index f4fe2c5d..b9ddc615 100644 --- a/test/input-cjk-hold.test.ts +++ b/test/input-cjk-hold.test.ts @@ -305,6 +305,22 @@ describe('CJK input: Send on Enter', () => { expect(textarea.value).toBe(PHANTOM); }); + it('turning the mode off drops the held draft and the stashes, so live mode sends only new keys', () => { + const { CjkInput, textarea, sent } = load(); + CjkInput.restoreDraft('session-a'); + type(textarea, 'rm -rf build\nsecond line'); + CjkInput.stashDraft('session-b'); + CjkInput.setHoldUntilEnter(false); + expect(textarea.value).toBe(PHANTOM); + type(textarea, 'x'); + vi.advanceTimersByTime(1000); + expect(sent).toEqual(['x']); + CjkInput.setHoldUntilEnter(true); + CjkInput.clear(); + CjkInput.restoreDraft('session-b'); + expect(textarea.value).toBe(PHANTOM); + }); + it('turns autocorrect on in the mode and back off without it', () => { const { CjkInput, textarea } = load(); expect(textarea.attrs.autocorrect).toBe('on');