feat(editor): finding dialog and citations side sheet in the history

Constats opens each element in one modal dialog (view, edit with a
local draft, creation) driven by the editor view layer of the browser
history. The citations panel becomes a modal side sheet stacked over
it; its geometry, exit machinery and page inertness are removed.
Back and Forward restore the layers, a dirty form asks first.
This commit is contained in:
bchanot committed 2026-11-13 18:51:35 +01:00
1 parent 8e4f02620a
commit 18085cecf8
50 files changed
+2642 -1504

No files matched your search

+60 -8
View File
@@ -1,5 +1,7 @@
import type { ElementKey } from '@faunosteo/core-wasm/types'
import {
ANNOTATION_SECTIONS,
isFindingNode,
sameView,
type AnnotationSection,
type EditorView,
@@ -32,24 +34,66 @@ export const ENTRY_KEY = 'editorEntry'
const isRecord = (value: unknown): value is Record<string, unknown> =>
typeof value === 'object' && value !== null
/** A finding or zone key as the tree builds it, or null when `raw` is no such key. */
function sanitizeKey(raw: unknown): ElementKey | null {
if (!isRecord(raw)) return null
if (raw.kind === 'zone' && typeof raw.id === 'string') {
return { kind: 'zone', id: raw.id }
}
const { zone_id: zoneId, path } = raw
const ids = Array.isArray(path) && path.every((id) => typeof id === 'string')
if (raw.kind !== 'finding' || typeof zoneId !== 'string' || !ids) return null
return { kind: 'finding', zone_id: zoneId, path: path as string[] }
}
const CREATION_KINDS = ['zone', 'category', 'node']
/** A finding creation, a form: only read back where forms are kept. */
function sanitizeCreation(raw: unknown): EditorView['finding'] {
if (!isRecord(raw) || !isRecord(raw.create)) return null
const { kind, parent } = raw.create
if (typeof kind !== 'string' || !CREATION_KINDS.includes(kind)) return null
const key = parent === null ? null : sanitizeKey(parent)
if (parent !== null && key === null) return null
return { create: { kind, parent: key } }
}
/** The finding layer of a view: its dialog, with its citations sheet over it. */
function sanitizeFinding(raw: unknown, forms: boolean): EditorView['finding'] {
if (!isRecord(raw)) return null
if ('create' in raw) return forms ? sanitizeCreation(raw) : null
const key = sanitizeKey(raw.key)
if (!key) return null
const sheet = raw.citations === true && isFindingNode(key)
return { key, citations: sheet }
}
/**
* 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.
* A view read back from a history entry, or null when it is no view. The finding layer
* must name a well-formed key and sit on the Constats tab; whether the element still
* exists is the dialog's own gone rule. `forms` false drops the creation form (a reload
* never reopens a form). The reference layer and the filter stay null until they exist.
*/
export function sanitizeView(raw: unknown): EditorView | null {
export function sanitizeView(raw: unknown, forms = true): 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)
const finding =
raw.tab === 'findings' ? sanitizeFinding(raw.finding, forms) : null
return {
tab: raw.tab as EditorTabId,
section: known ? (raw.section as AnnotationSection) : 'drawing',
finding: null,
finding,
reference: null,
referenceFilter: null,
}
}
/** The view a reload shows for an entry: its reading layers, never a form. */
export function withoutForms(view: EditorView): EditorView {
return sanitizeView(view, false) ?? view
}
/** 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
@@ -76,9 +120,14 @@ export function ownedEntry(
export const entryState = (entry: HistoryEntry) => ({ [ENTRY_KEY]: entry })
/** Layers over the place: a finding dialog or a reference dialog each count one. */
/**
* Layers over the place: the finding dialog and the citations sheet over it, the
* reference dialog, each count one.
*/
export function layerCount(view: EditorView): number {
return (view.finding ? 1 : 0) + (view.reference ? 1 : 0)
const { finding } = view
const sheet = finding && 'key' in finding && finding.citations
return (finding ? 1 : 0) + (sheet ? 1 : 0) + (view.reference ? 1 : 0)
}
/** An Annotations section other than the drawing is a step over it, like a layer. */
@@ -88,6 +137,8 @@ function isStepOver(view: EditorView): boolean {
/** True when the move took a layer away, or left a section for its drawing. */
function isRemoval(top: EditorView, view: EditorView): boolean {
// Another tab is a move of its own (« Tout afficher », « Aller à »): it pushes.
if (view.tab !== top.tab) return false
if (layerCount(view) < layerCount(top)) return true
const backToDrawing = view.tab === top.tab && isStepOver(top)
return backToDrawing && !isStepOver(view) && layerCount(view) === 0
@@ -108,10 +159,11 @@ export function planWrite(
replace: boolean,
): HistoryWrite {
if (sameView(top.view, view)) return 'none'
if (replace) return 'replace'
if (isRemoval(top.view, view)) {
return top.below && sameView(top.below, view) ? 'back' : 'replace'
}
return replace ? 'replace' : 'push'
return 'push'
}
/** Sliding-window budget of history writes: past it a view stays in memory. */