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 an empty last line's caret on that line
Gloow_eb7508b199f9
diff --git a/apps/desktop/src/features/chat/composer/editor.ts b/apps/desktop/src/features/chat/composer/editor.tsindex 7ecf55b25..f072890f1 100644--- a/apps/desktop/src/features/chat/composer/editor.ts+++ b/apps/desktop/src/features/chat/composer/editor.ts@@ -97,10 +97,34 @@ function isChipElement(node: Node): boolean {);}+const TAIL_PLACEHOLDER_ATTRIBUTE = "data-composer-tail";++/** The break that keeps an empty last line laid out; the draft never reads it. */+function buildTailPlaceholder(): HTMLBRElement {+ const placeholder = document.createElement("br");+ placeholder.setAttribute(TAIL_PLACEHOLDER_ATTRIBUTE, "");+ placeholder.setAttribute("aria-hidden", "true");+ return placeholder;+}++/** Paint the placeholder for a value whose last line is empty. */+function paintTailPlaceholder(el: HTMLElement, value: string): void {+ if (value.endsWith("\n")) el.appendChild(buildTailPlaceholder());+}++function isTailPlaceholder(node: Node): boolean {+ return (+ node.nodeType === Node.ELEMENT_NODE &&+ (node as HTMLElement).tagName === "BR" &&+ (node as HTMLElement).hasAttribute(TAIL_PLACEHOLDER_ATTRIBUTE)+ );+}+/** Rendered length of a node in draft-string characters (chip = 1 char). */function editorNodeLength(node: Node): number {if (node.nodeType === Node.TEXT_NODE) return (node.nodeValue ?? "").length;if (isChipElement(node)) return 1;+ if (isTailPlaceholder(node)) return 0;if (node.nodeType === Node.ELEMENT_NODE) {const element = node as HTMLElement;if (element.tagName === "BR") return 1;@@ -126,6 +150,7 @@ export function readEditorValue(el: HTMLElement): string {out += isChipTokenChar(token) ? token : "";return;}+ if (isTailPlaceholder(element)) return;if (element.tagName === "BR") {out += "\n";return;@@ -162,6 +187,8 @@ export function editorIndexAt(for (const child of Array.from(parent.childNodes)) {if (child.nodeType === Node.TEXT_NODE) {total += (child.nodeValue ?? "").length;+ } else if (isTailPlaceholder(child)) {+ // The placeholder break is not part of the draft.} else if (isChipElement(child) || (child as HTMLElement).tagName === "BR") {total += 1;} else if (child.nodeType === Node.ELEMENT_NODE) {@@ -208,6 +235,9 @@ function editorCaretPoint(el: HTMLElement, target: number): { node: Node; offsetreturn true;}remaining -= length;+ } else if (isTailPlaceholder(child)) {+ // Not a draft position: a caret at the end goes before the placeholder.+ continue;} else if (isChipElement(child) || (child as HTMLElement).tagName === "BR") {if (point === null && remaining <= 1) {point = { node: parent, offset: remaining === 0 ? index : index + 1 };@@ -221,7 +251,9 @@ function editorCaretPoint(el: HTMLElement, target: number): { node: Node; offsetreturn false;};descend(el);- return point ?? { node: el, offset: el.childNodes.length };+ if (point) return point;+ const placeholder = Array.from(el.childNodes).findIndex(isTailPlaceholder);+ return { node: el, offset: placeholder === -1 ? el.childNodes.length : placeholder };}export function setEditorCaret(el: HTMLElement, index: number): void {@@ -239,6 +271,29 @@ export function setEditorCaret(el: HTMLElement, index: number): void {selection.addRange(range);}+/**+ * Keep the empty last line laid out. Chromium has no box for a line that holds+ * nothing, so a caret left after the final break resolves backwards and the next+ * keystroke lands on the line above (#2433). A placeholder break gives that line+ * a box; `readEditorValue`, `editorIndexAt` and `editorCaretPoint` all skip it,+ * so the draft string and its indices are unchanged.+ */+export function syncTailPlaceholder(el: HTMLElement): void {+ const value = readEditorValue(el);+ const existing = Array.from(el.childNodes).find(isTailPlaceholder) ?? null;+ if (!value.endsWith("\n")) {+ existing?.remove();+ return;+ }+ if (!existing) el.appendChild(buildTailPlaceholder());+ // A caret after the final break is the position without a box; naming the end+ // by draft index puts it on the placeholder side instead.+ const selection = window.getSelection();+ if (!selection || selection.rangeCount === 0 || !selection.isCollapsed) return;+ if (!el.contains(selection.anchorNode)) return;+ if (editorSelectionRange(el).start === value.length) setEditorCaret(el, value.length);+}+/** One glyph per file family on attachment chips, as inline SVG strings. */const CHIP_ICON_SVG: Record<string, string> = {dir: '<path d="M3 7V5a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2Z"/><path d="M3 9h18"/>',@@ -438,6 +493,7 @@ export function paintEditorValue(// Chromium needs at least one node for reliable caret placement.el.appendChild(document.createTextNode(""));}+ paintTailPlaceholder(el, value);}export function clipboardFiles(data: DataTransfer): File[] {diff --git a/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.ts b/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.tsindex a6cd31b64..43edc1264 100644--- a/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.ts+++ b/apps/desktop/src/features/chat/composer/hooks/useComposerDraft.ts@@ -42,6 +42,7 @@ import {paintEditorValue,readEditorValue,setEditorCaret,+ syncTailPlaceholder,type ComposerFileReference,} from "../editor";import type { ComposerPrefill } from "../model";@@ -305,6 +306,13 @@ export function useComposerDraft({editorValueRef.current = nextValue;};+ // An empty last line has no box of its own, so Chromium types the next+ // keystroke one line up; give it a placeholder break after every commit.+ useLayoutEffect(() => {+ const element = ref.current;+ if (element) syncTailPlaceholder(element);+ }, [value]);+useLayoutEffect(() => {const element = ref.current;if (!element) return;@@ -406,14 +414,11 @@ export function useComposerDraft({if (!selection || selection.rangeCount === 0) return;const range = selection.getRangeAt(0);if (!element.contains(range.startContainer) || !element.contains(range.endContainer)) return;+ const { start } = editorSelectionRange(element);range.deleteContents();const node = document.createTextNode("\n");range.insertNode(node);- const after = document.createRange();- after.setStart(node, 1);- after.collapse(true);- selection.removeAllRanges();- selection.addRange(after);+ setEditorCaret(element, start + 1);commitEditorDom();};diff --git a/apps/desktop/test/composer-clipboard.test.mjs b/apps/desktop/test/composer-clipboard.test.mjsindex ab567c200..e0efadca0 100644--- a/apps/desktop/test/composer-clipboard.test.mjs+++ b/apps/desktop/test/composer-clipboard.test.mjs@@ -104,6 +104,13 @@ test("paintEditorValue preserves line breaks across DOM rendering", async () =>tagName: tagName.toUpperCase(),classList: { contains: () => false },childNodes: [],+ attributes: {},+ setAttribute(name, value) {+ this.attributes[name] = value;+ },+ hasAttribute(name) {+ return name in this.attributes;+ },};},};diff --git a/apps/desktop/test/composer-tail-line.test.mjs b/apps/desktop/test/composer-tail-line.test.mjsnew file mode 100644index 000000000..b6c6f28e3--- /dev/null+++ b/apps/desktop/test/composer-tail-line.test.mjs@@ -0,0 +1,75 @@+/**+ * #2433: the empty last line has no box of its own, so Chromium types the next+ * keystroke one line up. The editor keeps a placeholder break on that line and+ * every read path has to ignore it.+ */+import assert from "node:assert/strict";+import { readFileSync } from "node:fs";+import test from "node:test";+import ts from "typescript";+import vm from "node:vm";+import { readComposerModule } from "./helpers/source-contracts.mjs";++function loadFunction(path, name, globals = {}) {+ const source = readFileSync(new URL(path, import.meta.url), "utf8");+ const tree = ts.createSourceFile("module.ts", source, ts.ScriptTarget.Latest, true);+ const node = tree.statements.find(+ (statement) => ts.isFunctionDeclaration(statement) && statement.name?.text === name,+ );+ assert.ok(node, name);+ const code = ts.transpileModule(node.getText(tree).replace(/^export\s+/, ""), {+ compilerOptions: { target: ts.ScriptTarget.ES2022 },+ }).outputText;+ return vm.runInNewContext(`${code}\n${name};`, globals);+}++const nodeConstants = { TEXT_NODE: 3, ELEMENT_NODE: 1 };+const isTailPlaceholder = loadFunction("../src/features/chat/composer/editor.ts", "isTailPlaceholder", {+ Node: nodeConstants,+ TAIL_PLACEHOLDER_ATTRIBUTE: "data-composer-tail",+});+const editorNodeLength = loadFunction("../src/features/chat/composer/editor.ts", "editorNodeLength", {+ Node: nodeConstants,+ isChipElement: () => false,+ isTailPlaceholder: (node) => isTailPlaceholder(node),+});++const placeholder = {+ nodeType: 1,+ tagName: "BR",+ hasAttribute: (name) => name === "data-composer-tail",+};+const realBreak = { nodeType: 1, tagName: "BR", hasAttribute: () => false };+const text = (value) => ({ nodeType: 3, nodeValue: value, childNodes: [] });++test("only the marked break is a tail placeholder, and it has no length", () => {+ assert.equal(isTailPlaceholder(placeholder), true);+ assert.equal(isTailPlaceholder(realBreak), false);+ assert.equal(isTailPlaceholder(text("\n")), false);+ assert.equal(editorNodeLength(placeholder), 0);+ assert.equal(editorNodeLength(realBreak), 1);+ assert.equal(editorNodeLength(text("ab")), 2);+});++test("the editor model places, reads and paints around the tail placeholder", async () => {+ const editor = await readComposerModule("editor.ts");+ const draft = await readComposerModule("hooks/useComposerDraft.ts");+ const deletion = await readComposerModule("native-deletion.ts");++ // Reading and indexing skip it, so the draft string never grows a newline.+ assert.match(editor, /if \(isTailPlaceholder\(element\)\) return;/);+ assert.match(editor, /} else if \(isTailPlaceholder\(child\)\) \{/);++ // A caret placed by draft index lands before it, never on it.+ assert.match(editor, /if \(isTailPlaceholder\(child\)\) \{\n\s+\/\/ Not a draft position/);+ assert.match(editor, /const placeholder = Array\.from\(el\.childNodes\)\.findIndex\(isTailPlaceholder\);/);++ // Painting gives an empty last line its box, and every commit keeps it.+ assert.match(editor, /paintTailPlaceholder\(el, value\);/);+ assert.match(editor, /if \(value\.endsWith\("\\n"\)\) el\.appendChild\(buildTailPlaceholder\(\)\);/);+ assert.match(draft, /useLayoutEffect\(\(\) => \{\n\s+const element = ref\.current;\n\s+if \(element\) syncTailPlaceholder\(element\);/);++ // The invariant is restored after the event, never inside it: mutating the+ // editor while Chromium runs an editing command makes it insert twice.+ assert.doesNotMatch(deletion, /syncTailPlaceholder/);+});diff --git a/docs/spec/04-ux/08-component-spec.md b/docs/spec/04-ux/08-component-spec.mdindex bdb1e2f18..eeab8b6d6 100644--- a/docs/spec/04-ux/08-component-spec.md+++ b/docs/spec/04-ux/08-component-spec.md@@ -4767,6 +4767,14 @@ as well as its DOM, so submission and draft caching retain the path. Deletedreference history is local to the current draft/workspace and is cleared on send;pasting a private-use character alone must not restore an attachment. The input owns and disposes the native event listeners.+An empty last line keeps its own caret. Chromium has no box for a line that holds+nothing, so a caret left after the final line break types the next keystroke onto+the line above. The editor paints a placeholder break on that line, places an+end-of-draft caret before it when the draft ends in a newline, and restores it+after every commit; the draft string, its indices and the caret's position in it+are unchanged, and the placeholder disappears as soon as the line holds text. A+middle empty line needs none: the break that ends it already gives it a box.+### Dialog long-text containmentExtension prompts keep the 420px rename-dialog width. Their heading columndiff --git a/docs/spec/06-delivery/e2e/conversation.md b/docs/spec/06-delivery/e2e/conversation.mdindex 6bf75649c..9f0d0c203 100644--- a/docs/spec/06-delivery/e2e/conversation.md+++ b/docs/spec/06-delivery/e2e/conversation.md@@ -367,10 +367,12 @@ folder/mixed-selection behavior. No system clipboard contents are changed.- **Preconditions**: Provider configured; a session is open.- **Steps**: 1) Leave Enter-to-send enabled. Type a draft and press Enter.2) Disable Enter-to-send in Settings. 3) Type a second draft and press- Cmd/Ctrl+Enter. 4) Press Enter and Shift+Enter in a third draft.+ Cmd/Ctrl+Enter. 4) Press Enter and Shift+Enter in a third draft, then+ Shift+Enter at the end and type one character.- **Expected**: Step 1 sends. After the setting is off, Cmd/Ctrl+Enter sends;plain Enter and Shift+Enter insert newlines and do not send. IME composition- and an open autocomplete menu still take precedence over send.+ and an open autocomplete menu still take precedence over send. The character+ typed on the empty last line stays on that line.- **Specs linked**: `04-ux/01-ui-ia.md`, `04-ux/08-component-spec.md` §11.5,`04-ux/09-interaction-patterns.md` §1.2- **Acceptance**: C (composer send)