@@ -21,6 +21,7 @@ import {
2121 Plugin ,
2222 PluginKey ,
2323 TextSelection ,
24+ type Transaction ,
2425} from "@tiptap/pm/state" ;
2526import { Decoration , DecorationSet , type EditorView } from "@tiptap/pm/view" ;
2627import type { NodeType , ResolvedPos } from "@tiptap/pm/model" ;
@@ -186,7 +187,7 @@ export interface ComposerPromptEditorProps {
186187 onPageScrollKeyDown ?: ( key : "PageUp" | "PageDown" ) => void ;
187188 onPageScrollKeyUp ?: ( key : string ) => void ;
188189 onPageScrollRelease ?: ( ) => void ;
189- onCitationSubmitAndSend ?: ( ) => void ;
190+ onCitationSubmitAndSend ?: ( ) => boolean ;
190191 /** Saves unsaved citation text into replacedValue when a controlled rewrite replaces the document. */
191192 onCitationDraftSave ?: ( replacedValue : string , savedValue : string ) => void ;
192193 onPaste : React . ClipboardEventHandler < HTMLElement > ;
@@ -213,12 +214,12 @@ const ComposerCitationCommentContext = createContext<{
213214 openComment : OpenCitationComment | null ;
214215 onOpenChange : ( citeKey : string , open : boolean ) => void ;
215216 onDraftChange : ( citeKey : string , comment : string ) => void ;
216- onSubmitAndSend : ( ) => void ;
217+ onSubmitAndSend : ( ) => boolean ;
217218} > ( {
218219 openComment : null ,
219220 onOpenChange : ( ) => { } ,
220221 onDraftChange : ( ) => { } ,
221- onSubmitAndSend : ( ) => { } ,
222+ onSubmitAndSend : ( ) => false ,
222223} ) ;
223224
224225const RichComposerSkillsContext = createContext < ReadonlyArray < ServerProviderSkill > > ( [ ] ) ;
@@ -477,13 +478,23 @@ function ComposerCitationNodeView({ node, editor, getPos }: NodeViewProps) {
477478 editor . chain ( ) . focus ( ) . deleteRange ( { from, to } ) . run ( ) ;
478479 } ;
479480
481+ const sentRef = useRef ( false ) ;
480482 const onRestoreFocus = ( ) => {
483+ const sent = sentRef . current ;
484+ sentRef . current = false ;
481485 if ( ! editor . isEditable ) return ;
482486 const pos = nodePos ( ) ;
483- if ( pos === null ) return ;
484- const current = editor . state . doc . nodeAt ( pos ) ;
485- if ( ! current || current . type . name !== "composer-citation" ) return ;
486- editor . commands . focus ( pos + current . nodeSize ) ;
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 ( ) ;
490+ return ;
491+ }
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 ( ) ;
487498 } ;
488499
489500 return (
@@ -525,7 +536,7 @@ function ComposerCitationNodeView({ node, editor, getPos }: NodeViewProps) {
525536 onDraftChange : ( comment ) => commentContext . onDraftChange ( citeKey , comment ) ,
526537 onSaveAndSend : ( comment ) => {
527538 if ( ! onSaveComment ( comment ) ) return false ;
528- commentContext . onSubmitAndSend ( ) ;
539+ sentRef . current = commentContext . onSubmitAndSend ( ) ;
529540 return true ;
530541 } ,
531542 onRestoreFocus,
@@ -729,6 +740,8 @@ function chipSelectionDecorations({ doc, selection }: EditorState): DecorationSe
729740
730741type TiptapEditor = NonNullable < ReturnType < typeof useEditor > > ;
731742
743+ const SILENT_SELECTION_META = "composerSilentSelection" ;
744+
732745function replaceSkillChips (
733746 editor : TiptapEditor ,
734747 replacements : ReturnType < typeof skillChipReplacements > ,
@@ -995,10 +1008,10 @@ function ComposerPromptEditorTiptapInner(
9951008 onDraftChange : ( nodeKey : string , comment : string ) => {
9961009 citationDraftRef . current = { key : nodeKey , comment } ;
9971010 } ,
998- onSubmitAndSend : onCitationSubmitAndSend ?? ( ( ) => { } ) ,
1011+ onSubmitAndSend : onCitationSubmitAndSend ?? ( ( ) => false ) ,
9991012 } ;
10001013
1001- const handleEditorChange = ( updated : TiptapEditor ) => {
1014+ const handleEditorChange = ( updated : TiptapEditor , transaction : Transaction ) => {
10021015 const map = serializeEditorDoc ( updated . state . doc ) ;
10031016 const contextIds = literalText ? [ ] : [ ...map . contextIds ] ;
10041017 const { from, to } = updated . state . selection ;
@@ -1025,7 +1038,7 @@ function ComposerPromptEditorTiptapInner(
10251038 previousSnapshot . contextIds . length === contextIds . length &&
10261039 previousSnapshot . contextIds . every ( ( id , index ) => id === contextIds [ index ] )
10271040 ) ;
1028- if ( isApplyingControlledUpdateRef . current ) return ;
1041+ if ( isApplyingControlledUpdateRef . current || transaction . getMeta ( SILENT_SELECTION_META ) ) return ;
10291042 if ( ! snapshotChanged ) {
10301043 if ( didComposerSelectionChangeVisibly ( previousSelectionRange , nextSelectionRange ) ) {
10311044 readCallbacks ( ) . onVisibleSelectionChange ?.( ) ;
@@ -1459,10 +1472,10 @@ function ComposerPromptEditorTiptapInner(
14591472 return ;
14601473 }
14611474 }
1462- handleEditorChange ( updated ) ;
1475+ handleEditorChange ( updated , transaction ) ;
14631476 } ,
1464- onSelectionUpdate : ( { editor : updated } ) => {
1465- handleEditorChange ( updated ) ;
1477+ onSelectionUpdate : ( { editor : updated , transaction } ) => {
1478+ handleEditorChange ( updated , transaction ) ;
14661479 } ,
14671480 } ,
14681481 [ ] ,
0 commit comments