Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(composer): keep the end-of-draft break in the native undo stack
Gloow_3bffe922a98f
diff --git a/apps/desktop/src/features/chat/composer/editor.ts b/apps/desktop/src/features/chat/composer/editor.tsindex 6635236ad..4195642dd 100644--- a/apps/desktop/src/features/chat/composer/editor.ts+++ b/apps/desktop/src/features/chat/composer/editor.ts@@ -130,6 +130,15 @@ function paintTailPlaceholder(el: HTMLElement, value: string): void {if (value.endsWith("\n")) el.appendChild(createTailPlaceholder());}+/**+ * One native edit for an end-of-draft break: the real break plus the layout+ * placeholder. Chromium records it as a single undo step, so undo and redo take+ * the new line and its caret together.+ */+export function tailBreakFragment(): string {+ return `<br><br ${TAIL_PLACEHOLDER}>`;+}+/** Rendered length of a node in draft-string characters (chip = 1 char). */function editorNodeLength(node: Node): number {if (isTailPlaceholder(node)) return 0;diff --git a/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.ts b/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.tsindex 38f0c3160..8d70b018e 100644--- a/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.ts+++ b/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.ts@@ -43,6 +43,7 @@ import {readEditorValue,setEditorCaret,syncTailPlaceholder,+ tailBreakFragment,type ComposerFileReference,} from "../editor";import type { ComposerPrefill } from "../model";@@ -407,7 +408,11 @@ export function useComposerDraft({updateCursor(start);};- /** Enter inserts exactly one newline; Chromium would add a second one. */+ /**+ * End-of-draft break. One native edit adds the real break together with the+ * layout placeholder, so undo and redo take the new line and its caret at once;+ * Chromium's own break would add a second, layout-only newline here.+ */const insertNewlineInEditor = () => {const element = ref.current;if (!element) return;@@ -416,9 +421,13 @@ export function useComposerDraft({const range = selection.getRangeAt(0);if (!element.contains(range.startContainer) || !element.contains(range.endContainer)) return;const { start } = editorSelectionRange(element);- // Chromium's own `insertLineBreak` also adds a placeholder newline after the- // caret, which reaches the draft as one empty line too many (#2555), so the- // break is inserted here as the single newline the draft model stores.+ if (document.execCommand("insertHTML", false, tailBreakFragment())) {+ commitEditorDom();+ // The edit leaves the caret after the placeholder; the draft end is before+ // it, on the empty line the placeholder lays out.+ setEditorCaret(element, readEditorValue(element).length);+ return;+ }range.deleteContents();range.insertNode(document.createTextNode("\n"));setEditorCaret(element, start + 1);diff --git a/apps/desktop/src/features/chat/composer/native-deletion.ts b/apps/desktop/src/features/chat/composer/native-deletion.tsindex f30fd7534..5273391ee 100644--- a/apps/desktop/src/features/chat/composer/native-deletion.ts+++ b/apps/desktop/src/features/chat/composer/native-deletion.ts@@ -4,9 +4,10 @@ type DeletionSnapshot = { expected: string; breaks: Set<HTMLBRElement> };/*** Preserve native deletion/undo; remove only a proven extra Chromium placeholder.- * With `onInsertLineBreak` the editor also owns line breaks: Chromium inserts the- * break *and* a placeholder newline after the caret, so one Enter would reach the- * draft as one empty line too many.+ * With `onInsertLineBreak` the editor also owns the end-of-draft break: Chromium+ * adds a second, layout-only newline after the caret there, which reaches the+ * draft as one empty line too many. A break anywhere else is exactly one newline+ * natively and stays in the undo stack, so it is left alone.*/export function installComposerDeletionGuard(editor: HTMLElement,@@ -16,12 +17,14 @@ export function installComposerDeletionGuard(// spurious, never a string pattern that could also be an intentional newline.const placeholders = new WeakSet<HTMLBRElement>();let snapshot: DeletionSnapshot | null = null;+ const atDraftEnd = () => editorSelectionRange(editor).end === readEditorValue(editor).length;const beforeInput = (event: InputEvent) => {snapshot = null;if (event.isComposing) return;if (onInsertLineBreak &&- (event.inputType === "insertLineBreak" || event.inputType === "insertParagraph")+ (event.inputType === "insertLineBreak" || event.inputType === "insertParagraph") &&+ atDraftEnd()) {event.preventDefault();onInsertLineBreak();diff --git a/apps/desktop/test/composer-native-deletion.test.mjs b/apps/desktop/test/composer-native-deletion.test.mjsindex ba6b9ba1c..c1f05b2e5 100644--- a/apps/desktop/test/composer-native-deletion.test.mjs+++ b/apps/desktop/test/composer-native-deletion.test.mjs@@ -18,11 +18,17 @@ test("composer input owns the native deletion guard", () => {assert.doesNotMatch(deletionSource, /trim\(\)/);});-test("one Enter is one newline, owned by the editor", () => {- // Chromium inserts the break plus a placeholder newline after the caret, so a- // draft would gain an empty line the user did not ask for (#2555).+test("one Enter is one newline, owned by the editor at the draft end only", () => {+ // Chromium adds a second, layout-only newline when the caret ends the block, so+ // a draft would gain an empty line the user did not ask for (#2555).assert.match(deletionSource, /event\.inputType === "insertLineBreak" \|\| event\.inputType === "insertParagraph"/);+ assert.match(deletionSource, /atDraftEnd\(\)/);+ assert.match(deletionSource, /editorSelectionRange\(editor\)\.end === readEditorValue\(editor\)\.length/);assert.match(deletionSource, /event\.preventDefault\(\);\n\s+onInsertLineBreak\(\);/);+ // A break anywhere else is exactly one newline natively and stays in the undo+ // stack, so the guard must not take it over.+ assert.match(deletionSource, /A break anywhere else is exactly one newline/);// React never dispatches the synthetic beforeinput event, so the guard owns it.assert.doesNotMatch(inputSource, /onBeforeInput/);});+diff --git a/apps/desktop/test/composer-tail-line.test.mjs b/apps/desktop/test/composer-tail-line.test.mjsindex ba3cb6e30..0fedfa829 100644--- a/apps/desktop/test/composer-tail-line.test.mjs+++ b/apps/desktop/test/composer-tail-line.test.mjs@@ -54,3 +54,15 @@ test("Chromium does not get to add a second newline for one Enter", () => {assert.match(deletion, /event\.inputType === "insertLineBreak"/);assert.doesNotMatch(deletion, /syncTailPlaceholder/);});++test("the end-of-draft break is one native edit, so undo takes both nodes", () => {+ assert.match(editor, /export function tailBreakFragment\(\): string \{/);+ assert.match(editor, /return `<br><br \$\{TAIL_PLACEHOLDER\}>`;/);+ // One edit inserts the real break and the placeholder, so Chromium records a+ // single undo step instead of a bare DOM mutation (#2588 follow-up).+ assert.match(draft, /document\.execCommand\("insertHTML", false, tailBreakFragment\(\)\)/);+ // The native edit is the primary path; the DOM fallback only covers a browser+ // that refuses insertHTML.+ assert.ok(draft.indexOf("execCommand") < draft.indexOf("range.insertNode"));+});+diff --git a/docs/spec/04-ux/08-component-spec.md b/docs/spec/04-ux/08-component-spec.mdindex 337c06cc3..c85039ce4 100644--- a/docs/spec/04-ux/08-component-spec.md+++ b/docs/spec/04-ux/08-component-spec.md@@ -4820,12 +4820,12 @@ index and caret path counts it as zero characters. The placeholder carries notext, so it cannot be selected, copied or typed, never merges into a neighbouringtext node, and the draft string and its indices are unchanged; the end-of-draftcaret sits before the placeholder, on the empty line, and the placeholder is-removed as soon as the line holds text. The editor also owns the break itself:-Chromium inserts the break plus a placeholder newline after the caret, which-reached the draft as one empty line too many, so a native `insertLineBreak` or-`insertParagraph` is prevented and the editor inserts a single newline. A middle-empty line keeps its existing behavior; the placeholder only covers the line that-ends the draft.+removed as soon as the line holds text. The editor owns only the end-of-draft+break: Chromium would add the break plus a placeholder newline there, so a native+`insertLineBreak` or `insertParagraph` at the draft end is prevented and one+native edit inserts the real break together with the placeholder, which keeps undo+and redo covering both in a single step. A break anywhere else stays native, and a+middle empty line keeps its existing behavior.### Dialog long-text containmentdiff --git a/scripts/e2e-composer-tail-line.mjs b/scripts/e2e-composer-tail-line.mjsindex 9d8a0bb4b..00bbfff95 100644--- a/scripts/e2e-composer-tail-line.mjs+++ b/scripts/e2e-composer-tail-line.mjs@@ -64,7 +64,9 @@ const { app, BrowserWindow } = require("electron");const path = require("node:path");app.setPath("userData", path.join(__dirname, "profile"));const SHIFT = 8;-const VK = { Enter: 13, Backspace: 8 };+const CTRL = 2;+const VK = { Enter: 13, Backspace: 8, z: 90, y: 89 };+const CODE = { z: "KeyZ", y: "KeyY" };app.whenReady().then(async () => {const window = new BrowserWindow({show: false, width: 760, height: 520,@@ -90,13 +92,13 @@ app.whenReady().then(async () => {const text = key === "Enter" ? "\\r" : undefined;await input("dispatchKeyEvent", {type: text ? "keyDown" : "rawKeyDown",- key, code: key, modifiers,+ key, code: CODE[key] ?? key, modifiers,windowsVirtualKeyCode: VK[key] ?? 0,nativeVirtualKeyCode: VK[key] ?? 0,...(text ? { text, unmodifiedText: text } : {}),});await input("dispatchKeyEvent", {- type: "keyUp", key, code: key, modifiers,+ type: "keyUp", key, code: CODE[key] ?? key, modifiers,windowsVirtualKeyCode: VK[key] ?? 0,nativeVirtualKeyCode: VK[key] ?? 0,});@@ -146,25 +148,69 @@ app.whenReady().then(async () => {const beyondLast = await clickLine(5);expect(beyondLast.clickLine !== null && beyondLast.clickLine === 18, "there is no line below the last one", beyondLast);- await js("globalThis.composerTailCaret(" + JSON.stringify("1test\\n\\n2test\\n3test".length) + ")");+ const NL = String.fromCharCode(10);+ const base = ["1test", "", "2test", "3test"].join(NL);+ await js("globalThis.composerTailCaret(" + JSON.stringify(base.length) + ")");await type("x");const typedAtEnd = await state();- expect(typedAtEnd.value === "1test\\n\\n2test\\n3testx", "typing at the end must append there", typedAtEnd);+ expect(typedAtEnd.value === base + "x", "typing at the end must append there", typedAtEnd);expect(typedAtEnd.caret === typedAtEnd.value.length, "typing at the end must keep the caret at the end", typedAtEnd);await press("Backspace");const deletedAtEnd = await state();- expect(deletedAtEnd.value === "1test\\n\\n2test\\n3test", "Backspace at the end must drop the last character", deletedAtEnd);+ expect(deletedAtEnd.value === base, "Backspace at the end must drop the last character", deletedAtEnd);expect(deletedAtEnd.caret === deletedAtEnd.value.length, "Backspace must keep the caret at the end", deletedAtEnd);+ const baseLines = deletedAtEnd.lineTops.length;+ // The end-of-draft break is one native edit: undo and redo take the new line+ // and its layout placeholder together (#2588 follow-up).await press("Enter", SHIFT);const tailBreak = await state();- expect(tailBreak.value === "1test\\n\\n2test\\n3test\\n", "a trailing break must add one line", tailBreak);+ expect(tailBreak.value === base + NL, "a trailing break must add one line", tailBreak);expect(tailBreak.invisibleChars.length === 0, "an empty last line must hold no character", tailBreak);+ expect(tailBreak.caret === tailBreak.value.length, "the trailing break must leave the caret at the end", tailBreak);+ expect(tailBreak.lineTops.length === baseLines + 1, "the trailing break must paint one more line", tailBreak);++ await press("z", CTRL);+ const undoneBreak = await state();+ expect(undoneBreak.value === base, "undo must remove the trailing break", undoneBreak);+ expect(undoneBreak.caret === base.length, "undo must keep the caret at the end", undoneBreak);+ expect(undoneBreak.lineTops.length === baseLines, "undo must drop the extra line", undoneBreak);++ await press("y", CTRL);+ const redoneBreak = await state();+ expect(redoneBreak.value === base + NL, "redo must restore the trailing break", redoneBreak);+ expect(redoneBreak.lineTops.length === baseLines + 1, "redo must restore the extra line", redoneBreak);await type("z");- const typedOnEmptyLine = await state();- expect(typedOnEmptyLine.value === "1test\\n\\n2test\\n3test\\nz", "the keystroke must land on the empty line", typedOnEmptyLine);- expect(typedOnEmptyLine.lineTops.length === 5, "the empty last line must keep its box", typedOnEmptyLine);+ const typedAfterRedo = await state();+ expect(typedAfterRedo.value === base + NL + "z", "typing after redo must land on the empty line", typedAfterRedo);++ // Consecutive end-of-draft breaks each add exactly one line.+ await press("Enter", SHIFT);+ await press("Enter", SHIFT);+ await press("Enter", SHIFT);+ const consecutive = await state();+ expect(+ consecutive.value === base + NL + "z" + NL + NL + NL,+ "each trailing break must add one line",+ consecutive,+ );+ expect(consecutive.invisibleChars.length === 0, "consecutive empty lines must hold no character", consecutive);+ expect(consecutive.lineTops.length === baseLines + 4, "consecutive breaks must paint one line each", consecutive);++ // A break in the middle stays native: one newline, and it is undoable.+ await js("globalThis.composerTailCaret(5)");+ await press("Enter", SHIFT);+ const middleBreak = await state();+ expect(+ middleBreak.value === ["1test", "", "", "2test", "3test", "z", "", "", ""].join(NL),+ "a middle break must add exactly one line",+ middleBreak,+ );+ await press("z", CTRL);+ const undoneMiddle = await state();+ expect(undoneMiddle.value === consecutive.value, "undo must remove the middle break", undoneMiddle);+ const typedOnEmptyLine = undoneMiddle;if (failures.length > 0) {console.error("COMPOSER_TAIL_LINE " + JSON.stringify({ ok: false, failures }));