77 Node ,
88 wrappingInputRule ,
99 type JSONContent ,
10+ type TiptapEditorHTMLElement ,
1011} from "@tiptap/core" ;
1112import { ReactNodeViewRenderer , NodeViewWrapper , type NodeViewProps } from "@tiptap/react" ;
1213import StarterKit from "@tiptap/starter-kit" ;
@@ -976,36 +977,15 @@ function ComposerPromptEditorTiptapInner(
976977 const expandEditorCursor = ( text : string , position : number ) =>
977978 literalText ? position : expandCollapsedComposerCursor ( text , position ) ;
978979
979- const onChangeRef = useRef ( onChange ) ;
980- const onVisibleSelectionChangeRef = useRef ( onVisibleSelectionChange ) ;
981- const onCommandKeyDownRef = useRef ( onCommandKeyDown ) ;
982- const isPathQueryActiveRef = useRef ( isPathQueryActive ) ;
983- const buildFragmentRef = useRef ( buildContextClipboardFragment ) ;
984- const importFragmentRef = useRef ( importContextFragment ) ;
985980 const latestValueRef = useRef ( value ) ;
986- const editorHolder = useRef < TiptapEditor | null > ( null ) ;
987-
988- useEffect ( ( ) => {
989- onChangeRef . current = onChange ;
990- } , [ onChange ] ) ;
991- useEffect ( ( ) => {
992- onVisibleSelectionChangeRef . current = onVisibleSelectionChange ;
993- } , [ onVisibleSelectionChange ] ) ;
994- useEffect ( ( ) => {
995- onCommandKeyDownRef . current = onCommandKeyDown ;
996- } , [ onCommandKeyDown ] ) ;
997- useEffect ( ( ) => {
998- isPathQueryActiveRef . current = isPathQueryActive ;
999- } , [ isPathQueryActive ] ) ;
1000- useEffect ( ( ) => {
1001- buildFragmentRef . current = buildContextClipboardFragment ;
1002- } , [ buildContextClipboardFragment ] ) ;
1003- useEffect ( ( ) => {
1004- importFragmentRef . current = importContextFragment ;
1005- } , [ importContextFragment ] ) ;
1006- useLayoutEffect ( ( ) => {
1007- latestValueRef . current = value ;
1008- } , [ value ] ) ;
981+ // The editor config is built once, so its handlers reach the current callbacks through here.
982+ const readCallbacks = useStableCallback ( ( ) => ( {
983+ onChange,
984+ onVisibleSelectionChange,
985+ onCommandKeyDown,
986+ isPathQueryActive,
987+ importContextFragment,
988+ } ) ) ;
1009989
1010990 // Stable so a skills refresh does not rerun the controlled-value effect.
1011991 const skillLabelFor = useStableCallback ( ( name : string ) => composerSkillMeta ( skills , name ) ) ;
@@ -1074,7 +1054,7 @@ function ComposerPromptEditorTiptapInner(
10741054 if ( isApplyingControlledUpdateRef . current ) return ;
10751055 if ( ! snapshotChanged ) {
10761056 if ( didComposerSelectionChangeVisibly ( previousSelectionRange , nextSelectionRange ) ) {
1077- onVisibleSelectionChangeRef . current ?.( ) ;
1057+ readCallbacks ( ) . onVisibleSelectionChange ?.( ) ;
10781058 }
10791059 return ;
10801060 }
@@ -1096,7 +1076,13 @@ function ComposerPromptEditorTiptapInner(
10961076 [ updated . state . selection . $from . nodeBefore , updated . state . selection . $from . nodeAfter ] . some (
10971077 ( node ) => node ?. isInline && node . isAtom && ! node . isText && node . type . name !== "hardBreak" ,
10981078 ) ;
1099- onChangeRef . current ( nextValue , nextCursor , nextExpandedCursor , suppressTrigger , contextIds ) ;
1079+ readCallbacks ( ) . onChange (
1080+ nextValue ,
1081+ nextCursor ,
1082+ nextExpandedCursor ,
1083+ suppressTrigger ,
1084+ contextIds ,
1085+ ) ;
11001086 } ;
11011087
11021088 const editorAttributes = {
@@ -1301,9 +1287,9 @@ function ComposerPromptEditorTiptapInner(
13011287 return true ;
13021288 }
13031289 }
1304- const handler = onCommandKeyDownRef . current ;
1290+ const handler = readCallbacks ( ) . onCommandKeyDown ;
13051291 if ( event . key === "Enter" ) {
1306- const instance = editorHolder . current ;
1292+ const instance = ( view . dom as TiptapEditorHTMLElement ) . editor ;
13071293 const isTaskItem = richText && ( instance ?. isActive ( "taskItem" ) ?? false ) ;
13081294 const isListItem = richText && ( instance ?. isActive ( "listItem" ) ?? false ) ;
13091295 const handled = handler ?.( "Enter" , event , isTaskItem || isListItem ) ?? false ;
@@ -1390,7 +1376,7 @@ function ComposerPromptEditorTiptapInner(
13901376 ) ;
13911377 return true ;
13921378 }
1393- const importFragment = importFragmentRef . current ;
1379+ const importFragment = readCallbacks ( ) . importContextFragment ;
13941380 let text =
13951381 ! literalText && importFragment
13961382 ? importPastedComposerText ( clipboardData , importFragment )
@@ -1412,8 +1398,8 @@ function ComposerPromptEditorTiptapInner(
14121398 const lineBefore = $from . parent . textBetween ( 0 , $from . parentOffset , undefined , "\uFFFC" ) ;
14131399 const literalLength = literalText
14141400 ? 0
1415- : pastedPathQueryLength ( lineBefore , text , isPathQueryActiveRef . current ?.( ) ) ;
1416- const editorInstance = editorHolder . current ;
1401+ : pastedPathQueryLength ( lineBefore , text , readCallbacks ( ) . isPathQueryActive ?.( ) ) ;
1402+ const editorInstance = ( view . dom as TiptapEditorHTMLElement ) . editor ;
14171403 if ( editorInstance ) {
14181404 const $paste = view . state . selection . $from ;
14191405 const nested = [ "listItem" , "taskItem" , "blockquote" ] . some ( ( name ) =>
@@ -1476,10 +1462,6 @@ function ComposerPromptEditorTiptapInner(
14761462 editor ?. setEditable ( ! disabled ) ;
14771463 } , [ disabled , editor ] ) ;
14781464
1479- useEffect ( ( ) => {
1480- editorHolder . current = editor ;
1481- } , [ editor ] ) ;
1482-
14831465 // Push attributes before layout measurements; Tiptap updates them later in a passive effect.
14841466 useLayoutEffect ( ( ) => {
14851467 if ( ! editor ?. isInitialized ) return ;
@@ -1539,7 +1521,10 @@ function ComposerPromptEditorTiptapInner(
15391521 onCitationDraftSave ( replacedValue , serializeEditorDoc ( current . state . doc ) . value ) ;
15401522 } ) ;
15411523
1524+ // The one place the controlled value reaches the editor. Imperative handlers compare against
1525+ // the committed value, so it is recorded here even before the editor exists.
15421526 useLayoutEffect ( ( ) => {
1527+ latestValueRef . current = value ;
15431528 if ( ! editor ) return ;
15441529 const initialSelection = ! hasAppliedControlledSelectionRef . current ;
15451530 hasAppliedControlledSelectionRef . current = true ;
@@ -1665,7 +1650,7 @@ function ComposerPromptEditorTiptapInner(
16651650 start : snapshotRef . current . expandedCursor ,
16661651 end : snapshotRef . current . expandedCursor ,
16671652 } ;
1668- onChangeRef . current (
1653+ readCallbacks ( ) . onChange (
16691654 snapshotRef . current . value ,
16701655 boundedCursor ,
16711656 snapshotRef . current . expandedCursor ,
@@ -1756,7 +1741,7 @@ function ComposerPromptEditorTiptapInner(
17561741 ) ;
17571742
17581743 const handleCopyCut = ( event : React . ClipboardEvent , cut : boolean ) => {
1759- const build = buildFragmentRef . current ;
1744+ const build = buildContextClipboardFragment ;
17601745 if ( ! editor || ( cut && ! editor . isEditable ) ) return ;
17611746 const clipboardData = event . clipboardData ;
17621747 const { from, to } = editor . state . selection ;
0 commit comments