11import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext" ;
22import { CLEAR_HISTORY_COMMAND } from "lexical" ;
3- import { useEffect } from "react" ;
3+ import { RefObject , useEffect } from "react" ;
44import { EditorAdaptor , EXTERNAL_USJ_MUTATION_TAG , LoggerBasic , NodeOptions } from "shared" ;
55
66/**
77 * A plugin component that updates the state of the lexical editor when incoming Scripture changes.
88 * @param scripture - Scripture data.
9+ * @param scriptureRef - Optional ref to scripture data. If provided, reads from ref at update time
10+ * to get the most current value (useful when options change triggers state updates).
911 * @param nodeOptions - Options for each node.
1012 * @param editorAdaptor - Editor adaptor.
1113 * @param viewOptions - View options of the editor.
@@ -14,12 +16,14 @@ import { EditorAdaptor, EXTERNAL_USJ_MUTATION_TAG, LoggerBasic, NodeOptions } fr
1416 */
1517export function LoadStatePlugin < TLogger extends LoggerBasic > ( {
1618 scripture,
19+ scriptureRef,
1720 nodeOptions,
1821 editorAdaptor,
1922 viewOptions,
2023 logger,
2124} : {
2225 scripture ?: unknown ;
26+ scriptureRef ?: RefObject < unknown > ;
2327 nodeOptions ?: NodeOptions ;
2428 editorAdaptor : EditorAdaptor ;
2529 viewOptions ?: unknown ;
@@ -32,8 +36,12 @@ export function LoadStatePlugin<TLogger extends LoggerBasic>({
3236 } , [ editorAdaptor , logger , nodeOptions ] ) ;
3337
3438 useEffect ( ( ) => {
39+ // Read scripture from ref if available (to get latest value after state updates),
40+ // otherwise fall back to the prop value
41+ const currentScripture = scriptureRef ?. current ?? scripture ;
42+
3543 editorAdaptor . reset ?.( ) ;
36- const serializedEditorState = editorAdaptor . serializeEditorState ( scripture , viewOptions ) ;
44+ const serializedEditorState = editorAdaptor . serializeEditorState ( currentScripture , viewOptions ) ;
3745 if ( serializedEditorState == null ) {
3846 logger ?. warn (
3947 "LoadStatePlugin: serializedEditorState was null or undefined. Skipping editor update." ,
@@ -57,7 +65,7 @@ export function LoadStatePlugin<TLogger extends LoggerBasic>({
5765 } catch {
5866 logger ?. error ( "LoadStatePlugin: error parsing or setting editor state." ) ;
5967 }
60- } , [ editor , editorAdaptor , logger , scripture , viewOptions ] ) ;
68+ } , [ editor , editorAdaptor , logger , scripture , scriptureRef , viewOptions ] ) ;
6169
6270 return null ;
6371}
0 commit comments