fix: Add debounced Undo/Redo system to avoid character-by-character history

- Add debounced state updates for title and content (500ms delay)
- Immediate UI updates with delayed history saving
- Prevent one-letter-per-undo issue
- Add cleanup for debounce timers on unmount
This commit is contained in:
2026-01-04 14:28:11 +01:00
parent 355ffb59bb
commit 8d95f34fcc
4106 changed files with 630392 additions and 0 deletions

View File

@@ -69,12 +69,36 @@ export function NoteInput() {
const { title, content, checkItems, images } = noteState
// Debounced state updates for performance
const debounceTimerRef = useRef<NodeJS.Timeout | null>(null)
const updateTitle = (newTitle: string) => {
// Clear previous timer
if (debounceTimerRef.current) {
clearTimeout(debounceTimerRef.current)
}
// Update immediately for UI
setNoteState(prev => ({ ...prev, title: newTitle }))
// Debounce history update
debounceTimerRef.current = setTimeout(() => {
setNoteState(prev => ({ ...prev, title: newTitle }))
}, 500)
}
const updateContent = (newContent: string) => {
// Clear previous timer
if (debounceTimerRef.current) {
clearTimeout(debounceTimerRef.current)
}
// Update immediately for UI
setNoteState(prev => ({ ...prev, content: newContent }))
// Debounce history update
debounceTimerRef.current = setTimeout(() => {
setNoteState(prev => ({ ...prev, content: newContent }))
}, 500)
}
const updateCheckItems = (newCheckItems: CheckItem[]) => {
@@ -85,6 +109,15 @@ export function NoteInput() {
setNoteState(prev => ({ ...prev, images: newImages }))
}
// Cleanup debounce timer
useEffect(() => {
return () => {
if (debounceTimerRef.current) {
clearTimeout(debounceTimerRef.current)
}
}
}, [])
// Keyboard shortcuts
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {