Fix phantom note updates caused by setEditable emitting update events
This commit is contained in:
1 parent
25560b0e82
commit
70cf49f005
1 file changed
+14
-1
@@ -101,7 +101,9 @@ export default function RichNoteEditor({
|
||||
children,
|
||||
}: RichNoteEditorProps) {
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
// Track active note ID to only setContent when switching notes
|
||||
const currentNoteIdRef = useRef<string>(noteId);
|
||||
const isLoadedRef = useRef<boolean>(false);
|
||||
const [isRecording, setIsRecording] = useState(false);
|
||||
const [recordingSeconds, setRecordingSeconds] = useState(0);
|
||||
const mediaRecorderRef = useRef<MediaRecorder | null>(null);
|
||||
@@ -408,6 +410,8 @@ export default function RichNoteEditor({
|
||||
},
|
||||
},
|
||||
onUpdate: ({ editor: ed }) => {
|
||||
// Guard against synthetic/initial onUpdate events firing before user interaction
|
||||
if (!isLoadedRef.current) return;
|
||||
onChange(ed.getHTML());
|
||||
},
|
||||
});
|
||||
@@ -417,13 +421,22 @@ export default function RichNoteEditor({
|
||||
if (!editor) return;
|
||||
if (currentNoteIdRef.current !== noteId) {
|
||||
currentNoteIdRef.current = noteId;
|
||||
isLoadedRef.current = false;
|
||||
editor.commands.setContent(sanitizeOrUnescapeLinks(contentHtml || '<p></p>'), false);
|
||||
// Re-enable user edit detection after content has loaded
|
||||
setTimeout(() => {
|
||||
isLoadedRef.current = true;
|
||||
}, 50);
|
||||
} else if (!isLoadedRef.current) {
|
||||
isLoadedRef.current = true;
|
||||
}
|
||||
}, [noteId, contentHtml, editor]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!editor) return;
|
||||
editor.setEditable(!readOnly);
|
||||
if (editor.isEditable === readOnly) {
|
||||
editor.setEditable(!readOnly, false);
|
||||
}
|
||||
}, [readOnly, editor]);
|
||||
|
||||
const handleInsertFiles = async (files: File[]) => {
|
||||
|
||||
Reference in new issue
Block a user