@@ -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+
164168type OpenCitationComment = {
165169 key : string ;
166170 sourceAnchor ?: AssistantCitationSourceAnchor ;
167- removeOnCancel ?: boolean ;
171+ removeOnCancel ?: CitationInsertedSpaces ;
168172} ;
169173
170174const 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 } ,
0 commit comments