diff --git a/fix_editor.py b/fix_editor.py new file mode 100644 index 0000000..dfcbc46 --- /dev/null +++ b/fix_editor.py @@ -0,0 +1,77 @@ +import sys + +with open(sys.argv[1], 'r') as f: + content = f.read() + +# Replace NoteEditor to use ref-based onChange (no re-render on keystroke) +old_editor = '''const NoteEditor = memo(function NoteEditor({ content, onChange, placeholder = "Start writing..." }: { content: string; onChange: (html: string) => void; placeholder?: string }) { + const editorRef = useRef(null); + const [isPlaceholder, setIsPlaceholder] = useState(!content); + + useEffect(() => { + if (editorRef.current && !editorRef.current.innerHTML) { + editorRef.current.innerHTML = content || ""; + } + }, [content]); + + const handleInput = () => { + const html = editorRef.current?.innerHTML || ""; + setIsPlaceholder(!html || html === "
"); + onChange(html); + }; + + return ( +
+ {isPlaceholder && ( +
{placeholder}
+ )} +
+
+ ); +});''' + +new_editor = '''const NoteEditor = memo(function NoteEditor({ content, onChange, placeholder = "Start writing..." }: { content: string; onChange: (html: string) => void; placeholder?: string }) { + const editorRef = useRef(null); + const onChangeRef = useRef(onChange); + onChangeRef.current = onChange; + const [isPlaceholder, setIsPlaceholder] = useState(!content); + + useEffect(() => { + if (editorRef.current && !editorRef.current.innerHTML) { + editorRef.current.innerHTML = content || ""; + } + }, [content]); + + const handleInput = useCallback(() => { + const html = editorRef.current?.innerHTML || ""; + setIsPlaceholder(!html || html === "
"); + onChangeRef.current(html); + }, []); + + return ( +
+ {isPlaceholder && ( +
{placeholder}
+ )} +
+
+ ); +});''' + +content = content.replace(old_editor, new_editor, 1) + +with open(sys.argv[1], 'w') as f: + f.write(content) +print('Done')