43 lines
1.8 KiB
TypeScript
43 lines
1.8 KiB
TypeScript
import { coreStopped } from '@/core/runtime'
|
|
import type { SheetIndex } from '@/services/sheetIndex'
|
|
|
|
/**
|
|
* The inline SVG of one view, as the core composed it (R3): the sheet's drawing, read and
|
|
* sanitised by the core's allowlist at load, framed on the view, with the annotation layer
|
|
* last (docs/ANNOTATION-RENDERING.md). The browser only parses the text (`DOMParser` never
|
|
* runs scripts) and imports the tree into the page.
|
|
*/
|
|
|
|
export type ViewStates = ReadonlyMap<string, 'current'>
|
|
|
|
/**
|
|
* Why a view cannot be drawn: the engine did not run (a retry may succeed), the engine
|
|
* stopped after its three recoveries (only a reload starts it again), or the view
|
|
* itself cannot be drawn (a refused drawing, a view the core cannot compose: retrying
|
|
* changes nothing). The technical kind stays in the core's outcome and the DEV console.
|
|
*/
|
|
export type ViewFailure = 'engine' | 'engine_stopped' | 'view'
|
|
|
|
export type ViewBuild = { ok: true; svg: SVGSVGElement } | { ok: false; failure: ViewFailure }
|
|
|
|
const ENGINE_KINDS: ReadonlySet<string> = new Set(['core_unavailable', 'internal'])
|
|
|
|
function failed(failure: ViewFailure, kind: string, detail?: string): ViewBuild {
|
|
if (import.meta.env.DEV) console.warn(`canvas: ${kind}`, detail ?? '')
|
|
return { ok: false, failure }
|
|
}
|
|
|
|
export function buildViewSvg(index: SheetIndex, viewId: string): ViewBuild {
|
|
const text = index.canvasSvg(viewId)
|
|
if (!text.ok) {
|
|
const engine = coreStopped() ? 'engine_stopped' : 'engine'
|
|
return failed(ENGINE_KINDS.has(text.kind) ? engine : 'view', text.kind, text.detail)
|
|
}
|
|
const parsed = new DOMParser().parseFromString(text.value, 'image/svg+xml')
|
|
const root = parsed.documentElement
|
|
if (parsed.querySelector('parsererror') || root.localName !== 'svg') {
|
|
return failed('view', 'parse')
|
|
}
|
|
return { ok: true, svg: document.importNode(root, true) as unknown as SVGSVGElement }
|
|
}
|