diff --git a/docs/wiki/Input-And-Voice.md b/docs/wiki/Input-And-Voice.md index e5e97bf8..99e18a0d 100644 --- a/docs/wiki/Input-And-Voice.md +++ b/docs/wiki/Input-And-Voice.md @@ -74,10 +74,12 @@ Enter always goes out as its own write, a moment after the text, to the session typed for: a single character is typed, so CLI menus that take a digit still work, and anything longer goes like the prompt composer's Send, as one paste. The one exception is a lone digit while a permission or question dialog is waiting: the digit answers the dialog by itself, so no Enter -follows it, which would otherwise confirm whatever the CLI shows next. The toolbar's Enter button sends the -field too. With the keyboard bar's one-shot Ctrl armed, the next key you type goes out as a control -key (Ctrl, then `c` is Ctrl+C) instead of being held. With the keyboard down the field stays one -line tall. A draft stays with its session: switching tabs and coming back brings it back. +follows it, which would otherwise confirm whatever the CLI shows next. The toolbar's Enter button +sends the field too. With the keyboard bar's one-shot Ctrl armed, the next key you type goes out +as a control key (Ctrl, then `c` is Ctrl+C) instead of being held. With a hardware keyboard, Ctrl +shortcuts (undo, select all, copy) act on the field while it holds a draft and reach the CLI only +when it is empty. With the keyboard down the field stays one line tall. A draft stays with its +session: switching tabs and coming back brings it back. ## Voice dictation diff --git a/src/web/public/input-cjk.js b/src/web/public/input-cjk.js index 50beebc9..af06109c 100644 --- a/src/web/public/input-cjk.js +++ b/src/web/public/input-cjk.js @@ -73,6 +73,11 @@ const CjkInput = (() => { let _holdApplied = false; // Send-on-Enter: held drafts of the sessions switched away from, by session id. const _drafts = new Map(); + // Send-on-Enter: the session the field's text belongs to (the one last shown, + // set by restoreDraft). clear() stashes a held draft under it, so every path + // that drops the active session before the next cleanup (goHome, the tile + // grid's _replaceTileGrid and _onTileAdoptedElsewhere) keeps the answer. + let _owner = null; const _listeners = {}; const PHANTOM = '​'; @@ -480,7 +485,11 @@ const CjkInput = (() => { return; } - if (e.ctrlKey && CTRL_KEYS[e.key]) { + // Send-on-Enter with a draft in the field: Ctrl shortcuts are the + // field's (Ctrl+Z undo, Ctrl+A select all, Ctrl+C copy a selection), so + // they reach the CLI only while the field is empty, the rule Backspace + // and the arrows already follow. + if (e.ctrlKey && CTRL_KEYS[e.key] && (!_hold || _isEffectivelyEmpty())) { e.preventDefault(); _send(CTRL_KEYS[e.key]); return; @@ -615,6 +624,14 @@ const CjkInput = (() => { clear() { if (!_initialized || !_textarea) return; _t('clear (external)'); + // Send-on-Enter: a held draft is stashed for the session it was typed + // for, whichever path got here. discardDraft() drops the owner of a + // removed session, so its text is not stashed again. + if (_hold && _owner) { + const draft = _strip(_textarea.value); + if (draft.trim()) _drafts.set(_owner, draft); + } + _owner = null; _cancelDebouncedFlush(); clearTimeout(_compositionFlushTimer); _compositionFlushTimer = null; @@ -692,7 +709,11 @@ const CjkInput = (() => { /** Send-on-Enter: show the draft stashed for this session, if any, unless * the field already holds text. */ restoreDraft(sessionId) { - if (!_initialized || !_textarea || !_hold || !_drafts.has(sessionId)) return; + if (!_initialized || !_textarea) return; + // The field now belongs to this session, draft or not (see _owner), + // unless it still holds another session's text (no clear() in between). + if (!_owner || !_strip(_textarea.value)) _owner = sessionId || null; + if (!_hold || !_drafts.has(sessionId)) return; if (_strip(_textarea.value)) return; const draft = _drafts.get(sessionId); _drafts.delete(sessionId); @@ -706,8 +727,12 @@ const CjkInput = (() => { /** Forget a stashed draft when its session no longer exists. */ discardDraft(sessionId) { _drafts.delete(sessionId); + if (_owner === sessionId) _owner = null; }, + /** Diagnostic/test: the session the field's text belongs to. */ + get draftOwner() { return _owner; }, + /** Diagnostic: recent IME event trace (ring buffer). */ getTrace() { return _trace.slice(); diff --git a/src/web/public/tile-grid.js b/src/web/public/tile-grid.js index 7fd3a0c7..18ce5019 100644 --- a/src/web/public/tile-grid.js +++ b/src/web/public/tile-grid.js @@ -2657,7 +2657,10 @@ Object.assign(CodemanApp.prototype, { // the draft openTileGrid's cleanup stashed for the session it parked). if (typeof CjkInput !== 'undefined') { if (this.activeSessionId && this.activeSessionId !== sessionId) { - CjkInput.stashDraft?.(this.activeSessionId); + // A tile closed from this tab is already gone from this.sessions and + // its draft discarded: stashing it again would leave it in the map + // until the page reloads. + if (this.sessions?.has(this.activeSessionId)) CjkInput.stashDraft?.(this.activeSessionId); CjkInput.clear(); } CjkInput.restoreDraft?.(sessionId); diff --git a/test/cjk-hold-per-device-setting.test.ts b/test/cjk-hold-per-device-setting.test.ts index 9e6a8d84..1891e1a8 100644 --- a/test/cjk-hold-per-device-setting.test.ts +++ b/test/cjk-hold-per-device-setting.test.ts @@ -122,6 +122,14 @@ describe('a held draft survives a reload of the same session', () => { ); }); + it('tile focus does not stash again for a tile closed from this tab', () => { + // removeSession discards the closed session's draft first; a stash after + // that would keep it in the map until the page reloads. + expect(read('tile-grid.js')).toContain( + 'if (this.sessions?.has(this.activeSessionId)) CjkInput.stashDraft?.(this.activeSessionId);' + ); + }); + it('tile focus stashes the draft of the tile left and restores the focused one', () => { const tiles = read('tile-grid.js'); const start = tiles.indexOf(' _selectTiledSession(sessionId, options = {}) {'); diff --git a/test/input-cjk-hold.test.ts b/test/input-cjk-hold.test.ts index d2b6140e..89f6cd21 100644 --- a/test/input-cjk-hold.test.ts +++ b/test/input-cjk-hold.test.ts @@ -365,7 +365,10 @@ describe('CJK input: Send on Enter', () => { expect(textarea.value).toBe(PHANTOM); CjkInput.restoreDraft('session-a'); expect(textarea.value).toBe(PHANTOM + 'half an answer'); - // Restored once: a second visit does not bring it back again. + // Restored once: the stash is consumed, so once the user empties the + // field (Esc here) a second visit does not bring it back. A draft still in + // the field when the session is left is stashed again by clear(). + textarea.fire('keydown', { key: 'Escape', keyCode: 27 }); CjkInput.clear(); CjkInput.restoreDraft('session-a'); expect(textarea.value).toBe(PHANTOM); @@ -409,6 +412,72 @@ describe('CJK input: Send on Enter', () => { expect(textarea.value).toBe(PHANTOM + 'keep me'); }); + it('clear() stashes a held draft for the session it was typed for, with no stash call', () => { + // goHome(), _replaceTileGrid and _onTileAdoptedElsewhere drop the active + // session before the next cleanup, which then has no session to stash for. + const { CjkInput, textarea, sent } = load(); + CjkInput.restoreDraft('session-a'); + type(textarea, 'typed for a'); + CjkInput.clear(); + expect(textarea.value).toBe(PHANTOM); + CjkInput.restoreDraft('session-b'); + expect(textarea.value).toBe(PHANTOM); + CjkInput.clear(); + CjkInput.restoreDraft('session-a'); + expect(textarea.value).toBe(PHANTOM + 'typed for a'); + expect(sent).toEqual([]); + }); + + it('a removed session loses its field text: discardDraft drops the owner', () => { + const { CjkInput, textarea } = load(); + CjkInput.restoreDraft('session-a'); + type(textarea, 'for a closed tile'); + CjkInput.discardDraft('session-a'); + expect(CjkInput.draftOwner).toBe(null); + CjkInput.clear(); + CjkInput.restoreDraft('session-a'); + expect(textarea.value).toBe(PHANTOM); + }); + + it('a field still holding text keeps its owner when another session is shown', () => { + const { CjkInput, textarea } = load(); + CjkInput.restoreDraft('session-a'); + type(textarea, 'still for a'); + CjkInput.restoreDraft('session-b'); + expect(CjkInput.draftOwner).toBe('session-a'); + CjkInput.clear(); + CjkInput.restoreDraft('session-a'); + expect(textarea.value).toBe(PHANTOM + 'still for a'); + }); + + it('Ctrl shortcuts stay in the field while it holds a draft', () => { + // Ctrl+Z is undo, Ctrl+A select all, Ctrl+C copy: never a control byte to + // the CLI while there is a draft to act on. + const { textarea, sent } = load(); + type(textarea, 'draft'); + for (const key of ['z', 'a', 'c']) { + const preventDefault = vi.fn(); + textarea.fire('keydown', { key, keyCode: key.toUpperCase().charCodeAt(0), ctrlKey: true, preventDefault }); + expect(preventDefault).not.toHaveBeenCalled(); + } + expect(sent).toEqual([]); + }); + + it('Ctrl shortcuts reach the CLI from an empty field', () => { + const { textarea, sent } = load(); + const preventDefault = vi.fn(); + textarea.fire('keydown', { key: 'c', keyCode: 67, ctrlKey: true, preventDefault }); + expect(preventDefault).toHaveBeenCalled(); + expect(sent).toEqual(['\x03']); + }); + + it('live mode keeps sending Ctrl shortcuts with text in the field', () => { + const { textarea, sent } = load({ hold: false }); + textarea.value = PHANTOM + 'x'; + textarea.fire('keydown', { key: 'c', keyCode: 67, ctrlKey: true }); + expect(sent).toEqual(['\x03']); + }); + it('sends a bare Enter when the field holds only blank lines', () => { const { textarea, sent, prompts } = load(); type(textarea, '\n');