|
| 1 | +import { CALENDAR_PANEL_CLOSED, type CalendarPanelState } from './calendar-panel-auto' |
| 2 | + |
| 3 | +/** |
| 4 | + * Which right-hand panels an editor pane shows: Connections, Outline, Comments |
| 5 | + * and the Calendar (whose open carries the provenance bit from #502). |
| 6 | + * |
| 7 | + * Panels have always been sticky per pane, Obsidian-style: open the Outline |
| 8 | + * and it stays while you browse. With "Keep panels when switching notes" off, |
| 9 | + * 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. |
| 14 | + */ |
| 15 | +export interface PanePanelsState { |
| 16 | + connections: boolean |
| 17 | + outline: boolean |
| 18 | + comments: boolean |
| 19 | + calendar: CalendarPanelState |
| 20 | + /** |
| 21 | + * The user closed the calendar on this note. Only written per note: there it |
| 22 | + * keeps the auto-open for daily and weekly notes from reopening a calendar |
| 23 | + * the note was told to forget. A sticky pane never sets it, so its auto-open |
| 24 | + * keeps firing on every arrival at a date note, as it always has (#502). |
| 25 | + */ |
| 26 | + calendarDismissed: boolean |
| 27 | +} |
| 28 | + |
| 29 | +export type PanePanelsByPath = Record<string, PanePanelsState> |
| 30 | + |
| 31 | +/** A note the pane has not seen yet starts with nothing open. */ |
| 32 | +export const PANE_PANELS_CLOSED: PanePanelsState = { |
| 33 | + connections: false, |
| 34 | + outline: false, |
| 35 | + comments: false, |
| 36 | + calendar: CALENDAR_PANEL_CLOSED, |
| 37 | + calendarDismissed: false |
| 38 | +} |
| 39 | + |
| 40 | +export function samePanePanels(a: PanePanelsState, b: PanePanelsState): boolean { |
| 41 | + return ( |
| 42 | + a.connections === b.connections && |
| 43 | + a.outline === b.outline && |
| 44 | + a.comments === b.comments && |
| 45 | + a.calendar.open === b.calendar.open && |
| 46 | + a.calendar.auto === b.calendar.auto && |
| 47 | + a.calendarDismissed === b.calendarDismissed |
| 48 | + ) |
| 49 | +} |
| 50 | + |
| 51 | +export function panePanelsForPath( |
| 52 | + panelsByPath: PanePanelsByPath, |
| 53 | + path: string | null |
| 54 | +): PanePanelsState { |
| 55 | + return path ? panelsByPath[path] ?? PANE_PANELS_CLOSED : PANE_PANELS_CLOSED |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * The calendar after a write, with the per-note "dismissed" bit kept in step: |
| 60 | + * going from open to closed is the user (or Esc) putting it away on this note, |
| 61 | + * and any open clears that again. |
| 62 | + */ |
| 63 | +export function withCalendar( |
| 64 | + panels: PanePanelsState, |
| 65 | + calendar: CalendarPanelState, |
| 66 | + perNote: boolean |
| 67 | +): PanePanelsState { |
| 68 | + const calendarDismissed = calendar.open |
| 69 | + ? false |
| 70 | + : perNote && panels.calendar.open |
| 71 | + ? true |
| 72 | + : panels.calendarDismissed |
| 73 | + return { ...panels, calendar, calendarDismissed } |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * The map with `path` set to `panels`. A note whose panels are all closed is |
| 78 | + * dropped rather than stored, since closed is what an absent note reads as, so |
| 79 | + * the map only ever holds notes that differ from the default. |
| 80 | + */ |
| 81 | +export function panePanelsWithPath( |
| 82 | + panelsByPath: PanePanelsByPath, |
| 83 | + path: string | null, |
| 84 | + panels: PanePanelsState |
| 85 | +): PanePanelsByPath { |
| 86 | + if (!path) return panelsByPath |
| 87 | + const current = panelsByPath[path] |
| 88 | + if (samePanePanels(panels, PANE_PANELS_CLOSED)) { |
| 89 | + if (!current) return panelsByPath |
| 90 | + const { [path]: _dropped, ...rest } = panelsByPath |
| 91 | + return rest |
| 92 | + } |
| 93 | + if (current && samePanePanels(current, panels)) return panelsByPath |
| 94 | + return { ...panelsByPath, [path]: panels } |
| 95 | +} |
0 commit comments