Skip to content

Commit de41c28

Browse files
authored
PT-3704: Fix changes being reverted when you change view options (#437)
* Fixed changing `viewOptions` reverting changes
1 parent 2fc5589 commit de41c28

File tree

3 files changed

+12
-38
lines changed

3 files changed

+12
-38
lines changed

libs/shared-react/src/plugins/usj/LoadStatePlugin.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
22
import { CLEAR_HISTORY_COMMAND } from "lexical";
3-
import { useEffect } from "react";
3+
import { RefObject, useEffect } from "react";
44
import { 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
*/
1517
export 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
}

packages/platform/src/editor/Editor.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import usjEditorAdaptor from "./adaptors/usj-editor.adaptor";
33
import { getUsjMarkerAction } from "./adaptors/usj-marker-action.utils";
44
import { EditorOptions, EditorProps, EditorRef } from "./editor.model";
55
import editorTheme from "./editor.theme";
6-
import OptionChangePlugin from "./OptionChangePlugin";
76
import ScriptureReferencePlugin from "./ScriptureReferencePlugin";
87
import TreeViewPlugin from "./TreeViewPlugin";
98
import { ToolbarPlugin } from "./toolbar/ToolbarPlugin";
@@ -360,15 +359,10 @@ const Editor = forwardRef(function Editor<TLogger extends LoggerBasic>(
360359
}
361360
/>
362361
)}
363-
<OptionChangePlugin
364-
options={{ view: viewOptions, nodes: nodeOptions }}
365-
editedUsjRef={editedUsjRef}
366-
usj={usj}
367-
setUsj={setUsj}
368-
/>
369362
<LoadStatePlugin
370363
key={loadTrigger}
371364
scripture={usj}
365+
scriptureRef={editedUsjRef}
372366
nodeOptions={nodeOptions}
373367
editorAdaptor={usjEditorAdaptor}
374368
viewOptions={viewOptions}

packages/platform/src/editor/OptionChangePlugin.tsx

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)