fix: stabilize handleContentChange callback via ref to prevent NoteEditor re-render stealing focus
This commit is contained in:
@@ -53,6 +53,7 @@ function NotesPage() {
|
|||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const [search, setSearch] = useState("");
|
const [search, setSearch] = useState("");
|
||||||
const [selectedNote, setSelectedNote] = useState<Note | null>(null);
|
const [selectedNote, setSelectedNote] = useState<Note | null>(null);
|
||||||
|
const [titleDraft, setTitleDraft] = useState("");
|
||||||
const [editorContent, setEditorContent] = useState("");
|
const [editorContent, setEditorContent] = useState("");
|
||||||
const saveTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
const saveTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||||
const [showBacklinks, setShowBacklinks] = useState(false);
|
const [showBacklinks, setShowBacklinks] = useState(false);
|
||||||
@@ -93,24 +94,30 @@ function NotesPage() {
|
|||||||
try {
|
try {
|
||||||
const detail = await api.get<Note>("/notes/" + note.id);
|
const detail = await api.get<Note>("/notes/" + note.id);
|
||||||
setSelectedNote(detail);
|
setSelectedNote(detail);
|
||||||
|
setTitleDraft(detail.title);
|
||||||
setEditorContent(detail.content || "");
|
setEditorContent(detail.content || "");
|
||||||
} catch {
|
} catch {
|
||||||
setSelectedNote(note);
|
setSelectedNote(note);
|
||||||
|
setTitleDraft(note.title);
|
||||||
setEditorContent(note.content || "");
|
setEditorContent(note.content || "");
|
||||||
}
|
}
|
||||||
setShowBacklinks(false);
|
setShowBacklinks(false);
|
||||||
setShowVersions(false);
|
setShowVersions(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const selectedNoteRef = useRef(selectedNote);
|
||||||
|
selectedNoteRef.current = selectedNote;
|
||||||
|
|
||||||
const handleContentChange = useCallback((html: string) => {
|
const handleContentChange = useCallback((html: string) => {
|
||||||
setEditorContent(html);
|
setEditorContent(html);
|
||||||
if (saveTimerRef.current) clearTimeout(saveTimerRef.current);
|
if (saveTimerRef.current) clearTimeout(saveTimerRef.current);
|
||||||
saveTimerRef.current = setTimeout(() => {
|
saveTimerRef.current = setTimeout(() => {
|
||||||
if (selectedNote) {
|
const note = selectedNoteRef.current;
|
||||||
updateMutation.mutate({ id: selectedNote.id, data: { content: html } });
|
if (note) {
|
||||||
|
updateMutation.mutate({ id: note.id, data: { content: html } });
|
||||||
}
|
}
|
||||||
}, 500);
|
}, 500);
|
||||||
}, [selectedNote, updateMutation]);
|
}, [updateMutation]);
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -166,6 +173,7 @@ function NotesPage() {
|
|||||||
<input
|
<input
|
||||||
key={selectedNote?.id || 'none'}
|
key={selectedNote?.id || 'none'}
|
||||||
defaultValue={selectedNote?.title || ''}
|
defaultValue={selectedNote?.title || ''}
|
||||||
|
onMouseDown={(e) => e.stopPropagation()}
|
||||||
onBlur={(e) => {
|
onBlur={(e) => {
|
||||||
if (selectedNote && e.target.value !== selectedNote.title) {
|
if (selectedNote && e.target.value !== selectedNote.title) {
|
||||||
updateMutation.mutate({ id: selectedNote.id, data: { title: e.target.value } });
|
updateMutation.mutate({ id: selectedNote.id, data: { title: e.target.value } });
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import sys
|
||||||
|
|
||||||
|
with open(sys.argv[1], 'r') as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
old = '<input\n key={selectedNote?.id || \'none\'}\n defaultValue={selectedNote?.title || \'\'}\n onBlur={(e) => {'
|
||||||
|
new = '<input\n key={selectedNote?.id || \'none\'}\n defaultValue={selectedNote?.title || \'\'}\n onMouseDown={(e) => e.stopPropagation()}\n onBlur={(e) => {'
|
||||||
|
|
||||||
|
content = content.replace(old, new, 1)
|
||||||
|
|
||||||
|
with open(sys.argv[1], 'w') as f:
|
||||||
|
f.write(content)
|
||||||
|
print('Done')
|
||||||
Reference in New Issue
Block a user