Skip to content

The agent that grows with you

fix(desktop): preserve composer caret during normalization

Ahmett10179e8aab91c10
diff --git a/apps/desktop/src/app/chat/composer/rich-editor.test.ts b/apps/desktop/src/app/chat/composer/rich-editor.test.ts
index e9b70501af3..b4e8f381640 100644
--- a/apps/desktop/src/app/chat/composer/rich-editor.test.ts
+++ b/apps/desktop/src/app/chat/composer/rich-editor.test.ts
@@ -147,6 +147,38 @@ describe('normalizeComposerEditorDom', () => {
expect(composerPlainText(editor)).toBe('@file:`src/foo.ts`')
expect(editor.querySelector('br')).toBeNull()
})
+
+ it('preserves a live caret anchored in an empty direct text node', () => {
+ const editor = document.createElement('div')
+ editor.dataset.slot = RICH_INPUT_SLOT
+ editor.contentEditable = 'true'
+ document.body.append(editor)
+
+ const leadingLitter = document.createTextNode('')
+ const caretContainer = document.createTextNode('')
+ const trailingLitter = document.createTextNode('')
+ editor.append(leadingLitter, refChipElement('file', '`src/foo.ts`'), caretContainer, trailingLitter)
+
+ const caret = document.createRange()
+ caret.setStart(caretContainer, 0)
+ caret.collapse(true)
+
+ const selection = window.getSelection()!
+ selection.removeAllRanges()
+ selection.addRange(caret)
+
+ normalizeComposerEditorDom(editor)
+
+ expect(leadingLitter.isConnected).toBe(false)
+ expect(trailingLitter.isConnected).toBe(false)
+ expect(caretContainer.isConnected).toBe(true)
+ expect(editor.contains(selection.getRangeAt(0).startContainer)).toBe(true)
+ expect(selection.getRangeAt(0).startContainer).toBe(caretContainer)
+ expect(composerPlainText(editor)).toBe('@file:`src/foo.ts`')
+
+ selection.removeAllRanges()
+ editor.remove()
+ })
})
describe('insertInlineRefsIntoEditor', () => {
diff --git a/apps/desktop/src/app/chat/composer/rich-editor.ts b/apps/desktop/src/app/chat/composer/rich-editor.ts
index 9258da250de..bd6036db5b7 100644
--- a/apps/desktop/src/app/chat/composer/rich-editor.ts
+++ b/apps/desktop/src/app/chat/composer/rich-editor.ts
@@ -50,6 +50,19 @@ function meaningfulNextSibling(node: ChildNode | null): ChildNode | null {
return next
}
+/** The live collapsed selection container, if it belongs to this editor. */
+function composerCollapsedSelectionContainer(editor: HTMLElement): Node | null {
+ const selection = window.getSelection()
+
+ if (!selection?.isCollapsed || selection.rangeCount === 0) {
+ return null
+ }
+
+ const range = selection.getRangeAt(0)
+
+ return editor.contains(range.startContainer) ? range.startContainer : null
+}
+
/** Keep the `data-empty` marker the placeholder paints on in step with the
* editor root's contents.
*
@@ -724,10 +737,12 @@ function isBlankNode(node: ChildNode | null): boolean {
* rendering emits (we use text nodes + <br> + chips). Real <br> line breaks
* (Shift+Enter, which sit after actual text) are preserved. */
export function normalizeComposerEditorDom(editor: HTMLElement) {
+ const selectedContainer = composerCollapsedSelectionContainer(editor)
+
// Chromium's zero-length text nodes first: every check below reads siblings,
// and litter between them makes a chip look like it has text either side.
for (const child of Array.from(editor.childNodes)) {
- if (isEmptyTextNode(child)) {
+ if (isEmptyTextNode(child) && child !== selectedContainer) {
child.remove()
}
}