Skip to content

Commit e604f75

Browse files
committed
fix(web): cancelling a new citation no longer leaves a stray space
1 parent 611132c commit e604f75

2 files changed

Lines changed: 31 additions & 14 deletions

File tree

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,16 @@ export type ComposerCitationCommentRequest = {
159159
value: string;
160160
citationStart: number;
161161
sourceAnchor: AssistantCitationSourceAnchor;
162+
insertedSpaces: CitationInsertedSpaces;
162163
};
163164

165+
// Spaces added around a freshly inserted citation, removed with it if its comment is cancelled.
166+
type CitationInsertedSpaces = { before: boolean; after: boolean };
167+
164168
type OpenCitationComment = {
165169
key: string;
166170
sourceAnchor?: AssistantCitationSourceAnchor;
167-
removeOnCancel?: boolean;
171+
removeOnCancel?: CitationInsertedSpaces;
168172
};
169173

170174
const ComposerCitationCommentContext = createContext<{
@@ -368,18 +372,20 @@ function ComposerCitationNodeView({ node, editor, getPos }: NodeViewProps) {
368372
[editor, nodePos],
369373
);
370374

375+
// Undo a fresh insertion: drop the chip and the spaces inserted with it.
376+
const removeOnCancel = commentTarget?.removeOnCancel;
371377
const onRemove = useCallback(() => {
372-
if (!editor.isEditable) return;
378+
if (!editor.isEditable || !removeOnCancel) return;
373379
const pos = nodePos();
374380
if (pos === null) return;
375-
const current = editor.state.doc.nodeAt(pos);
381+
const { doc } = editor.state;
382+
const current = doc.nodeAt(pos);
376383
if (!current) return;
377-
editor
378-
.chain()
379-
.focus()
380-
.deleteRange({ from: pos, to: pos + current.nodeSize })
381-
.run();
382-
}, [editor, nodePos]);
384+
const end = pos + current.nodeSize;
385+
const from = removeOnCancel.before && doc.textBetween(pos - 1, pos) === " " ? pos - 1 : pos;
386+
const to = removeOnCancel.after && doc.textBetween(end, end + 1) === " " ? end + 1 : end;
387+
editor.chain().focus().deleteRange({ from, to }).run();
388+
}, [editor, nodePos, removeOnCancel]);
383389

384390
// Put the caret right after the chip so Enter sends and typing continues the prompt.
385391
const onRestoreFocus = useCallback(() => {
@@ -426,7 +432,7 @@ function ComposerCitationNodeView({ node, editor, getPos }: NodeViewProps) {
426432
if (open && !editor.isEditable) return;
427433
commentContext.onOpenChange(citeKey, open);
428434
},
429-
...(commentTarget?.removeOnCancel ? { onCancel: onRemove } : {}),
435+
...(removeOnCancel ? { onCancel: onRemove } : {}),
430436
onSave: onSaveComment,
431437
onSaveAndSend: (comment) => {
432438
if (!onSaveComment(comment)) return false;
@@ -1197,7 +1203,7 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
11971203
setOpenCitation({
11981204
key: citeKey,
11991205
sourceAnchor: pendingCitation.sourceAnchor,
1200-
removeOnCancel: true,
1206+
removeOnCancel: pendingCitation.insertedSpaces,
12011207
});
12021208
}
12031209
}
@@ -1280,7 +1286,7 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
12801286
setOpenCitation({
12811287
key: citeKey,
12821288
sourceAnchor: request.sourceAnchor,
1283-
removeOnCancel: true,
1289+
removeOnCancel: request.insertedSpaces,
12841290
});
12851291
}
12861292
},

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,11 @@ import {
200200
shouldUseRestingComposerLayout,
201201
} from "../composerFooterLayout";
202202
import { measureRestingComposerControls } from "./restingComposerControlsMeasurement";
203-
import { type ComposerPromptEditorHandle, ComposerPromptEditor } from "../ComposerPromptEditor";
203+
import {
204+
type ComposerCitationCommentRequest,
205+
type ComposerPromptEditorHandle,
206+
ComposerPromptEditor,
207+
} from "../ComposerPromptEditor";
204208
import {
205209
ComposerContextActionsContext,
206210
composerContextRecordsFromDraft,
@@ -3832,7 +3836,11 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
38323836
expectedText?: string;
38333837
expandedCursorAfterReplace?: number;
38343838
focusEditorAfterReplace?: boolean;
3835-
citationComment?: { start: number; sourceAnchor: AssistantCitationSourceAnchor };
3839+
citationComment?: {
3840+
start: number;
3841+
sourceAnchor: AssistantCitationSourceAnchor;
3842+
insertedSpaces: ComposerCitationCommentRequest["insertedSpaces"];
3843+
};
38363844
},
38373845
): boolean => {
38383846
if (
@@ -3862,6 +3870,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
38623870
value: next.text,
38633871
citationStart: options.citationComment.start,
38643872
sourceAnchor: options.citationComment.sourceAnchor,
3873+
insertedSpaces: options.citationComment.insertedSpaces,
38653874
});
38663875
}
38673876
promptRef.current = next.text;
@@ -6122,6 +6131,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
61226131
citationComment: {
61236132
start: cursor + (needsLeadingSpace ? 1 : 0),
61246133
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 },
61256136
},
61266137
focusEditorAfterReplace: false,
61276138
}

0 commit comments

Comments
 (0)