diff --git a/openspec/changes/sidepanel-ux-improvements/tasks.md b/openspec/changes/sidepanel-ux-improvements/tasks.md index f590c5d..d8a92ce 100644 --- a/openspec/changes/sidepanel-ux-improvements/tasks.md +++ b/openspec/changes/sidepanel-ux-improvements/tasks.md @@ -9,7 +9,7 @@ - [x] 2.1 In `sidepanel/sidepanel.js`, pass current caret (`selectionStart`/`selectionEnd`) and `scrollTop` to `saveNote` in the existing autosave (`doSave`) - [x] 2.2 Track caret-only movement via `document` `selectionchange` (filtered to the editor) and editor `scroll`; debounce ~800ms into a caret-only save that calls `saveNote` without broadcasting; skip it while a content autosave is pending -- [ ] 2.3 Flush any pending caret-only save on editor `blur` and on `visibilitychange` → hidden +- [x] 2.3 Flush any pending caret-only save on editor `blur` and on `visibilitychange` → hidden ## 3. Focus and caret/scroll restore diff --git a/sidepanel/sidepanel.js b/sidepanel/sidepanel.js index 4c51206..ddd8174 100644 --- a/sidepanel/sidepanel.js +++ b/sidepanel/sidepanel.js @@ -118,6 +118,7 @@ function scheduleSave() { } async function doSave() { + saveTimer = null; if (!currentKey || currentTabId == null) return; try { @@ -219,8 +220,22 @@ editor.addEventListener('blur', () => { if (deferredReload) { reloadFromDB(); } + flushCaretSave(); }); +document.addEventListener('visibilitychange', () => { + if (document.visibilityState === 'hidden') { + flushCaretSave(); + } +}); + +function flushCaretSave() { + if (caretTimer) { + clearTimeout(caretTimer); + doCaretSave(); + } +} + // --------------------------------------------------------------------------- // Caret-only persistence //