diff --git a/packages/editor/core/src/ui/hooks/useEditor.tsx b/packages/editor/core/src/ui/hooks/useEditor.tsx index 08444cff0..74d690ce7 100644 --- a/packages/editor/core/src/ui/hooks/useEditor.tsx +++ b/packages/editor/core/src/ui/hooks/useEditor.tsx @@ -4,6 +4,7 @@ import { useRef, MutableRefObject, useEffect, + useState, } from "react"; import { DeleteImage } from "../../types/delete-image"; import { CoreEditorProps } from "../props"; @@ -38,32 +39,40 @@ export const useEditor = ({ forwardedRef, setShouldShowAlert, }: CustomEditorProps) => { - const editor = useCustomEditor({ - editorProps: { - ...CoreEditorProps(uploadFile, setIsSubmitting), - ...editorProps, + const [internalEditorContent, setInternalEditorContent] = useState(value); + const editor = useCustomEditor( + { + editorProps: { + ...CoreEditorProps(uploadFile, setIsSubmitting), + ...editorProps, + }, + extensions: [...CoreEditorExtensions(deleteFile), ...extensions], + content: + typeof value === "string" && value.trim() !== "" ? value : "

", + onUpdate: async ({ editor }) => { + // for instant feedback loop + setIsSubmitting?.("submitting"); + setShouldShowAlert?.(true); + onChange?.(editor.getJSON(), getTrimmedHTML(editor.getHTML())); + }, }, - extensions: [...CoreEditorExtensions(deleteFile), ...extensions], - content: - typeof value === "string" && value.trim() !== "" ? value : "

", - onUpdate: async ({ editor }) => { - // for instant feedback loop - setIsSubmitting?.("submitting"); - setShouldShowAlert?.(true); - onChange?.(editor.getJSON(), getTrimmedHTML(editor.getHTML())); - }, - }); + [internalEditorContent], + ); const hasIntiliazedContent = useRef(false); useEffect(() => { - if (editor && value!=="" && !hasIntiliazedContent.current) { - editor.commands.setContent( - typeof value === "string" && value.trim() !== "" ? value : "

", - ); - hasIntiliazedContent.current = true; + if (editor) { + const cleanedValue = + typeof value === "string" && value.trim() !== "" ? value : "

"; + if (cleanedValue !== "

" && !hasIntiliazedContent.current) { + setInternalEditorContent(cleanedValue); + hasIntiliazedContent.current = true; + } else if (cleanedValue === "

" && hasIntiliazedContent.current) { + hasIntiliazedContent.current = false; + } } - }, [value]); + }, [value, editor]); const editorRef: MutableRefObject = useRef(null); editorRef.current = editor;