@@ -11,7 +11,7 @@ import { ListItemNode, ListNode } from '@payloadcms/richtext-lexical/lexical/lis
1111import { HeadingNode } from '@payloadcms/richtext-lexical/lexical/rich-text'
1212
1313import { OnChangePlugin } from '@payloadcms/richtext-lexical/lexical/react/LexicalOnChangePlugin'
14- import React , { Dispatch , SetStateAction , useEffect } from 'react'
14+ import React , { Dispatch , SetStateAction , useEffect , useRef } from 'react'
1515
1616import ToolbarPlugin from './plugins/toolbar-plugin'
1717import { UploadNode } from './nodes/image-node'
@@ -27,27 +27,41 @@ interface RichTextProps {
2727}
2828const RichTextContent : React . FC < RichTextProps > = ( { setValue, value, name, editable = true } ) => {
2929 const [ editor ] = useLexicalComposerContext ( )
30+ const previousValueRef = useRef < any > ( null )
3031
3132 const handleEditorChange = ( editorState : any ) => {
3233 editorState . read ( ( ) => {
3334 const json = editorState . toJSON ( )
34- setValue ?.( ( ) => {
35- return json
36- } )
35+ // Only update if the value actually changed
36+ if ( JSON . stringify ( json ) !== JSON . stringify ( previousValueRef . current ) ) {
37+ previousValueRef . current = json
38+ setValue ?.( ( ) => {
39+ return json
40+ } )
41+ }
3742 } )
3843 }
3944
4045 useEffect ( ( ) => {
4146 editor . setEditable ( editable )
42- } , [ editable ] )
47+ } , [ editable , editor ] )
4348
4449 useEffect ( ( ) => {
45- if ( value ) {
46- editor . update ( ( ) => {
47- editor . setEditorState ( editor . parseEditorState ( value as any ) )
48- } )
50+ if ( value && JSON . stringify ( value ) !== JSON . stringify ( previousValueRef . current ) ) {
51+ editor . update (
52+ ( ) => {
53+ try {
54+ const editorState = editor . parseEditorState ( value as any )
55+ editor . setEditorState ( editorState )
56+ previousValueRef . current = value
57+ } catch ( error ) {
58+ console . error ( 'Error parsing editor state:' , error )
59+ }
60+ } ,
61+ { discrete : true } ,
62+ )
4963 }
50- } , [ ] )
64+ } , [ value , editor ] )
5165
5266 return (
5367 < div className = "w-full" >
0 commit comments