feat(editor): the canvas draws the view the Rust core composed; denylist and anatomy dependency removed
This commit is contained in:
1 parent
43a370aed1
commit
09a6cf7def
11 files changed
+165
-158
No files matched your search
@@ -1,97 +1,20 @@
|
||||
import {
|
||||
createLayerElement,
|
||||
renderAnnotationLayer,
|
||||
viewBoxOf,
|
||||
} from '@faunosteo/anatomy/render'
|
||||
import type { SheetView } from '@faunosteo/core-wasm/types'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
|
||||
/**
|
||||
* Builds the inline SVG of one view: the sheet's drawing (sanitised asset
|
||||
* text) framed on the view's box, plus the annotation layer generated from
|
||||
* the document. The drawing is parsed once per asset and cloned per view.
|
||||
* 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.
|
||||
*/
|
||||
|
||||
const SVG_NS = 'http://www.w3.org/2000/svg'
|
||||
const FORBIDDEN_TAGS = ['script', 'foreignObject', 'iframe', 'object', 'embed']
|
||||
|
||||
export type ViewStates = ReadonlyMap<string, 'current'>
|
||||
|
||||
/** Drops scripts, embedded documents, event handlers and script URLs (LRN-025). */
|
||||
export function sanitizeSvg(root: Element): void {
|
||||
for (const tag of FORBIDDEN_TAGS) {
|
||||
for (const el of Array.from(root.getElementsByTagName(tag))) el.remove()
|
||||
}
|
||||
for (const el of Array.from(root.querySelectorAll('*'))) {
|
||||
for (const attr of Array.from(el.attributes)) {
|
||||
const name = attr.name.toLowerCase()
|
||||
const value = attr.value.trim().toLowerCase()
|
||||
if (name.startsWith('on') || value.startsWith('javascript:')) {
|
||||
el.removeAttribute(attr.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function parseDrawing(svgText: string): SVGSVGElement {
|
||||
const doc = new DOMParser().parseFromString(svgText, 'image/svg+xml')
|
||||
if (doc.querySelector('parsererror')) throw new Error('invalid SVG asset')
|
||||
const root = doc.documentElement
|
||||
if (root.tagName.toLowerCase() !== 'svg')
|
||||
throw new Error('asset root is not <svg>')
|
||||
sanitizeSvg(root)
|
||||
return root as unknown as SVGSVGElement
|
||||
}
|
||||
|
||||
const parsed = new Map<string, SVGSVGElement>()
|
||||
|
||||
function drawingOf(index: SheetIndex, assetId: string): SVGSVGElement {
|
||||
const key = `${index.sheet.ref.documentSha256}:${assetId}`
|
||||
let root = parsed.get(key)
|
||||
if (!root) {
|
||||
const text = index.sheet.assets[assetId]
|
||||
if (text === undefined) throw new Error(`asset ${assetId} not bundled`)
|
||||
root = parseDrawing(text)
|
||||
parsed.set(key, root)
|
||||
}
|
||||
return root
|
||||
}
|
||||
|
||||
/** The asset a view draws: its own, else its parent's (a crop). */
|
||||
export function assetOfView(index: SheetIndex, view: SheetView): string {
|
||||
if (view.svg_asset_id) return view.svg_asset_id
|
||||
if (view.kind === 'detail') {
|
||||
const parent = index.view(view.parent_view_id)
|
||||
if (parent) return assetOfView(index, parent)
|
||||
}
|
||||
throw new Error(`view ${view.id} has no asset`)
|
||||
}
|
||||
|
||||
export function buildViewSvg(
|
||||
index: SheetIndex,
|
||||
viewId: string,
|
||||
states: ViewStates,
|
||||
): SVGSVGElement {
|
||||
const view = index.view(viewId)
|
||||
if (!view) throw new Error(`unknown view ${viewId}`)
|
||||
const source = drawingOf(index, assetOfView(index, view))
|
||||
const svg = document.createElementNS(SVG_NS, 'svg')
|
||||
for (const attr of Array.from(source.attributes)) {
|
||||
if (attr.name === 'width' || attr.name === 'height') continue
|
||||
svg.setAttribute(attr.name, attr.value)
|
||||
}
|
||||
svg.setAttribute(
|
||||
'viewBox',
|
||||
viewBoxOf(view) ?? source.getAttribute('viewBox') ?? '0 0 100 100',
|
||||
)
|
||||
for (const child of Array.from(source.childNodes)) {
|
||||
svg.appendChild(document.importNode(child, true))
|
||||
}
|
||||
const layer = renderAnnotationLayer({
|
||||
document: index.sheet.document,
|
||||
viewId,
|
||||
states,
|
||||
})
|
||||
svg.appendChild(createLayerElement(document, layer) as unknown as Node)
|
||||
return svg
|
||||
export function buildViewSvg(index: SheetIndex, viewId: string): SVGSVGElement | null {
|
||||
const text = index.canvasSvg(viewId)
|
||||
if (!text.ok) return null
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user