Skip to content

Commit 7f614ad

Browse files
authored
Merge pull request #83 from cortex-reply/fix/files-tab
feat: adds options to update markdown files in files tab
2 parents 7779e31 + c57e035 commit 7f614ad

4 files changed

Lines changed: 785 additions & 28 deletions

File tree

src/components/Foundary/RichText/index.tsx

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { ListItemNode, ListNode } from '@payloadcms/richtext-lexical/lexical/lis
1111
import { HeadingNode } from '@payloadcms/richtext-lexical/lexical/rich-text'
1212

1313
import { 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

1616
import ToolbarPlugin from './plugins/toolbar-plugin'
1717
import { UploadNode } from './nodes/image-node'
@@ -27,27 +27,41 @@ interface RichTextProps {
2727
}
2828
const 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

Comments
 (0)