Skip to content

Commit ee5b802

Browse files
fix(web): restore the caret after the chip when a citation send is refused later
Save-and-send reports a dispatch once its synchronous checks pass, but the async send can still refuse without clearing the draft. The sent branch then only focused the editor, so the next keystroke landed before the chip. Move the caret after the chip in both branches, tagging the sent move with a transaction meta that handleEditorChange does not emit, so a pending controlled rewrite still wins when the send does clear the prompt. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 92633d0 commit ee5b802

2 files changed

Lines changed: 50 additions & 42 deletions

File tree

‎apps/web/src/components/ComposerPromptEditorTiptap.citationDraft.browser.test.tsx‎

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ const promptBefore = `See ${serializeAssistantCitation(citation)} `;
3131
function Composer({
3232
question,
3333
editorRef,
34-
sendBlocked = false,
34+
send = "clears",
3535
}: {
3636
question: boolean;
3737
editorRef: React.RefObject<ComposerPromptEditorHandle | null>;
38-
sendBlocked?: boolean;
38+
send?: "clears" | "blocked" | "refusedLater";
3939
}) {
4040
const [prompt, setPrompt] = useState(promptBefore);
4141
const [answer, setAnswer] = useState("");
@@ -64,8 +64,8 @@ function Composer({
6464
if (replacedValue === prompt) setPrompt(savedValue);
6565
}}
6666
onCitationSubmitAndSend={() => {
67-
if (sendBlocked) return false;
68-
setPrompt("");
67+
if (send === "blocked") return false;
68+
if (send === "clears") setPrompt("");
6969
return true;
7070
}}
7171
onPaste={() => {}}
@@ -130,30 +130,33 @@ it("clears the sent prompt when an empty citation comment sends it", async () =>
130130
await expect.element(page.getByTestId("composer-editor")).toHaveFocus();
131131
});
132132

