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
95 lines
3.1 KiB
TypeScript
95 lines
3.1 KiB
TypeScript
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
import { buildView } from '@/components/catalogue/CatalogueCanvas'
|
|
import { usePlaceVisible } from '@/lib/placeVisibility'
|
|
import { coreRuntime } from '@/core/runtime'
|
|
import { useEditorStore } from '../state/editorStore'
|
|
|
|
/** The composed root's own attributes, read as text (never `baseVal`), and its view. */
|
|
export interface RootAttributes {
|
|
/** The view this build was made for: a frame is never paired with another view's. */
|
|
viewId: string
|
|
viewBox: string | null
|
|
ratio: string
|
|
}
|
|
|
|
export type CanvasFailure = 'engine' | 'view'
|
|
|
|
interface Built {
|
|
root: RootAttributes | null
|
|
failure: CanvasFailure | null
|
|
}
|
|
|
|
const ANNOTATED = '[data-anno-id]'
|
|
|
|
function rootOf(viewId: string, svg: Element): RootAttributes {
|
|
return {
|
|
viewId,
|
|
viewBox: svg.getAttribute('viewBox'),
|
|
ratio: svg.getAttribute('preserveAspectRatio') ?? 'xMidYMid meet',
|
|
}
|
|
}
|
|
|
|
/** What the canvas rebuilds on: the handle, its version counter and its place shown. */
|
|
function useHandle() {
|
|
const preview = useEditorStore((s) => s.preview)
|
|
const version = useEditorStore((s) => s.handleVersion)
|
|
return { preview, version, visible: usePlaceVisible() }
|
|
}
|
|
|
|
/**
|
|
* Builds the core's composed view into `host`, again on every accepted handle change or
|
|
* retry (plan « Rendering and canvas »): the edit canvas follows the handle. Root and
|
|
* failure come from the same build; a failure is the engine notice (retry) or the view
|
|
* notice.
|
|
*/
|
|
export function useCanvasBuild(viewId: string) {
|
|
const handle = useHandle()
|
|
const host = useRef<HTMLDivElement | null>(null)
|
|
const [attempt, setAttempt] = useState(0)
|
|
const [built, setBuilt] = useState<Built>({ root: null, failure: null })
|
|
|
|
useEffect(() => {
|
|
const element = host.current
|
|
if (!element || !handle.preview) return
|
|
// One composed copy of a view in the page: the ids repeat otherwise.
|
|
if (!handle.visible) {
|
|
element.replaceChildren()
|
|
return
|
|
}
|
|
const result = buildView(handle.preview, viewId)
|
|
element.replaceChildren(...(result.ok ? [result.svg] : []))
|
|
setBuilt(
|
|
result.ok
|
|
? { root: rootOf(viewId, result.svg), failure: null }
|
|
: { root: null, failure: result.failure },
|
|
)
|
|
}, [handle.preview, viewId, handle.version, attempt, handle.visible])
|
|
|
|
const retry = useCallback(() => {
|
|
void coreRuntime()
|
|
.retry()
|
|
.finally(() => setAttempt((n) => n + 1))
|
|
}, [])
|
|
return { host, ...built, retry }
|
|
}
|
|
|
|
/** The selected annotation's markers wear `data-state="current"`, restyled in place. */
|
|
export function useSelectionMark(
|
|
host: React.RefObject<HTMLDivElement | null>,
|
|
selectedId: string | null,
|
|
rebuilt: unknown,
|
|
): void {
|
|
useEffect(() => {
|
|
host.current?.querySelectorAll<Element>(ANNOTATED).forEach((el) => {
|
|
if (el.getAttribute('data-anno-id') === selectedId) {
|
|
el.setAttribute('data-state', 'current')
|
|
} else el.removeAttribute('data-state')
|
|
})
|
|
}, [host, selectedId, rebuilt])
|
|
}
|
|
|
|
export const annotationIdOf = (target: EventTarget | null): string | null =>
|
|
target instanceof Element
|
|
? (target.closest(ANNOTATED)?.getAttribute('data-anno-id') ?? null)
|
|
: null
|