feat(editor): browser history for tabs, sections and Aller a

EditorView held by the navigation reducer (restore_view), a history
adapter hook mounted in EditorWorkspace (push, replace, back, mount
rule, ownership, write budget, scroll map, refused POP), a guard stack
by layer, an announcer queue joined after open dialogs.
This commit is contained in:
bchanot committed 2026-11-13 17:57:52 +01:00
1 parent 69e225ca7a
commit 792394d3b2
11 files changed
+1268 -40

No files matched your search

+111
View File
@@ -0,0 +1,111 @@
import {
ANNOTATION_SECTIONS,
sameView,
type AnnotationSection,
type EditorView,
} from '../navigation'
import { EDITOR_TABS, type EditorTabId } from '../tabs'
/**
* What one browser history entry of the editor holds (R2F-1): the view, the view below
* it (the place a Back returns to), and whose editor wrote it. Navigation only: never
* typed text, a search term or a draft.
*/
export interface HistoryEntry {
/** Keys the scroll map: one per entry written, kept by a replace. */
id: string
view: EditorView
below: EditorView | null
sheetId: string
userId: string
}
/** The sheet and the account an entry may be restored for. */
export interface HistoryOwner {
sheetId: string
userId: string
}
/** The key of the entry inside the router's `location.state`. */
export const ENTRY_KEY = 'editorEntry'
const isRecord = (value: unknown): value is Record<string, unknown> =>
typeof value === 'object' && value !== null
/**
* A view read back from a history entry, or null when it is no view. Vanished keys drop
* their layer: no layer is ever written yet, so the three layer fields come back null.
* shortcut: later layers validate their key against the loaded sheet here.
*/
export function sanitizeView(raw: unknown): EditorView | null {
if (!isRecord(raw)) return null
if (!EDITOR_TABS.includes(raw.tab as EditorTabId)) return null
const known = ANNOTATION_SECTIONS.includes(raw.section as AnnotationSection)
return {
tab: raw.tab as EditorTabId,
section: known ? (raw.section as AnnotationSection) : 'drawing',
finding: null,
reference: null,
referenceFilter: null,
}
}
/** The entry `state` (a `location.state`) carries, or null when it carries none. */
export function readEntry(state: unknown): HistoryEntry | null {
const raw = isRecord(state) ? state[ENTRY_KEY] : null
if (!isRecord(raw)) return null
const view = sanitizeView(raw.view)
const { id, sheetId, userId } = raw
if (!view || typeof id !== 'string') return null
if (typeof sheetId !== 'string' || typeof userId !== 'string') return null
const below = raw.below === null ? null : sanitizeView(raw.below)
return { id, view, below, sheetId, userId }
}
/** An entry restores only for the sheet and the account that wrote it. */
export function ownedEntry(
state: unknown,
owner: HistoryOwner,
): HistoryEntry | null {
const entry = readEntry(state)
const mine = entry?.sheetId === owner.sheetId && entry.userId === owner.userId
return mine ? entry : null
}
export const entryState = (entry: HistoryEntry) => ({ [ENTRY_KEY]: entry })
/** Layers over the place: a finding dialog or a reference dialog each count one. */
export function layerCount(view: EditorView): number {
return (view.finding ? 1 : 0) + (view.reference ? 1 : 0)
}
/** What a changed view asks of the history. */
export type HistoryWrite = 'none' | 'push' | 'replace' | 'back'
/**
* A user move to `view`. A layer taken away goes back when the entry holds the view the
* move lands on (the entry that opened the layer), else it replaces; a layer, tab or
* section added pushes, unless the move asked for a replace.
*/
export function planWrite(
top: HistoryEntry,
view: EditorView,
replace: boolean,
): HistoryWrite {
if (sameView(top.view, view)) return 'none'
if (layerCount(view) < layerCount(top.view)) {
return top.below && sameView(top.below, view) ? 'back' : 'replace'
}
return replace ? 'replace' : 'push'
}
/** Sliding-window budget of history writes: past it a view stays in memory. */
export function createWriteBudget(limit: number, windowMs: number) {
const stamps: number[] = []
return (now: number): boolean => {
while (stamps.length > 0 && now - stamps[0] >= windowMs) stamps.shift()
if (stamps.length >= limit) return false
stamps.push(now)
return true
}
}