Skip to content

Commit 53c339c

Browse files
committed
Feat(panels): per-note panels come back after a restart (#794)
With "Keep panels when switching notes" off, each note remembered its own Connections, Outline, Comments and Calendar, but only until you quit. Hand testing showed that as the one place the feature felt unfinished: set up a project note with its Outline, a reading note with Comments, relaunch, and every note was bare again while the tabs and splits around them had come back. The memory now rides in the workspace snapshot, the same record that already restores tabs, layout and the sidebar, locally and as `.zennotes/workspace.json` for a synced vault. Nothing is written into the notes themselves, which stay plain files. A calendar you closed on a daily note stays closed after a restart too, because the "dismissed" bit travels with the rest. What it is careful about, since that file syncs between machines and versions and can be edited by hand: - It stays small. Only panes that still exist are written, only what is open is spelled out (a note with Outline open is `{"outline": true}`), a note with nothing open is not stored at all, and each pane keeps the 200 most recently set notes. Setting a note's panels again moves it to the back of that queue. - Nothing in it is trusted on the way back in: unknown panes are dropped and every field is coerced to its type. A snapshot written before this change simply has no panels. - Once the note listing is in, memory for notes that no longer exist is retired, with the same empty-listing guard the tab check uses, so dead paths do not pile up after deletes on another machine. - Toggling a panel changes nothing else about the workspace, so that action asks for the save itself instead of waiting for a tab change. The default mode is untouched: with the setting on, panels belong to the pane for the session and nothing is restored at launch, exactly as in 2.51.1. Per-note view modes (Edit / Split / Preview) are still session-only; this change does not extend to them.
1 parent 007f52c commit 53c339c

7 files changed

Lines changed: 312 additions & 13 deletions

File tree

‎apps/desktop/src/main/app-config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ const SCALAR_FIELDS: Partial<Record<PortablePrefKey, ScalarFieldMap>> = {
367367
keepPanelsAcrossNotes: {
368368
section: 'view',
369369
tomlKey: 'keep_panels_across_notes',
370-
comment: 'true = Connections / Outline / Comments / Calendar stay as set while you switch notes; false = each note remembers its own panels for the session'
370+
comment: 'true = Connections / Outline / Comments / Calendar stay as set while you switch notes; false = each note remembers its own panels (saved with the workspace, so they survive a restart)'
371371
},
372372
calendarWeekStart: {
373373
section: 'view',

‎packages/app-core/src/components/SettingsModal.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2639,7 +2639,7 @@ export function SettingsModal(): JSX.Element {
26392639
/>
26402640
<ToggleRow
26412641
label="Keep panels when switching notes"
2642-
description="Connections, Outline, Comments and Calendar stay as you set them while you move between notes. Turn off and each note remembers its own panels until you quit, so a note you have not opened yet starts with none."
2642+
description="Connections, Outline, Comments and Calendar stay as you set them while you move between notes. Turn off and each note remembers its own panels, across restarts too, so a note you have not opened yet starts with none."
26432643
value={keepPanelsAcrossNotes}
26442644
settingId="keep-panels"
26452645
onChange={setKeepPanelsAcrossNotes}

‎packages/app-core/src/lib/help.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export const HELP_QUICK_START: HelpCard[] = [
7171
{
7272
title: 'Keep supporting material nearby',
7373
body:
74-
'Tabs, splits, floating windows, the reference pane, and the connections panel are all there to help you keep related material visible while you write instead of forcing constant back-and-forth navigation. The Connections, Outline, Comments and Calendar panels stay as you set them while you move between notes. If you would rather each note kept its own, turn off Settings → Editor → Keep panels when switching notes (or run Remember Panels per Note from the command palette): a note then comes back with the panels you left it with, until you quit, and a note you have not opened yet starts with none. With the automatic calendar on, a daily or weekly note whose calendar you closed stays closed when you return to it. The setting travels with your portable config as `keep_panels_across_notes` under `[view]`.'
74+
'Tabs, splits, floating windows, the reference pane, and the connections panel are all there to help you keep related material visible while you write instead of forcing constant back-and-forth navigation. The Connections, Outline, Comments and Calendar panels stay as you set them while you move between notes. If you would rather each note kept its own, turn off Settings → Editor → Keep panels when switching notes (or run Remember Panels per Note from the command palette): a note then comes back with the panels you left it with, after a restart too (the memory is saved with your tabs and layout, never in the note), and a note you have not opened yet starts with none. With the automatic calendar on, a daily or weekly note whose calendar you closed stays closed when you return to it. The setting travels with your portable config as `keep_panels_across_notes` under `[view]`.'
7575
},
7676
{
7777
title: 'Use files without leaving ZenNotes',

‎packages/app-core/src/lib/pane-panels.test.ts‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { CALENDAR_PANEL_CLOSED } from './calendar-panel-auto'
33
import {
44
PANE_PANELS_CLOSED,
55
panePanelsForPath,
6+
panePanelsForSnapshot,
7+
panePanelsFromSnapshot,
68
panePanelsWithPath,
9+
prunePanePanels,
710
withCalendar,
811
type PanePanelsState
912
} from './pane-panels'
@@ -58,3 +61,83 @@ describe('per-note panels (#794)', () => {
5861
expect(withCalendar(open, CALENDAR_PANEL_CLOSED, false).calendarDismissed).toBe(false)
5962
})
6063
})
64+
65+
describe('per-note panels in the workspace snapshot (#794)', () => {
66+
const panes = new Set(['pane-a', 'pane-b'])
67+
68+
it('round-trips through JSON, so a restart brings each note back as it was left', () => {
69+
const live = {
70+
'pane-a': {
71+
'inbox/A.md': outlineOpen,
72+
'inbox/Daily.md': withCalendar(PANE_PANELS_CLOSED, { open: true, auto: true }, true)
73+
},
74+
'pane-b': { 'inbox/B.md': { ...PANE_PANELS_CLOSED, comments: true, connections: true } }
75+
}
76+
const file = JSON.parse(JSON.stringify(panePanelsForSnapshot(live, panes)))
77+
expect(panePanelsFromSnapshot(file, panes)).toEqual(live)
78+
})
79+
80+
it('keeps a calendar the user closed on a note closed after a restart', () => {
81+
const open = withCalendar(PANE_PANELS_CLOSED, { open: true, auto: true }, true)
82+
const dismissed = withCalendar(open, CALENDAR_PANEL_CLOSED, true)
83+
const file = JSON.parse(JSON.stringify(panePanelsForSnapshot({ 'pane-a': { d: dismissed } }, panes)))
84+
expect(panePanelsFromSnapshot(file, panes)['pane-a'].d.calendarDismissed).toBe(true)
85+
})
86+
87+
it('writes only panes the layout still has', () => {
88+
const live = { 'pane-a': { a: outlineOpen }, 'pane-closed': { b: outlineOpen }, 'pane-b': {} }
89+
expect(panePanelsForSnapshot(live, panes)).toEqual({ 'pane-a': { a: { outline: true } } })
90+
})
91+
92+
it('spells out only what is open, so the synced file stays small', () => {
93+
const live = {
94+
'pane-a': {
95+
a: { ...PANE_PANELS_CLOSED, connections: true, calendar: { open: true, auto: false } },
96+
b: withCalendar(
97+
withCalendar(PANE_PANELS_CLOSED, { open: true, auto: true }, true),
98+
CALENDAR_PANEL_CLOSED,
99+
true
100+
)
101+
}
102+
}
103+
expect(panePanelsForSnapshot(live, panes)).toEqual({
104+
'pane-a': { a: { connections: true, calendar: { open: true } }, b: { calendarDismissed: true } }
105+
})
106+
})
107+
108+
it('lets go of the least recently set notes past the limit', () => {
109+
let byPath = {}
110+
for (const path of ['one', 'two', 'three']) byPath = panePanelsWithPath(byPath, path, outlineOpen)
111+
// Touching "one" again makes it the most recent, so "two" is the one to go.
112+
byPath = panePanelsWithPath(byPath, 'one', { ...outlineOpen, comments: true })
113+
const kept = panePanelsForSnapshot({ 'pane-a': byPath }, panes, 2)['pane-a']
114+
expect(Object.keys(kept)).toEqual(['three', 'one'])
115+
})
116+
117+
// The file syncs between machines and versions and can be edited by hand.
118+
it('trusts nothing in the file', () => {
119+
expect(panePanelsFromSnapshot(null, panes)).toEqual({})
120+
expect(panePanelsFromSnapshot('nope', panes)).toEqual({})
121+
expect(panePanelsFromSnapshot([], panes)).toEqual({})
122+
const restored = panePanelsFromSnapshot(
123+
{
124+
'pane-a': {
125+
good: { outline: true, connections: 'yes', calendar: { open: false, auto: true } },
126+
allClosed: { outline: false },
127+
garbage: 7,
128+
'': { outline: true }
129+
},
130+
'pane-b': 'not a map',
131+
'pane-from-another-layout': { x: { outline: true } }
132+
},
133+
panes
134+
)
135+
expect(restored).toEqual({ 'pane-a': { good: outlineOpen } })
136+
})
137+
138+
it('retires notes that no longer exist, and says so only when something went', () => {
139+
const live = { 'pane-a': { kept: outlineOpen, gone: outlineOpen }, 'pane-b': { gone: outlineOpen } }
140+
expect(prunePanePanels(live, (path) => path === 'kept')).toEqual({ 'pane-a': { kept: outlineOpen } })
141+
expect(prunePanePanels(live, () => true)).toBe(live)
142+
})
143+
})

‎packages/app-core/src/lib/pane-panels.ts‎

Lines changed: 107 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import { CALENDAR_PANEL_CLOSED, type CalendarPanelState } from './calendar-panel
77
* Panels have always been sticky per pane, Obsidian-style: open the Outline
88
* and it stays while you browse. With "Keep panels when switching notes" off,
99
* a pane instead remembers one of these per note, the way it already remembers
10-
* each note's Edit / Split / Preview mode (#794). Like those modes the memory
11-
* is for the session only. It never belongs in the note, which is a plain
12-
* file; if it should outlive a restart, the workspace snapshot that already
13-
* restores tabs and layout is where it goes.
10+
* each note's Edit / Split / Preview mode (#794). The memory never belongs in
11+
* the note, which is a plain file. It outlives a restart the way tabs and
12+
* layout do, through the workspace snapshot: see `panePanelsForSnapshot`.
1413
*/
1514
export interface PanePanelsState {
1615
connections: boolean
@@ -91,5 +90,108 @@ export function panePanelsWithPath(
9190
return rest
9291
}
9392
if (current && samePanePanels(current, panels)) return panelsByPath
94-
return { ...panelsByPath, [path]: panels }
93+
// Re-inserted rather than updated in place, so key order is "least recently
94+
// set first" and the snapshot limit below knows which notes to let go of.
95+
const { [path]: _previous, ...rest } = panelsByPath
96+
return { ...rest, [path]: panels }
97+
}
98+
99+
/** How many notes per pane the workspace snapshot remembers panels for. */
100+
export const PANE_PANELS_SNAPSHOT_LIMIT = 200
101+
102+
/** One note's panels as the snapshot file spells them: only what is set. */
103+
export interface PanePanelsSnapshotEntry {
104+
connections?: true
105+
outline?: true
106+
comments?: true
107+
calendar?: { open: true; auto?: true }
108+
calendarDismissed?: true
109+
}
110+
111+
export type PanePanelsSnapshot = Record<string, Record<string, PanePanelsSnapshotEntry>>
112+
113+
/**
114+
* The per-note panels worth writing to the workspace snapshot: panes that still
115+
* exist, per pane only the most recently set notes, and per note only what is
116+
* open. The snapshot syncs with the vault, so it has to stay small however long
117+
* the vault lives.
118+
*/
119+
export function panePanelsForSnapshot(
120+
panePanels: Record<string, PanePanelsByPath>,
121+
paneIds: ReadonlySet<string>,
122+
limit: number = PANE_PANELS_SNAPSHOT_LIMIT
123+
): PanePanelsSnapshot {
124+
const out: PanePanelsSnapshot = {}
125+
for (const [paneId, byPath] of Object.entries(panePanels)) {
126+
if (!paneIds.has(paneId)) continue
127+
const entries = Object.entries(byPath)
128+
if (entries.length === 0) continue
129+
out[paneId] = Object.fromEntries(
130+
entries.slice(-limit).map(([path, panels]): [string, PanePanelsSnapshotEntry] => [
131+
path,
132+
{
133+
...(panels.connections && { connections: true }),
134+
...(panels.outline && { outline: true }),
135+
...(panels.comments && { comments: true }),
136+
...(panels.calendar.open && {
137+
calendar: panels.calendar.auto ? { open: true, auto: true } : { open: true }
138+
}),
139+
...(panels.calendarDismissed && { calendarDismissed: true })
140+
}
141+
])
142+
)
143+
}
144+
return out
145+
}
146+
147+
/**
148+
* Per-note panels read back from a workspace snapshot. The snapshot is a file
149+
* that travels between machines and app versions and can be edited by hand, so
150+
* nothing in it is trusted: panes the layout does not have are dropped, every
151+
* field is coerced to its type, and a note with nothing to remember is not kept.
152+
*/
153+
export function panePanelsFromSnapshot(
154+
raw: unknown,
155+
paneIds: ReadonlySet<string>
156+
): Record<string, PanePanelsByPath> {
157+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return {}
158+
const out: Record<string, PanePanelsByPath> = {}
159+
for (const [paneId, rawByPath] of Object.entries(raw)) {
160+
if (!paneIds.has(paneId)) continue
161+
if (!rawByPath || typeof rawByPath !== 'object' || Array.isArray(rawByPath)) continue
162+
let byPath: PanePanelsByPath = {}
163+
for (const [path, rawPanels] of Object.entries(rawByPath)) {
164+
if (!path || !rawPanels || typeof rawPanels !== 'object') continue
165+
const value = rawPanels as Record<string, unknown>
166+
const calendar = (value.calendar ?? {}) as Record<string, unknown>
167+
byPath = panePanelsWithPath(byPath, path, {
168+
connections: value.connections === true,
169+
outline: value.outline === true,
170+
comments: value.comments === true,
171+
calendar: { open: calendar.open === true, auto: calendar.open === true && calendar.auto === true },
172+
calendarDismissed: value.calendarDismissed === true
173+
})
174+
}
175+
if (Object.keys(byPath).length > 0) out[paneId] = byPath
176+
}
177+
return out
178+
}
179+
180+
/**
181+
* The map without the notes `keep` turns down, used once the note listing is
182+
* in to retire memory for notes that were deleted somewhere else. Returns the
183+
* same map when nothing was dropped.
184+
*/
185+
export function prunePanePanels(
186+
panePanels: Record<string, PanePanelsByPath>,
187+
keep: (path: string) => boolean
188+
): Record<string, PanePanelsByPath> {
189+
let changed = false
190+
const out: Record<string, PanePanelsByPath> = {}
191+
for (const [paneId, byPath] of Object.entries(panePanels)) {
192+
const kept = Object.entries(byPath).filter(([path]) => keep(path))
193+
if (kept.length !== Object.keys(byPath).length) changed = true
194+
if (kept.length > 0) out[paneId] = Object.fromEntries(kept)
195+
}
196+
return changed ? out : panePanels
95197
}

‎packages/app-core/src/store.test.ts‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,88 @@ describe('local vault shortcuts', () => {
738738
})
739739
})
740740

741+
describe('per-note panels survive a restart (#794)', () => {
742+
const vault = { root: '/Users/test/Notes', name: 'Notes' }
743+
const WORKSPACE_KEY = 'zen:workspace:v1'
744+
745+
function installVault(paths: string[]): void {
746+
installZen({
747+
openLocalVault: vi.fn().mockResolvedValue(vault),
748+
listNotes: vi.fn().mockResolvedValue(paths.map((path) => makeNote(path, path))),
749+
readNote: vi.fn().mockImplementation((path: string) => Promise.resolve(makeNote(path, path)))
750+
})
751+
}
752+
753+
/** A fresh store module over the SAME localStorage: what a relaunch is. */
754+
async function relaunch() {
755+
vi.resetModules()
756+
const mod = await import('./store')
757+
lastLoadedStore = mod as unknown as typeof lastLoadedStore
758+
return mod
759+
}
760+
761+
function savedSnapshot(): { panePanels?: Record<string, Record<string, { outline?: boolean }>> } {
762+
return JSON.parse(localStorage.getItem(WORKSPACE_KEY) ?? '{}')[vault.root] ?? {}
763+
}
764+
765+
it('saves them with the workspace and brings them back on the next launch', async () => {
766+
installVault(['inbox/A.md', 'inbox/B.md'])
767+
const first = await loadStore()
768+
await first.useStore.getState().openLocalVault(vault.root)
769+
await first.useStore.getState().selectNote('inbox/A.md')
770+
const paneId = first.useStore.getState().activePaneId
771+
772+
// Toggling a panel is the only thing that changed, so it has to ask for
773+
// the save itself: no tab or layout change is coming to do it.
774+
first.useStore
775+
.getState()
776+
.updatePanePanelsForPath(paneId, 'inbox/A.md', (panels) => ({ ...panels, outline: true }))
777+
expect(savedSnapshot().panePanels?.[paneId]?.['inbox/A.md']?.outline).toBe(true)
778+
779+
const second = await relaunch()
780+
await second.useStore.getState().openLocalVault(vault.root)
781+
const restored = second.useStore.getState()
782+
expect(restored.activePaneId).toBe(paneId)
783+
expect(restored.panePanels[paneId]['inbox/A.md'].outline).toBe(true)
784+
expect(restored.panePanels[paneId]['inbox/B.md']).toBeUndefined()
785+
})
786+
787+
it('forgets a note that was deleted while the app was closed', async () => {
788+
installVault(['inbox/A.md', 'inbox/B.md'])
789+
const first = await loadStore()
790+
await first.useStore.getState().openLocalVault(vault.root)
791+
await first.useStore.getState().selectNote('inbox/A.md')
792+
const paneId = first.useStore.getState().activePaneId
793+
for (const path of ['inbox/A.md', 'inbox/B.md']) {
794+
first.useStore
795+
.getState()
796+
.updatePanePanelsForPath(paneId, path, (panels) => ({ ...panels, outline: true }))
797+
}
798+
799+
installVault(['inbox/A.md'])
800+
const second = await relaunch()
801+
await second.useStore.getState().openLocalVault(vault.root)
802+
expect(Object.keys(second.useStore.getState().panePanels[paneId])).toEqual(['inbox/A.md'])
803+
})
804+
805+
it('opens a snapshot written before 2.52, which has no panels in it', async () => {
806+
installVault(['inbox/A.md'])
807+
const first = await loadStore()
808+
await first.useStore.getState().openLocalVault(vault.root)
809+
await first.useStore.getState().selectNote('inbox/A.md')
810+
// In the app the workspace effect asks for this save after a tab change.
811+
first.useStore.getState().persistWorkspace()
812+
const all = JSON.parse(localStorage.getItem(WORKSPACE_KEY) ?? '{}')
813+
delete all[vault.root].panePanels
814+
localStorage.setItem(WORKSPACE_KEY, JSON.stringify(all))
815+
816+
const second = await relaunch()
817+
await second.useStore.getState().openLocalVault(vault.root)
818+
expect(second.useStore.getState().panePanels).toEqual({})
819+
expect(second.useStore.getState().selectedPath).toBe('inbox/A.md')
820+
})
821+
})
822+
741823
describe('asset undo', () => {
742824
it('records deleted assets and restores them on undo', async () => {
743825
const deleted = {

0 commit comments

Comments
 (0)