The best way to control Claude Code, Codex and more from any computer or phone
fix(web): cancelling a new citation no longer leaves a stray space (#16828)
Afonso Ferreira committed
Oct 7, 2026 at 23:58 UTC
3143335fc3a568cbbb5174764961889272135cb9
2 files changed
+31
-14
apps/web/src/components/ComposerPromptEditorTiptap.tsx
+18
-12
@@ -183,12 +183,16 @@ export type ComposerCitationCommentRequest = {
183
value: string;
184
citationStart: number;
185
sourceAnchor: AssistantCitationSourceAnchor;
186
+ insertedSpaces: CitationInsertedSpaces;
187
};
188
189
+// Spaces added around a freshly inserted citation, removed with it if its comment is cancelled.
190
+type CitationInsertedSpaces = { before: boolean; after: boolean };
191
+
192
type OpenCitationComment = {
193
key: string;
194
sourceAnchor?: AssistantCitationSourceAnchor;
191
- removeOnCancel?: boolean;
195
+ removeOnCancel?: CitationInsertedSpaces;
196
};
197
198
const ComposerCitationCommentContext = createContext<{
@@ -392,18 +396,20 @@ function ComposerCitationNodeView({ node, editor, getPos }: NodeViewProps) {
396
[editor, nodePos],
397
);
398
399
+ // Undo a fresh insertion: drop the chip and the spaces inserted with it.
400
+ const removeOnCancel = commentTarget?.removeOnCancel;
401
const onRemove = useCallback(() => {
396
- if (!editor.isEditable) return;
402
+ if (!editor.isEditable || !removeOnCancel) return;
403
const pos = nodePos();
404
if (pos === null) return;
399
- const current = editor.state.doc.nodeAt(pos);
405
+ const { doc } = editor.state;
406
+ const current = doc.nodeAt(pos);
407
if (!current) return;
401
- editor
402
- .chain()
403
- .focus()
404
- .deleteRange({ from: pos, to: pos + current.nodeSize })
405
- .run();
406
- }, [editor, nodePos]);
408
+ const end = pos + current.nodeSize;
409
+ const from = removeOnCancel.before && doc.textBetween(pos - 1, pos) === " " ? pos - 1 : pos;
410
+ const to = removeOnCancel.after && doc.textBetween(end, end + 1) === " " ? end + 1 : end;
411
+ editor.chain().focus().deleteRange({ from, to }).run();
412
+ }, [editor, nodePos, removeOnCancel]);
413
414
// Put the caret right after the chip so Enter sends and typing continues the prompt.
415
const onRestoreFocus = useCallback(() => {
@@ -450,7 +456,7 @@ function ComposerCitationNodeView({ node, editor, getPos }: NodeViewProps) {
456
if (open && !editor.isEditable) return;
457
commentContext.onOpenChange(citeKey, open);
458
},
453
- ...(commentTarget?.removeOnCancel ? { onCancel: onRemove } : {}),
459
+ ...(removeOnCancel ? { onCancel: onRemove } : {}),
460
onSave: onSaveComment,
461
onSaveAndSend: (comment) => {
462
if (!onSaveComment(comment)) return false;
@@ -1531,7 +1537,7 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
1537
setOpenCitation({
1538
key: citeKey,
1539
sourceAnchor: pendingCitation.sourceAnchor,
1534
- removeOnCancel: true,
1540
+ removeOnCancel: pendingCitation.insertedSpaces,
1541
});
1542
}
1543
}
@@ -1618,7 +1624,7 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
1624
setOpenCitation({
1625
key: citeKey,
1626
sourceAnchor: request.sourceAnchor,
1621
- removeOnCancel: true,
1627
+ removeOnCancel: request.insertedSpaces,
1628
});
1629
}
1630
},
apps/web/src/components/chat/ChatComposer.tsx
+13
-2
@@ -200,7 +200,11 @@ import {
200
shouldUseRestingComposerLayout,
201
} from "../composerFooterLayout";
202
import { measureRestingComposerControls } from "./restingComposerControlsMeasurement";
203
-import { type ComposerPromptEditorHandle, ComposerPromptEditor } from "../ComposerPromptEditor";
203
+import {
204
+ type ComposerCitationCommentRequest,
205
+ type ComposerPromptEditorHandle,
206
+ ComposerPromptEditor,
207
+} from "../ComposerPromptEditor";
208
import {
209
ComposerContextActionsContext,
210
composerContextRecordsFromDraft,
@@ -3832,7 +3836,11 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
3836
expectedText?: string;
3837
expandedCursorAfterReplace?: number;
3838
focusEditorAfterReplace?: boolean;
3835
- citationComment?: { start: number; sourceAnchor: AssistantCitationSourceAnchor };
3839
+ citationComment?: {
3840
+ start: number;
3841
+ sourceAnchor: AssistantCitationSourceAnchor;
3842
+ insertedSpaces: ComposerCitationCommentRequest["insertedSpaces"];
3843
+ };
3844
},
3845
): boolean => {
3846
if (
@@ -3862,6 +3870,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
3870
value: next.text,
3871
citationStart: options.citationComment.start,
3872
sourceAnchor: options.citationComment.sourceAnchor,
3873
+ insertedSpaces: options.citationComment.insertedSpaces,
3874
});
3875
}
3876
promptRef.current = next.text;
@@ -6122,6 +6131,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
6131
citationComment: {
6132
start: cursor + (needsLeadingSpace ? 1 : 0),
6133
sourceAnchor: options.citationCommentAnchor,
6134
+ // An existing space after the cursor is consumed and re-emitted, so it stays.
6135
+ insertedSpaces: { before: needsLeadingSpace, after: rangeEnd === cursor },
6136
},
6137
focusEditorAfterReplace: false,
6138
}