diff --git a/memento-note/components/rich-text-editor.tsx b/memento-note/components/rich-text-editor.tsx index 5eabdc5..4316ad5 100644 --- a/memento-note/components/rich-text-editor.tsx +++ b/memento-note/components/rich-text-editor.tsx @@ -359,12 +359,21 @@ export const RichTextEditor = forwardRef(null) const onChangeRef = useRef(onChange) onChangeRef.current = onChange + const htmlDebounceRef = useRef | null>(null) const emitContentChange = useCallback((html: string) => { lastEmittedContent.current = html onChangeRef.current?.(html) }, []) + // Debounced version for onUpdate — avoids calling getHTML() on every keystroke + const emitContentChangeDebounced = useCallback((editor: any) => { + if (htmlDebounceRef.current) clearTimeout(htmlDebounceRef.current) + htmlDebounceRef.current = setTimeout(() => { + emitContentChange(editor.getHTML()) + }, 400) + }, [emitContentChange]) + useEffect(() => { if (typeof window === 'undefined') return const checkMobile = () => setIsMobile(window.innerWidth < 768) @@ -595,8 +604,8 @@ export const RichTextEditor = forwardRef { - // Debounce getHTML() — it's expensive on long notes - emitContentChange(e.getHTML()) + // Debounced getHTML() — avoids traversing the whole doc on every keystroke + emitContentChangeDebounced(e) if (!e.isEditable) return const { from, empty } = e.state.selection if (!empty) return