fix(editor): final review round, history, dialogs, Plus and limits

I1 scroll restored at once on a Back into layers; I2 Aller a opens the target dialog in one push; M1-M9, criteria 8 and 17 (left-draft notice), entry caps, no exhaustive-deps disables, locals within 5.
This commit is contained in:
bchanot committed 2026-11-13 23:42:32 +01:00
1 parent ce171427c0
commit 6bf9290da7
25 files changed
+1002 -202

No files matched your search

+30 -13
View File
@@ -1,4 +1,5 @@
import type { ElementKey } from '@faunosteo/core-wasm/types'
import { MAX_FINDING_PATH } from '../findings/tree'
import {
ANNOTATION_SECTIONS,
isFindingNode,
@@ -34,16 +35,23 @@ export const ENTRY_KEY = 'editorEntry'
const isRecord = (value: unknown): value is Record<string, unknown> =>
typeof value === 'object' && value !== null
/** Longest id or key read back: a longer one is no id this editor wrote. */
const MAX_ID_LENGTH = 128
const isId = (value: unknown): value is string =>
typeof value === 'string' && value.length <= MAX_ID_LENGTH
/** A finding path no deeper than the tree allows, each id within the cap. */
const isPath = (path: unknown): path is string[] =>
Array.isArray(path) && path.length <= MAX_FINDING_PATH && path.every(isId)
/** 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 }
}
if (raw.kind === 'zone' && isId(raw.id)) 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[] }
if (raw.kind !== 'finding' || !isId(zoneId) || !isPath(path)) return null
return { kind: 'finding', zone_id: zoneId, path }
}
const CREATION_KINDS = ['zone', 'category', 'node']
@@ -82,7 +90,7 @@ function sanitizeReference(
const sheet = !!finding && 'key' in finding && finding.citations
if (raw.create === true) return tab === 'references' ? { create: true } : null
const id = raw.edit
if (typeof id !== 'string' || id === '') return null
if (!isId(id) || id === '') return null
return tab === 'references' || sheet ? { edit: id } : null
}
@@ -121,16 +129,22 @@ export function withoutForms(view: EditorView): EditorView {
return sanitizeView(view, false) ?? view
}
/** The entry's id and owner, each a string within the cap, or null. */
function idsOf(raw: Record<string, unknown>) {
const { id, sheetId, userId } = raw
const valid = isId(id) && isId(sheetId) && isId(userId)
return valid ? { id, sheetId, userId } : 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 ids = idsOf(raw)
if (!view || !ids) return null
const below = raw.below === null ? null : sanitizeView(raw.below)
return { id, view, below, sheetId, userId }
return { ...ids, view, below }
}
/** An entry restores only for the sheet and the account that wrote it. */
@@ -164,7 +178,7 @@ 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.
// Another tab is a move of its own (« Tout afficher »): it pushes.
if (view.tab !== top.tab) return false
if (layerCount(view) < layerCount(top)) return true
const backToDrawing = view.tab === top.tab && isStepOver(top)
@@ -186,18 +200,21 @@ export function showsFinding(view: EditorView, key: ElementKey): boolean {
* au dessin"), goes back when the entry holds the view the move lands on (the entry that
* opened it), else it replaces; a layer, tab or section added pushes, unless the move
* asked for a replace. `backTo` (« Revenir au constat ») goes back when the entry below
* shows that finding.
* shows that finding. `own` (« Aller à ») is a move of its own: it pushes, even when it
* leaves fewer layers on the same tab, so the place it left stays one Back away.
*/
export function planWrite(
top: HistoryEntry,
view: EditorView,
replace: boolean,
backTo: ElementKey | null = null,
own = false,
): HistoryWrite {
if (sameView(top.view, view)) return 'none'
if (replace) return 'replace'
// « Revenir au constat »: the entry below already shows that finding, a Back is the move.
if (backTo && top.below && showsFinding(top.below, backTo)) return 'back'
if (own) return 'push'
if (isRemoval(top.view, view)) {
return top.below && sameView(top.below, view) ? 'back' : 'replace'
}