133-
it("keeps the caret after the citation when save-and-send is blocked", async () => {
134-
const editorRef = createRef<ComposerPromptEditorHandle>();
135-
await using screen = await render(
136-
<Composer question={false} editorRef={editorRef} sendBlocked />,
137-
);
133+
it.each(["blocked", "refusedLater"] as const)(
134+
"keeps the caret after the citation when save-and-send is %s",
135+
async (send) => {
136+
const editorRef = createRef<ComposerPromptEditorHandle>();
137+
await using screen = await render(
138+
<Composer question={false} editorRef={editorRef} send={send} />,
139+
);
138140

139-
const sourceAnchor = findAssistantCitationSourceAnchor(document, citation);
140-
if (!sourceAnchor) throw new Error("Citation source did not resolve");
141-
await act(async () =>
142-
editorRef.current?.requestCitationComment({
143-
previousValue: "See ",
144-
value: promptBefore,
145-
citationStart: "See ".length,
146-
sourceAnchor,
147-
insertedSpaces: { before: false, after: true },
148-
}),
149-
);
150-
const comment = page.getByLabelText("Comment on selected text");
151-
await expect.element(comment).toHaveFocus();
152-
await userEvent.keyboard("{Control>}{Enter}{/Control}");
153-
await expect.element(comment).not.toBeInTheDocument();
154-
await userEvent.keyboard("x");
141+
const sourceAnchor = findAssistantCitationSourceAnchor(document, citation);
142+
if (!sourceAnchor) throw new Error("Citation source did not resolve");
143+
await act(async () =>
144+
editorRef.current?.requestCitationComment({
145+
previousValue: "See ",
146+
value: promptBefore,
147+
citationStart: "See ".length,
148+
sourceAnchor,
149+
insertedSpaces: { before: false, after: true },
150+
}),
151+
);
152+
const comment = page.getByLabelText("Comment on selected text");
153+
await expect.element(comment).toHaveFocus();
154+
await userEvent.keyboard("{Control>}{Enter}{/Control}");
155+
await expect.element(comment).not.toBeInTheDocument();
156+
await userEvent.keyboard("x");
155157

156-
await expect
157-
.element(screen.getByRole("status"))
158-
.toHaveAttribute("data-prompt", `See ${serializeAssistantCitation(citation)}x `);
159-
});
158+
await expect
159+
.element(screen.getByRole("status"))
160+
.toHaveAttribute("data-prompt", `See ${serializeAssistantCitation(citation)}x `);
161+
},
162+
);

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
Plugin,
2222
PluginKey,
2323
TextSelection,
24+
type Transaction,
2425
} from "@tiptap/pm/state";
2526
import { Decoration, DecorationSet, type EditorView } from "@tiptap/pm/view";
2627
import type { NodeType, ResolvedPos } from "@tiptap/pm/model";
@@ -482,16 +483,18 @@ function ComposerCitationNodeView({ node, editor, getPos }: NodeViewProps) {
482483
const sent = sentRef.current;
483484
sentRef.current = false;
484485
if (!editor.isEditable) return;
485-
// The send's controlled rewrite hasn't reached the editor yet; a caret move now would emit the old prompt.
486-
if (sent) {
487-
editor.view.focus();
486+
const pos = nodePos();
487+
const current = pos === null ? null : editor.state.doc.nodeAt(pos);
488+
if (pos === null || current?.type.name !== "composer-citation") {
489+
if (sent) editor.view.focus();
488490
return;
489491
}
490-
const pos = nodePos();
491-
if (pos === null) return;
492-
const current = editor.state.doc.nodeAt(pos);
493-
if (!current || current.type.name !== "composer-citation") return;
494-
editor.commands.focus(pos + current.nodeSize);
492+
// The send's controlled rewrite may still be pending; emitting this caret move would restore the old prompt.
493+
editor
494+
.chain()
495+
.setMeta(SILENT_SELECTION_META, sent)
496+
.focus(pos + current.nodeSize)
497+
.run();
495498
};
496499

497500
return (
@@ -737,6 +740,8 @@ function chipSelectionDecorations({ doc, selection }: EditorState): DecorationSe
737740

738741
type TiptapEditor = NonNullable<ReturnType<typeof useEditor>>;
739742

743+
const SILENT_SELECTION_META = "composerSilentSelection";
744+
740745
function replaceSkillChips(
741746
editor: TiptapEditor,
742747
replacements: ReturnType<typeof skillChipReplacements>,
@@ -1006,7 +1011,7 @@ function ComposerPromptEditorTiptapInner(
10061011
onSubmitAndSend: onCitationSubmitAndSend ?? (() => false),
10071012
};
10081013

1009-
const handleEditorChange = (updated: TiptapEditor) => {
1014+
const handleEditorChange = (updated: TiptapEditor, transaction: Transaction) => {
10101015
const map = serializeEditorDoc(updated.state.doc);
10111016
const contextIds = literalText ? [] : [...map.contextIds];
10121017
const { from, to } = updated.state.selection;
@@ -1033,7 +1038,7 @@ function ComposerPromptEditorTiptapInner(
10331038
previousSnapshot.contextIds.length === contextIds.length &&
10341039
previousSnapshot.contextIds.every((id, index) => id === contextIds[index])
10351040
);
1036-
if (isApplyingControlledUpdateRef.current) return;
1041+
if (isApplyingControlledUpdateRef.current || transaction.getMeta(SILENT_SELECTION_META)) return;
10371042
if (!snapshotChanged) {
10381043
if (didComposerSelectionChangeVisibly(previousSelectionRange, nextSelectionRange)) {
10391044
readCallbacks().onVisibleSelectionChange?.();
@@ -1467,10 +1472,10 @@ function ComposerPromptEditorTiptapInner(
14671472
return;
14681473
}
14691474
}
1470-
handleEditorChange(updated);
1475+
handleEditorChange(updated, transaction);
14711476
},
1472-
onSelectionUpdate: ({ editor: updated }) => {
1473-
handleEditorChange(updated);
1477+
onSelectionUpdate: ({ editor: updated, transaction }) => {
1478+
handleEditorChange(updated, transaction);
14741479
},
14751480
},
14761481
[],

0 commit comments

Comments
 (0)