Skip to content

The agent that grows with you

fix(desktop): preserve caret on same-draft scope rekey

JoaoMarcos445beac8a5ae5e
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts
index 85265a5db74..4ed5e363088 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts
@@ -49,8 +49,10 @@ import {
} from '../focus'
import { type InlineRefInput, insertInlineRefsIntoEditor } from '../inline-refs'
import {
+ caretOffsetInEditor,
composerPlainText,
normalizeComposerEditorDom,
+ placeCaretAtOffset,
placeCaretEnd,
REF_RE,
renderComposerContents
@@ -158,21 +160,38 @@ export function useComposerDraft({
// restores run mid-focus, and the runtime sync only repaints an unfocused
// editor — so the visible text never lags the store.
const paintDraft = useCallback(
- (next: string, focus = true) => {
+ (next: string, focus = true, preserveFocusedCaret = false) => {
draftRef.current = next
setComposerText(next)
const editor = editorRef.current
if (editor) {
+ // A durable-scope rekey can repaint the SAME unsent draft while this
+ // editor remains the browser's active element and another composer
+ // temporarily owns the focus-routing bus. Preserve that native caret
+ // across the text-node replacement, but only for an unchanged draft:
+ // programmatic inserts/restores keep their existing "caret to end"
+ // semantics.
+ const caretOffset =
+ preserveFocusedCaret &&
+ visibleRef.current &&
+ document.activeElement === editor &&
+ !isElementInHiddenPane(editor) &&
+ composerPlainText(editor) === next
+ ? caretOffsetInEditor(editor)
+ : null
+
renderComposerContents(editor, next, { trailingCommitted: true })
- // Selection is document-global: a keep-alive composer in a hidden tab
- // may repaint when its background session updates, but moving its caret
- // here steals the selection from the visible composer without changing
- // document.activeElement. The foreground then still looks focused while
- // printable keydowns produce no input.
- if (visibleRef.current && getActiveComposer() === target && !isElementInHiddenPane(editor)) {
+ if (caretOffset !== null) {
+ placeCaretAtOffset(editor, Math.min(caretOffset, composerPlainText(editor).length))
+ } else if (visibleRef.current && getActiveComposer() === target && !isElementInHiddenPane(editor)) {
+ // Selection is document-global: a keep-alive composer in a hidden tab
+ // may repaint when its background session updates, but moving its caret
+ // here steals the selection from the visible composer without changing
+ // document.activeElement. The foreground then still looks focused while
+ // printable keydowns produce no input.
placeCaretEnd(editor)
}
}
@@ -331,7 +350,11 @@ export function useComposerDraft({
)
}, [inputDisabled, paintDraft, target])
- const loadIntoComposer = (text: string, attachments: ComposerAttachment[]) => {
+ const loadIntoComposer = (
+ text: string,
+ attachments: ComposerAttachment[],
+ preserveFocusedCaret = false
+ ) => {
// Diagnostic breadcrumb for #59305-class reports: identifies WHAT kind of
// state got restored into the composer (session switch, queue-edit
// restore, history browse) without logging any raw content. REF_RE has the
@@ -348,7 +371,7 @@ export function useComposerDraft({
}
attachmentScope.$attachments.set(cloneAttachments(attachments))
- paintDraft(text, false)
+ paintDraft(text, false, preserveFocusedCaret)
}
const clearDraft = useCallback(() => {
@@ -534,7 +557,7 @@ export function useComposerDraft({
draftScopeRef.current = activeQueueSessionKey
const { attachments, text } = takeSessionDraft(activeQueueSessionKey)
- loadIntoComposer(text, attachments)
+ loadIntoComposer(text, attachments, true)
return () => {
const latestText = syncDraftFromEditor()