fix: prevent NoteEditor re-render stealing focus - use key remount, onBlur save, stable ref
This commit is contained in:
@@ -17,16 +17,17 @@ import { cn } from "@/lib/utils";
|
|||||||
import type { Note, PaginatedResponse } from "@/lib/types";
|
import type { Note, PaginatedResponse } from "@/lib/types";
|
||||||
|
|
||||||
// Simple TipTap-like editor using contentEditable
|
// Simple TipTap-like editor using contentEditable
|
||||||
|
// Uses a key to force remount on note change instead of setting innerHTML
|
||||||
const NoteEditor = memo(function NoteEditor({ initialContent, onSave, placeholder = "Start writing..." }: { initialContent: string; onSave: (html: string) => void; placeholder?: string }) {
|
const NoteEditor = memo(function NoteEditor({ initialContent, onSave, placeholder = "Start writing..." }: { initialContent: string; onSave: (html: string) => void; placeholder?: string }) {
|
||||||
const editorRef = useRef<HTMLDivElement>(null);
|
const editorRef = useRef<HTMLDivElement>(null);
|
||||||
const [isPlaceholder, setIsPlaceholder] = useState(!initialContent);
|
const [isPlaceholder, setIsPlaceholder] = useState(!initialContent);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (editorRef.current) {
|
if (editorRef.current && !editorRef.current.innerHTML) {
|
||||||
editorRef.current.innerHTML = initialContent || "";
|
editorRef.current.innerHTML = initialContent || "";
|
||||||
}
|
}
|
||||||
setIsPlaceholder(!initialContent);
|
setIsPlaceholder(!initialContent);
|
||||||
}, [initialContent]);
|
}, []); // Only set initial content once on mount
|
||||||
|
|
||||||
const handleBlur = () => {
|
const handleBlur = () => {
|
||||||
const html = editorRef.current?.innerHTML || "";
|
const html = editorRef.current?.innerHTML || "";
|
||||||
@@ -49,40 +50,13 @@ const NoteEditor = memo(function NoteEditor({ initialContent, onSave, placeholde
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Separate component for title input to prevent focus stealing on parent re-render
|
|
||||||
const NoteTitleInput = memo(function NoteTitleInput({ noteId, initialTitle }: { noteId: string; initialTitle: string }) {
|
|
||||||
const [localTitle, setLocalTitle] = useState(initialTitle);
|
|
||||||
const queryClient = useQueryClient();
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
setLocalTitle(initialTitle);
|
|
||||||
}, [initialTitle, noteId]);
|
|
||||||
|
|
||||||
const handleBlur = () => {
|
|
||||||
if (localTitle !== initialTitle) {
|
|
||||||
api.patch<Note>(/notes/ + noteId, { title: localTitle }).then(() => {
|
|
||||||
queryClient.invalidateQueries({ queryKey: [notes] });
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<input
|
|
||||||
value={localTitle}
|
|
||||||
onChange={(e) => setLocalTitle(e.target.value)}
|
|
||||||
onBlur={handleBlur}
|
|
||||||
className="flex h-9 w-full rounded-md border-0 bg-transparent px-0 py-1 text-lg font-semibold text-base shadow-none transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-0 disabled:cursor-not-allowed disabled:opacity-50"
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
function NotesPage() {
|
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 selectedNoteRef = useRef<Note | null>(null);
|
||||||
const [showBacklinks, setShowBacklinks] = useState(false);
|
const [showBacklinks, setShowBacklinks] = useState(false);
|
||||||
const [showVersions, setShowVersions] = useState(false);
|
const [showVersions, setShowVersions] = useState(false);
|
||||||
|
|
||||||
@@ -121,20 +95,17 @@ 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);
|
selectedNoteRef.current = detail;
|
||||||
setEditorContent(detail.content || "");
|
setEditorContent(detail.content || "");
|
||||||
} catch {
|
} catch {
|
||||||
setSelectedNote(note);
|
setSelectedNote(note);
|
||||||
setTitleDraft(note.title);
|
selectedNoteRef.current = note;
|
||||||
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) => {
|
||||||
if (saveTimerRef.current) clearTimeout(saveTimerRef.current);
|
if (saveTimerRef.current) clearTimeout(saveTimerRef.current);
|
||||||
saveTimerRef.current = setTimeout(() => {
|
saveTimerRef.current = setTimeout(() => {
|
||||||
@@ -145,7 +116,6 @@ function NotesPage() {
|
|||||||
}, 500);
|
}, 500);
|
||||||
}, [updateMutation]);
|
}, [updateMutation]);
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex h-[calc(100vh-8rem)] -m-4 md:-m-6">
|
<div className="flex h-[calc(100vh-8rem)] -m-4 md:-m-6">
|
||||||
{/* Left pane - note list */}
|
{/* Left pane - note list */}
|
||||||
@@ -199,7 +169,6 @@ 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 } });
|
||||||
@@ -235,7 +204,7 @@ function NotesPage() {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex-1 overflow-auto">
|
<div className="flex-1 overflow-auto">
|
||||||
<NoteEditor initialContent={editorContent} onSave={handleContentChange} />
|
<NoteEditor key={selectedNote?.id || 'none'} initialContent={editorContent} onSave={handleContentChange} />
|
||||||
</div>
|
</div>
|
||||||
{/* Backlinks section */}
|
{/* Backlinks section */}
|
||||||
{showBacklinks && selectedNote.backlinks && selectedNote.backlinks.length > 0 && (
|
{showBacklinks && selectedNote.backlinks && selectedNote.backlinks.length > 0 && (
|
||||||
|
|||||||
Reference in New Issue
Block a user