Skip to content

Commit 64148f7

Browse files
committed
fix(web): preserve selection when stepping effort
1 parent 517be31 commit 64148f7

3 files changed

Lines changed: 62 additions & 9 deletions

File tree

‎apps/web/src/components/ComposerPromptEditorTiptap.navigation.test.tsx‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { createRoot, type Root } from "react-dom/client";
77
import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
88

99
import { serializeEditorDoc } from "~/composer-rich-text-doc";
10+
import { collapseExpandedComposerCursor } from "~/composer-logic";
1011
import { ComposerPromptEditor, type ComposerPromptEditorHandle } from "./ComposerPromptEditor";
1112

1213
const draft =
@@ -28,11 +29,15 @@ afterEach(async () => {
2829
vi.unstubAllGlobals();
2930
});
3031

31-
async function mountEditor(value: string, richTextEnabled = true) {
32+
async function mountEditor(
33+
value: string,
34+
richTextEnabled = true,
35+
editorRef = createRef<ComposerPromptEditorHandle>(),
36+
) {
3237
await act(async () =>
3338
root.render(
3439
<ComposerPromptEditor
35-
editorRef={createRef<ComposerPromptEditorHandle>()}
40+
editorRef={editorRef}
3641
value={value}
3742
cursor={0}
3843
richTextEnabled={richTextEnabled}
@@ -57,6 +62,32 @@ function press(editor: Editor, key: string, options: KeyboardEventInit = {}) {
5762
return handled;
5863
}
5964

65+
it.each([
66+
{
67+
value: "Ultrathink:\nAlpha beta gamma",
68+
expected: "Ultrathink:\nAlpha X gamma",
69+
},
70+
{ value: "Alpha beta gamma", expected: "Alpha X gamma" },
71+
])(
72+
"restores the editing range after an effort prefix change to $value",
73+
async ({ value, expected }) => {
74+
const start = value.indexOf("beta");
75+
const end = start + "beta".length;
76+
const editorRef = createRef<ComposerPromptEditorHandle>();
77+
const editor = await mountEditor(value, true, editorRef);
78+
editor.view.setProps({ handleScrollToSelection: () => true });
79+
await act(async () => {
80+
editorRef.current?.focusAt(
81+
collapseExpandedComposerCursor(value, start),
82+
collapseExpandedComposerCursor(value, end),
83+
);
84+
expect(editorRef.current?.readSelectionRange()).toEqual({ start, end });
85+
editor.commands.insertContent("X");
86+
});
87+
expect(serializeEditorDoc(editor.state.doc).value).toBe(expected);
88+
},
89+
);
90+
6091
it("keeps Up before the opening bold marker in a quote, including while typing", async () => {
6192
const editor = await mountEditor(draft);
6293
const position = editor.state.selection.from;

‎apps/web/src/components/ComposerPromptEditorTiptap.tsx‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ import type { ComposerDraftContextRecords } from "./composerContextPresentation"
123123

124124
export interface ComposerPromptEditorHandle {
125125
focus: () => void;
126-
focusAt: (cursor: number) => void;
126+
focusAt: (cursor: number, endCursor?: number) => void;
127127
focusAtEnd: () => void;
128128
readSelectionRange: () => { start: number; end: number };
129129
requestCitationComment: (request: ComposerCitationCommentRequest) => void;
@@ -1684,7 +1684,7 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
16841684
]);
16851685

16861686
const focusAt = useCallback(
1687-
(nextCursor: number) => {
1687+
(nextCursor: number, endCursor = nextCursor) => {
16881688
if (!editor) return;
16891689
// view.focus() writes the state selection to the DOM as it focuses. A
16901690
// bare DOM focus leaves the native caret at the start until
@@ -1696,9 +1696,13 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
16961696
// overwrite that prompt; the pending rewrite places the caret instead.
16971697
if (snapshotRef.current.value !== latestValueRef.current) return;
16981698
const boundedCursor = clampEditorCursor(snapshotRef.current.value, nextCursor);
1699+
const boundedEndCursor = clampEditorCursor(snapshotRef.current.value, endCursor);
16991700
const map = serializeEditorDoc(editor.state.doc);
17001701
const flat = collapsedToFlat(map, boundedCursor);
1701-
editor.commands.setTextSelection(flatToPm(map, flat));
1702+
editor.commands.setTextSelection({
1703+
from: flatToPm(map, flat),
1704+
to: flatToPm(map, collapsedToFlat(map, boundedEndCursor)),
1705+
});
17021706
scrollTiptapCaretIntoView(editor);
17031707
if (boundedCursor === snapshotRef.current.cursor) return;
17041708
snapshotRef.current = {
@@ -1709,7 +1713,7 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
17091713
};
17101714
selectionRangeRef.current = {
17111715
start: snapshotRef.current.expandedCursor,
1712-
end: snapshotRef.current.expandedCursor,
1716+
end: expandEditorCursor(snapshotRef.current.value, boundedEndCursor),
17131717
};
17141718
onChangeRef.current(
17151719
snapshotRef.current.value,

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3879,6 +3879,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
38793879
options?: {
38803880
expectedText?: string;
38813881
expandedCursorAfterReplace?: number;
3882+
expandedSelectionEndAfterReplace?: number;
38823883
focusEditorAfterReplace?: boolean;
38833884
citationComment?: {
38843885
start: number;
@@ -3947,7 +3948,14 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
39473948
// Skip the deferred caret placement when the draft has moved on,
39483949
// or it drags the caret back behind what was typed since.
39493950
if (promptRef.current !== next.text) return;
3950-
composerEditorRef.current?.focusAt(nextCursor);
3951+
composerEditorRef.current?.focusAt(
3952+
nextCursor,
3953+
collapseExpandedComposerCursor(
3954+
next.text,
3955+
options?.expandedSelectionEndAfterReplace ?? nextExpandedCursor,
3956+
isLiteralPendingAnswer,
3957+
),
3958+
);
39513959
});
39523960
}
39533961
return true;
@@ -6424,7 +6432,18 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
64246432
},
64256433
);
64266434
}
6427-
if (change.prompt !== promptRef.current) setPromptFromTraitsStable(change.prompt);
6435+
if (change.prompt !== promptRef.current) {
6436+
const snapshot = readComposerSnapshotStable();
6437+
const selection = composerEditorRef.current?.readSelectionRange() ?? {
6438+
start: snapshot.expandedCursor,
6439+
end: snapshot.expandedCursor,
6440+
};
6441+
const offset = change.prompt.length - snapshot.value.length;
6442+
applyPromptReplacementStable(0, promptRef.current.length, change.prompt, {
6443+
expandedCursorAfterReplace: Math.max(0, selection.start + offset),
6444+
expandedSelectionEndAfterReplace: Math.max(0, selection.end + offset),
6445+
});
6446+
}
64286447
},
64296448
toggleModelPicker: () => {
64306449
if (isComposerModelPickerOpen) {
@@ -6596,7 +6615,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
65966615
selectedModel,
65976616
selectedInstanceId,
65986617
reportedModelSelection,
6599-
setPromptFromTraitsStable,
66006618
settings.planModeEnabled,
66016619
selectedModelOptionsForDispatch,
66026620
selectedModelSelection,

0 commit comments

Comments
 (0)