68 lines
2.3 KiB
TypeScript
68 lines
2.3 KiB
TypeScript
import type { Annotation, EditCommand, NewElement } from '@faunosteo/core-wasm/types'
|
|
import { emptySheetDocument } from '@/core/sheetDocumentOps'
|
|
import { cleanSvg } from '@/editor/core/cleaning'
|
|
import { applyEdit } from '@/editor/core/editOps'
|
|
|
|
export const ASSET = '11111111-1111-4111-8111-111111111111'
|
|
const NAME = { fr: 'N', en: 'N' }
|
|
|
|
export function svgBytes(viewBox = '0 0 100 100'): {
|
|
bytes: Uint8Array
|
|
sha256: string
|
|
} {
|
|
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="${viewBox}"><rect width="4" height="4"/></svg>`
|
|
const cleaned = cleanSvg(new TextEncoder().encode(svg))
|
|
if (!cleaned.ok || !cleaned.value.stored || !cleaned.value.sha256) {
|
|
throw new Error('uncleanable drawing')
|
|
}
|
|
return { bytes: new TextEncoder().encode(cleaned.value.stored), sha256: cleaned.value.sha256 }
|
|
}
|
|
|
|
export function edit(text: string, command: EditCommand): string {
|
|
const result = applyEdit(text, command)
|
|
if (!result.ok) throw new Error(`${result.kind} ${result.detail ?? ''}`)
|
|
return result.value
|
|
}
|
|
|
|
export function insert(text: string, element: NewElement): string {
|
|
return edit(text, { op: 'insert', element })
|
|
}
|
|
|
|
export function emptyText(name = 'Fiche'): string {
|
|
const empty = emptySheetDocument({ speciesId: 'equine', name: { fr: name, en: name } })
|
|
if (!empty.ok) throw new Error(empty.kind)
|
|
return JSON.stringify(empty.value)
|
|
}
|
|
|
|
export function point(id: string, cx: number, cy: number, r: number, viewId = 'view-main') {
|
|
const value: Annotation = {
|
|
id,
|
|
view_id: viewId,
|
|
category_id: 'cat-a',
|
|
laterality: 'unpaired',
|
|
geometry: { type: 'circle', cx, cy, r },
|
|
review_status: 'to_confirm',
|
|
color: null,
|
|
name: NAME,
|
|
finding_zone_id: null,
|
|
vertebral_region_code: null,
|
|
kind: 'point',
|
|
}
|
|
return { kind: 'annotation', value } satisfies NewElement
|
|
}
|
|
|
|
/** An empty sheet whose main view draws ASSET and with one category `cat-a`. */
|
|
export function drawnText(): string {
|
|
const withCategory = insert(emptyText(), {
|
|
kind: 'category',
|
|
value: { id: 'cat-a', name: NAME, color: null },
|
|
})
|
|
const document = JSON.parse(withCategory)
|
|
const main = document.views.find((v: { id: string }) => v.id === 'view-main')
|
|
return edit(withCategory, {
|
|
op: 'update',
|
|
key: { kind: 'view', id: 'view-main' },
|
|
element: { kind: 'view', value: { ...main, svg_asset_id: ASSET } },
|
|
})
|
|
}
|