110 lines
3.2 KiB
TypeScript
110 lines
3.2 KiB
TypeScript
import type {
|
|
Annotation,
|
|
PreviewState,
|
|
ViewBox,
|
|
} from '@faunosteo/core-wasm/types'
|
|
import { CataloguePreview } from '@/catalogue/preview'
|
|
import { buildView } from '@/components/catalogue/CatalogueCanvas'
|
|
import { coreRuntime } from '@/core/runtime'
|
|
import type { CommitResult } from '@/editor/annotations/annotationCommit'
|
|
import {
|
|
parseFrame,
|
|
type CircleShape,
|
|
type Frame,
|
|
} from '@/editor/annotations/frame'
|
|
import { applyEdit } from '@/editor/core/editOps'
|
|
import {
|
|
geometryAccept,
|
|
geometryErrorsById,
|
|
type GeometryRefusal,
|
|
} from '@/editor/issues'
|
|
import {
|
|
ASSET,
|
|
drawnText,
|
|
edit,
|
|
insert,
|
|
point,
|
|
svgBytes,
|
|
} from './editorFixtures'
|
|
|
|
/** A draft on the Node core: its main drawing's viewBox, optionally with a crop view. */
|
|
export interface Bed {
|
|
preview: CataloguePreview
|
|
text: string
|
|
state: PreviewState
|
|
}
|
|
|
|
export function bed(
|
|
viewBox: string,
|
|
text = drawnText(),
|
|
withDrawing = true,
|
|
): Bed {
|
|
const drawing = svgBytes(viewBox)
|
|
const drawings = withDrawing
|
|
? [{ assetId: ASSET, sha256: drawing.sha256, bytes: drawing.bytes }]
|
|
: []
|
|
const preview = new CataloguePreview(
|
|
coreRuntime(),
|
|
{ status: 'draft', document: text },
|
|
drawings,
|
|
)
|
|
const state = preview.state()
|
|
if (!state.ok) throw new Error(state.kind)
|
|
return { preview, text, state: state.value }
|
|
}
|
|
|
|
export function withCrop(text: string, crop: ViewBox, id = 'view-det'): string {
|
|
return insert(text, {
|
|
kind: 'view',
|
|
value: {
|
|
kind: 'detail',
|
|
id,
|
|
name: { fr: 'D', en: 'D' },
|
|
svg_asset_id: null,
|
|
view_box: crop,
|
|
parent_view_id: 'view-main',
|
|
},
|
|
})
|
|
}
|
|
|
|
/** F as the page reads it: the composed root's `viewBox` text. */
|
|
export function frameOf(b: Bed, viewId = 'view-main'): Frame | null {
|
|
const built = buildView(b.preview, viewId)
|
|
return built.ok ? parseFrame(built.svg.getAttribute('viewBox')) : null
|
|
}
|
|
|
|
/** The insert of `circle` on `viewId` through the core under the geometry `accept`. */
|
|
export function circleInserter(b: Bed, viewId = 'view-main', id = 'anno-new') {
|
|
return (circle: CircleShape): CommitResult => {
|
|
const element = point(id, circle.cx, circle.cy, circle.r, viewId)
|
|
const edited = applyEdit(b.preview.document, { op: 'insert', element })
|
|
if (!edited.ok) return { ok: false, messages: [edited.kind], codes: [] }
|
|
const accept = geometryAccept({ before: b.state, changedIds: [id] })
|
|
const outcome = b.preview.replaceDocument(edited.value, accept)
|
|
if (outcome.ok) return { ok: true }
|
|
if (outcome.reason !== 'refused')
|
|
return { ok: false, messages: ['core'], codes: [] }
|
|
const { issues } = outcome.refusal as GeometryRefusal
|
|
return {
|
|
ok: false,
|
|
messages: issues.map((i) => i.message),
|
|
codes: issues.map((i) => i.code),
|
|
}
|
|
}
|
|
}
|
|
|
|
export function annotationOn(b: Bed, id: string): Annotation | undefined {
|
|
const state = b.preview.state()
|
|
return state.ok
|
|
? state.value.document.annotations.find((a) => a.id === id)
|
|
: undefined
|
|
}
|
|
|
|
export function currentErrors(b: Bed): Map<string, unknown> {
|
|
const state = b.preview.state()
|
|
if (!state.ok) throw new Error(state.kind)
|
|
return geometryErrorsById(state.value)
|
|
}
|
|
|
|
export { drawnText, edit, insert, point }
|