feat(app): editor Constats tree with review states and reorder

This commit is contained in:
bchanot committed 2026-11-04 13:45:34 +01:00
1 parent 66663dc0ca
commit 92c595caa1
11 files changed
+1219 -4

No files matched your search

+203
View File
@@ -0,0 +1,203 @@
import type {
AnatomicalSheetDocument,
ElementKey,
FindingCategory,
FindingNode,
FindingZone,
LocalizedText,
ReviewStatus,
} from '@faunosteo/core-wasm/types'
/**
* Levels a finding may have under its zone (category = 1). The core refuses a deeper
* insert (`too_deep`); the page only stops offering the button.
*/
export const MAX_FINDING_PATH = 8
export type FindingKind = 'zone' | 'category' | 'node'
/** One element of the tree, flattened in document order. */
export interface FindingItem {
/** Stable within the document: zone id then the path ids, as JSON. */
id: string
key: ElementKey
kind: FindingKind
zoneId: string
path: readonly string[]
/** `aria-level`: zone 1, category 2, nodes below. */
level: number
name: LocalizedText
/** null for a zone: it has no status. */
status: ReviewStatus | null
parentId: string | null
/** Position among its siblings and their count. */
index: number
siblings: number
hasChildren: boolean
/** The element as `update` takes it. */
element: FindingElement
}
export type FindingElement =
| { kind: 'zone'; value: FindingZone }
| { kind: 'finding_category'; value: FindingCategory }
| { kind: 'finding_node'; value: FindingNode }
/** The same element under another name (the core decides what that does to its marking). */
export function withName(
item: FindingItem,
name: LocalizedText,
): FindingElement {
const { element } = item
switch (element.kind) {
case 'zone':
return { kind: 'zone', value: { ...element.value, name } }
case 'finding_category':
return { kind: 'finding_category', value: { ...element.value, name } }
case 'finding_node':
return { kind: 'finding_node', value: { ...element.value, name } }
}
}
export const itemId = (zoneId: string, path: readonly string[]): string =>
JSON.stringify([zoneId, ...path])
function nodeItems(
zone: FindingZone,
nodes: readonly FindingNode[],
path: readonly string[],
parentId: string,
): FindingItem[] {
return nodes.flatMap((node, index) => {
const here = [...path, node.id]
const id = itemId(zone.id, here)
const self: FindingItem = {
id,
key: { kind: 'finding', zone_id: zone.id, path: here },
kind: 'node',
zoneId: zone.id,
path: here,
level: here.length + 1,
name: node.name,
status: node.review_status,
parentId,
index,
siblings: nodes.length,
hasChildren: node.children.length > 0,
element: { kind: 'finding_node', value: node },
}
return [self, ...nodeItems(zone, node.children, here, id)]
})
}
function categoryItems(zone: FindingZone, zoneItemId: string): FindingItem[] {
return zone.categories.flatMap((category, index) => {
const path = [category.id]
const id = itemId(zone.id, path)
const self: FindingItem = {
id,
key: { kind: 'finding', zone_id: zone.id, path },
kind: 'category',
zoneId: zone.id,
path,
level: 2,
name: category.name,
status: category.review_status,
parentId: zoneItemId,
index,
siblings: zone.categories.length,
hasChildren: category.children.length > 0,
element: { kind: 'finding_category', value: category },
}
return [self, ...nodeItems(zone, category.children, path, id)]
})
}
/** Every element of the findings tree in document order, collapsed or not. */
export function buildItems(doc: AnatomicalSheetDocument): FindingItem[] {
return doc.finding_zones.flatMap((zone, index) => {
const id = itemId(zone.id, [])
const self: FindingItem = {
id,
key: { kind: 'zone', id: zone.id },
kind: 'zone',
zoneId: zone.id,
path: [],
level: 1,
name: zone.name,
status: null,
parentId: null,
index,
siblings: doc.finding_zones.length,
hasChildren: zone.categories.length > 0,
element: { kind: 'zone', value: zone },
}
return [self, ...categoryItems(zone, id)]
})
}
/** The items whose ancestors are all expanded. */
export function visibleItems(
items: readonly FindingItem[],
expanded: ReadonlySet<string>,
): FindingItem[] {
const shown = new Set<string>()
return items.filter((item) => {
const open =
item.parentId === null ||
(shown.has(item.parentId) && expanded.has(item.parentId))
if (open) shown.add(item.id)
return open
})
}
export const canAddNode = (item: FindingItem | undefined): boolean =>
item !== undefined &&
item.kind !== 'zone' &&
item.path.length < MAX_FINDING_PATH
export interface TreeMove {
select?: string
expand?: string
collapse?: string
}
/** WAI-ARIA tree keys: what a key does from `current`, among the visible items. */
export function keyMove(
visible: readonly FindingItem[],
current: FindingItem,
expanded: ReadonlySet<string>,
key: string,
): TreeMove | null {
const at = visible.findIndex((item) => item.id === current.id)
const open = expanded.has(current.id)
switch (key) {
case 'ArrowDown':
return at < visible.length - 1 ? { select: visible[at + 1].id } : null
case 'ArrowUp':
return at > 0 ? { select: visible[at - 1].id } : null
case 'Home':
return { select: visible[0].id }
case 'End':
return { select: visible[visible.length - 1].id }
case 'ArrowRight':
if (!current.hasChildren) return null
return open ? { select: visible[at + 1].id } : { expand: current.id }
case 'ArrowLeft':
if (open && current.hasChildren) return { collapse: current.id }
return current.parentId ? { select: current.parentId } : null
default:
return null
}
}
/** The item id of a just-inserted element, appended under `parent` (null = a zone). */
export function createdItemId(
kind: FindingKind,
parent: FindingItem | null,
id: string,
): string {
if (kind === 'zone' || !parent) return itemId(id, [])
const path = kind === 'category' ? [id] : [...parent.path, id]
return itemId(parent.zoneId, path)
}