-
Notifications
You must be signed in to change notification settings - Fork 618
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(lexical-editor): update editor state via UpdateStatePlugin (#3650)
- Loading branch information
Showing
6 changed files
with
62 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 0 additions & 36 deletions
36
packages/lexical-editor/src/plugins/LexicalUpdateStatePlugin/LexicalUpdateStatePlugin.tsx
This file was deleted.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
packages/lexical-editor/src/plugins/LexicalUpdateStatePlugin/UpdateStatePlugin.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useEffect } from "react"; | ||
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext"; | ||
import { isValidLexicalData } from "~/utils/isValidLexicalData"; | ||
import { generateInitialLexicalValue } from "~/utils/generateInitialLexicalValue"; | ||
import { LexicalValue } from "~/types"; | ||
|
||
interface UpdateStatePluginProps { | ||
value: LexicalValue | null; | ||
} | ||
|
||
/** | ||
* Updates the lexical state if new value is provided to the lexical editor trough props. | ||
*/ | ||
export const UpdateStatePlugin = ({ value }: UpdateStatePluginProps) => { | ||
const [editor] = useLexicalComposerContext(); | ||
useEffect(() => { | ||
if (value && editor) { | ||
editor.update(() => { | ||
const editorState = editor.getEditorState(); | ||
|
||
if (JSON.stringify(editorState.toJSON()) === value) { | ||
return; | ||
} | ||
|
||
try { | ||
const initialEditorState = editor.parseEditorState( | ||
isValidLexicalData(value) | ||
? (value as string) | ||
: (generateInitialLexicalValue() as string) | ||
); | ||
editor.setEditorState(initialEditorState); | ||
} catch (err) { | ||
console.log("Lexical state update error", err.message); | ||
// Ignore errors | ||
} | ||
}); | ||
} | ||
}, [value, editor]); | ||
|
||
return null; | ||
}; |
2 changes: 1 addition & 1 deletion
2
packages/lexical-editor/src/plugins/LexicalUpdateStatePlugin/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export * from "./LexicalUpdateStatePlugin"; | ||
export * from "./UpdateStatePlugin"; |