feat(editor): the canvas tells an engine failure (retry) from a view it cannot draw (no retry)
This commit is contained in:
1 parent
6882c6da4a
commit
c0a5c8c7ce
8 files changed
+182
-36
No files matched your search
@@ -4,17 +4,36 @@ 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. `null` when the core or the parse
|
||||
* fails: the canvas shows its degraded notice.
|
||||
* runs scripts) and imports the tree into the page.
|
||||
*/
|
||||
|
||||
export type ViewStates = ReadonlyMap<string, 'current'>
|
||||
|
||||
export function buildViewSvg(index: SheetIndex, viewId: string): SVGSVGElement | null {
|
||||
/**
|
||||
* Why a view cannot be drawn: the engine did not run (a retry may succeed), 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' | '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) return null
|
||||
if (!text.ok) {
|
||||
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 null
|
||||
return document.importNode(root, true) as unknown as SVGSVGElement
|
||||
if (parsed.querySelector('parsererror') || root.localName !== 'svg') {
|
||||
return failed('view', 'parse')
|
||||
}
|
||||
return { ok: true, svg: document.importNode(root, true) as unknown as SVGSVGElement }
|
||||
}
|
||||
Reference in new issue
Block a user