Files
faunosteo/apps/app/src/editor/sheetDocument.ts
T
bchanot 79a09f8b25 test(editor): keep-alive paths, neutral place visibility
Place visibility context moves to lib/placeVisibility.ts so the catalogue
no longer imports from components/editor. Adds tests: selection survival,
unmeasured hint round trip, keyboard tab move and « Aller à » with a field
focused, replace-tracing kept, announcements. Parse-sharing test uses
object identity. Early exits are two statements; shortcut comment on the
parse cache.

R-24:
- editor-equine.spec.ts:118, editor-geometry.spec.ts:376,
  editor-recovery.spec.ts:39, editor-signout.spec.ts:26: page.getByLabel
  ('Nom (FR)') -> place(page, 'general').getByLabel(...)
- editor-shell.spec.ts: toHaveCount(0) on the visible query -> count 1
  on the raw selector and toBeHidden()
- editor-keep-alive.test.tsx: JSON.parse stack-trace count -> same
  object reference across places
2026-11-06 20:57:08 +01:00

148 lines
4.0 KiB
TypeScript

import { useMemo } from 'react'
import type {
AnatomicalSheetDocument,
ElementKey,
LocalizedOptionalText,
SheetTexts,
} from '@faunosteo/core-wasm/types'
import type { Locale } from '@/i18n/types'
import { useEditorStore } from './state/editorStore'
/**
* The last text parsed and its document. Every kept place reads the document, so one parse
* serves them all per commit.
*
* shortcut: one entry of module state, not store state. Upgrade path: the store holds
* the parsed document next to its text and the places select it.
*/
let lastParse: { text: string; doc: AnatomicalSheetDocument | null } | null =
null
function parseDocument(text: string): AnatomicalSheetDocument | null {
if (text === '') return null
if (lastParse?.text === text) return lastParse.doc
let doc: AnatomicalSheetDocument | null = null
try {
doc = JSON.parse(text) as AnatomicalSheetDocument
} catch {
doc = null
}
lastParse = { text, doc }
return doc
}
/** The edited document, read from the store's text; null while nothing is loaded. */
export function useSheetDocument(): AnatomicalSheetDocument | null {
const text = useEditorStore((s) => s.documentText)
return useMemo(() => parseDocument(text), [text])
}
export const isBlank = (value: string): boolean => value.trim() === ''
/** A blank optional text is absent (LocalizedOptionalText omits the key, never ""). */
export function optionalText(
current: LocalizedOptionalText,
locale: Locale,
value: string,
): LocalizedOptionalText {
const next = { ...current }
if (isBlank(value)) delete next[locale]
else next[locale] = value
return next
}
/** A blank optional field is null where the type is nullable. */
export const nullable = (value: string): string | null =>
isBlank(value) ? null : value
export function textsOf(doc: AnatomicalSheetDocument): SheetTexts {
return {
name: doc.name,
description: doc.description,
scope_disclaimer: doc.scope_disclaimer,
attribution: doc.attribution,
}
}
interface Named {
id: string
name: { fr: string; en: string }
children: readonly Named[]
}
function findNode(
nodes: readonly Named[],
path: readonly string[],
): Named | undefined {
const [head, ...rest] = path
const node = nodes.find((n) => n.id === head)
if (!node || rest.length === 0) return node
return findNode(node.children, rest)
}
function localized(
name: { fr: string; en: string } | null | undefined,
locale: Locale,
): string | undefined {
return name?.[locale]
}
/** The name an element shows in a list or a dependents message; its id as a last resort. */
export function elementLabel(
doc: AnatomicalSheetDocument,
key: ElementKey,
locale: Locale,
): string {
switch (key.kind) {
case 'sheet':
return doc.name[locale]
case 'reference':
return doc.references.find((r) => r.id === key.id)?.citation ?? key.id
case 'region':
return (
localized(
doc.vertebral_config.regions.find((r) => r.code === key.code)?.name,
locale,
) ?? key.code
)
case 'category':
return (
localized(doc.categories.find((c) => c.id === key.id)?.name, locale) ??
key.id
)
case 'view':
return (
localized(doc.views.find((v) => v.id === key.id)?.name, locale) ??
key.id
)
case 'annotation':
return (
localized(doc.annotations.find((a) => a.id === key.id)?.name, locale) ??
key.id
)
case 'zone':
return (
localized(
doc.finding_zones.find((z) => z.id === key.id)?.name,
locale,
) ?? key.id
)
case 'finding':
return findingLabel(doc, key, locale)
}
}
function findingLabel(
doc: AnatomicalSheetDocument,
key: Extract<ElementKey, { kind: 'finding' }>,
locale: Locale,
): string {
const zone = doc.finding_zones.find((z) => z.id === key.zone_id)
const found = zone ? findNode(zone.categories, key.path) : undefined
return (
localized(found?.name, locale) ??
key.path[key.path.length - 1] ??
key.zone_id
)
}