Skip to content

The agent that grows with you

fix(desktop): scroll the composer caret into view after paste and voice inserts

brooklyn!b9b9476a494d
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 89cb3ba50e6..7a506f40a93 100644
--- a/apps/desktop/src/app/chat/composer/rich-editor.test.ts
+++ b/apps/desktop/src/app/chat/composer/rich-editor.test.ts
@@ -1,10 +1,11 @@
-import { afterEach, beforeEach, describe, expect, it } from 'vitest'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { rememberDesktopCommandsCatalog } from '@/lib/desktop-slash-commands'
import { insertInlineRefsIntoEditor } from './inline-refs'
import {
caretOffsetInEditor,
+ caretRevealScrollTop,
composerPlainText,
deleteSelectionInEditor,
insertComposerContentsAtCaret,
@@ -570,3 +571,69 @@ describe('normalizeComposerEditorDom — caret preservation', () => {
editor.remove()
})
})
+
+describe('caretRevealScrollTop', () => {
+ const viewport = { top: 100, bottom: 200 }
+
+ it('leaves a visible caret alone', () => {
+ expect(caretRevealScrollTop({ top: 120, bottom: 140 }, viewport, 50)).toBeNull()
+ })
+
+ it('scrolls down just far enough to show a caret below the viewport', () => {
+ expect(caretRevealScrollTop({ top: 400, bottom: 420 }, viewport, 50)).toBe(270)
+ })
+
+ it('scrolls up just far enough to show a caret above the viewport', () => {
+ expect(caretRevealScrollTop({ top: 60, bottom: 80 }, viewport, 50)).toBe(10)
+ })
+})
+
+describe('caret reveal after programmatic inserts', () => {
+ // jsdom has no layout: give the editor a 100px viewport and put every other
+ // element (the caret probe) at `caretTop`, as a long insert would.
+ function overflowingEditor(caretTop: number) {
+ const editor = document.createElement('div')
+ editor.dataset.slot = RICH_INPUT_SLOT
+ document.body.append(editor)
+
+ Object.defineProperty(editor, 'clientHeight', { configurable: true, value: 100 })
+ vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function (this: HTMLElement) {
+ const [top, bottom] = this === editor ? [0, 100] : [caretTop, caretTop + 20]
+
+ return { bottom, height: bottom - top, left: 0, right: 0, top, width: 0, x: 0, y: top, toJSON: () => ({}) }
+ })
+ vi.spyOn(window, 'requestAnimationFrame').mockImplementation(callback => {
+ callback(0)
+
+ return 0
+ })
+
+ return editor
+ }
+
+ afterEach(() => {
+ vi.restoreAllMocks()
+ document.body.replaceChildren()
+ })
+
+ it('scrolls a long paste so the caret after it is visible', () => {
+ const editor = overflowingEditor(480)
+ placeCaretAtEnd(editor)
+
+ insertComposerContentsAtCaret(editor, Array.from({ length: 30 }, (_, i) => `line ${i}`).join('\n'))
+
+ expect(editor.scrollTop).toBe(400)
+ expect(editor.querySelectorAll('span').length).toBe(0)
+ expect(composerPlainText(editor)).toContain('line 29')
+ })
+
+ it('scrolls to the caret when a repaint parks it at the end (voice transcript)', () => {
+ const editor = overflowingEditor(300)
+
+ renderComposerContents(editor, Array.from({ length: 30 }, (_, i) => `said ${i}`).join('\n'))
+ placeCaretEnd(editor)
+
+ expect(editor.scrollTop).toBe(220)
+ expect(composerPlainText(editor)).toContain('said 29')
+ })
+})
diff --git a/apps/desktop/src/app/chat/composer/rich-editor.ts b/apps/desktop/src/app/chat/composer/rich-editor.ts
index cff162d2c7c..b4f96e4259c 100644
--- a/apps/desktop/src/app/chat/composer/rich-editor.ts
+++ b/apps/desktop/src/app/chat/composer/rich-editor.ts
@@ -359,6 +359,8 @@ export function insertComposerContentsAtCaret(editor: HTMLElement, text: string,
selection?.removeAllRanges()
selection?.addRange(caret)
}
+
+ revealCaret(editor)
}
/** Range covering exactly `length` serialized characters immediately before a
@@ -606,6 +608,93 @@ export function placeCaretEnd(element: HTMLElement) {
range.collapse(false)
selection?.removeAllRanges()
selection?.addRange(range)
+ revealCaret(element)
+}
+
+/** The editor `scrollTop` that brings a caret spanning `caret.top..bottom`
+ * (client px) inside the visible band `viewport`, or null when it's already
+ * visible. Moves the nearest edge only, like `block: 'nearest'`. */
+export function caretRevealScrollTop(
+ caret: { bottom: number; top: number },
+ viewport: { bottom: number; top: number },
+ scrollTop: number
+): number | null {
+ if (caret.top < viewport.top) {
+ return scrollTop - (viewport.top - caret.top)
+ }
+
+ if (caret.bottom > viewport.bottom) {
+ return scrollTop + (caret.bottom - viewport.bottom)
+ }
+
+ return null
+}
+
+function caretClientRect(range: Range): { bottom: number; top: number } | null {
+ const end = range.cloneRange()
+ end.collapse(false)
+
+ const rects = typeof end.getClientRects === 'function' ? end.getClientRects() : null
+
+ if (rects?.length) {
+ return rects[rects.length - 1]
+ }
+
+ // Chromium gives a caret between elements (after a chip or <br>, or at the
+ // editor's end, which is where inserts leave it) no box. Measure a probe
+ // there instead. A text-node caret always has a box, so the probe never
+ // splits text.
+ if (end.startContainer.nodeType === Node.TEXT_NODE) {
+ return null
+ }
+
+ const probe = document.createElement('span')
+ probe.textContent = '\u200b'
+ end.insertNode(probe)
+
+ const rect = probe.getBoundingClientRect()
+
+ probe.remove()
+
+ return rect
+}
+
+const pendingReveals = new WeakSet<HTMLElement>()
+
+/** Scroll the editor so its caret is visible, on the next frame (one per
+ * frame across a burst of inserts). Chromium only does this itself for native
+ * edit commands; programmatic inserts (paste, voice transcripts, repaints)
+ * leave the caret wherever the old scroll position puts it (#79806). Local to
+ * the editor: `scrollIntoView` would also move the transcript. */
+export function revealCaret(editor: HTMLElement) {
+ if (pendingReveals.has(editor)) {
+ return
+ }
+
+ pendingReveals.add(editor)
+ window.requestAnimationFrame(() => {
+ pendingReveals.delete(editor)
+
+ const selection = window.getSelection()
+ const range = selection?.rangeCount ? selection.getRangeAt(0) : null
+
+ if (!editor.isConnected || !range || !editor.contains(range.endContainer)) {
+ return
+ }
+
+ const caret = caretClientRect(range)
+
+ if (!caret) {
+ return
+ }
+
+ const top = editor.getBoundingClientRect().top + editor.clientTop
+ const next = caretRevealScrollTop(caret, { bottom: top + editor.clientHeight, top }, editor.scrollTop)
+
+ if (next !== null) {
+ editor.scrollTop = next
+ }
+ })
}
/** The caret's offset in `composerPlainText` coordinates, so it can be restored