feat(app): editor Annotations tab, core-decided placement, repair rules
This commit is contained in:
1 parent
92c595caa1
commit
26097852ff
37 files changed
+5001
-6
No files matched your search
@@ -0,0 +1,72 @@
|
||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||
import { buildView } from '@/components/catalogue/CatalogueCanvas'
|
||||
import { coreRuntime } from '@/core/runtime'
|
||||
import { useEditorStore } from '../state/editorStore'
|
||||
|
||||
/** The composed root's own attributes, read as text (never `baseVal`). */
|
||||
export interface RootAttributes {
|
||||
viewBox: string | null
|
||||
ratio: string
|
||||
}
|
||||
|
||||
export type CanvasFailure = 'engine' | 'view'
|
||||
|
||||
const ANNOTATED = '[data-anno-id]'
|
||||
|
||||
/**
|
||||
* 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. Reports the
|
||||
* root's attributes; a failure is the engine notice (retry) or the view notice.
|
||||
*/
|
||||
export function useCanvasBuild(viewId: string) {
|
||||
const preview = useEditorStore((s) => s.preview)
|
||||
const handleVersion = useEditorStore((s) => s.handleVersion)
|
||||
const host = useRef<HTMLDivElement | null>(null)
|
||||
const [attempt, setAttempt] = useState(0)
|
||||
const [root, setRoot] = useState<RootAttributes | null>(null)
|
||||
const [failure, setFailure] = useState<CanvasFailure | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
const element = host.current
|
||||
if (!element || !preview) return
|
||||
const built = buildView(preview, viewId)
|
||||
element.replaceChildren(...(built.ok ? [built.svg] : []))
|
||||
setFailure(built.ok ? null : built.failure)
|
||||
setRoot(
|
||||
built.ok
|
||||
? {
|
||||
viewBox: built.svg.getAttribute('viewBox'),
|
||||
ratio:
|
||||
built.svg.getAttribute('preserveAspectRatio') ?? 'xMidYMid meet',
|
||||
}
|
||||
: null,
|
||||
)
|
||||
}, [preview, viewId, handleVersion, attempt])
|
||||
|
||||
const retry = useCallback(() => {
|
||||
void coreRuntime()
|
||||
.retry()
|
||||
.finally(() => setAttempt((n) => n + 1))
|
||||
}, [])
|
||||
return { host, root, failure, retry, handleVersion }
|
||||
}
|
||||
|
||||
/** 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
|
||||
Reference in new issue
Block a user