11import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext" ;
2+ import { mergeRegister } from "@lexical/utils" ;
23import { SerializedVerseRef } from "@sillsdev/scripture" ;
34import {
45 $getNodeByKey ,
@@ -22,8 +23,9 @@ import {
2223 isVerseRange ,
2324 removeNodeAndAfter ,
2425 removeNodesBeforeNode ,
26+ VerseNode ,
2527} from "shared" ;
26- import { $findThisVerse , $findVerseOrPara } from "shared-react" ;
28+ import { $findThisVerse , $findVerseOrPara , ImmutableVerseNode } from "shared-react" ;
2729
2830/**
2931 * A component (plugin) that keeps the Scripture reference updated.
@@ -101,6 +103,22 @@ export default function ScriptureReferencePlugin({
101103 [ editor , book , chapterNum , verseNum , onScrRefChange ] ,
102104 ) ;
103105
106+ // Verse node destroyed - SELECTION_CHANGE_COMMAND won't fire if the cursor position didn't
107+ // change (e.g. cursor was at offset 0 of the node after the verse, and stays there after
108+ // deletion of the non-keyboard-selectable DecoratorNode).
109+ useEffect ( ( ) => {
110+ const onVerseDestroyed = ( nodeMutations : Map < string , "created" | "updated" | "destroyed" > ) => {
111+ const hasDestroyedVerse = [ ...nodeMutations . values ( ) ] . some (
112+ ( m ) => m === "created" || m === "destroyed" ,
113+ ) ;
114+ if ( hasDestroyedVerse ) editor . dispatchCommand ( SELECTION_CHANGE_COMMAND , undefined ) ;
115+ } ;
116+ return mergeRegister (
117+ editor . registerMutationListener ( ImmutableVerseNode , onVerseDestroyed ) ,
118+ editor . registerMutationListener ( VerseNode , onVerseDestroyed ) ,
119+ ) ;
120+ } , [ editor ] ) ;
121+
104122 return null ;
105123}
106124
0 commit comments