159 lines
5.1 KiB
TypeScript
159 lines
5.1 KiB
TypeScript
import { DOMParser } from '@xmldom/xmldom'
|
|
import { beforeAll, describe, expect, it } from 'vitest'
|
|
import {
|
|
createLayerElement,
|
|
renderAnnotationLayer,
|
|
serializeLayer,
|
|
styleForView,
|
|
type DraftResult,
|
|
type LayerElement,
|
|
} from '../src'
|
|
import { miniSheet } from './fixtures/mini-sheet'
|
|
import { runDraft } from './helpers'
|
|
|
|
function flatten(node: LayerElement): LayerElement[] {
|
|
return [node, ...node.children.flatMap(flatten)]
|
|
}
|
|
|
|
function byId(layer: LayerElement, id: string): LayerElement[] {
|
|
return flatten(layer).filter((n) => n.attrs['data-anno-id'] === id)
|
|
}
|
|
|
|
describe('annotation layer on the real draft', () => {
|
|
let draft: DraftResult
|
|
beforeAll(async () => {
|
|
;({ draft } = await runDraft())
|
|
})
|
|
|
|
it('draws every annotation of the main view as the legacy pair, zone or trigger', () => {
|
|
const layer = renderAnnotationLayer({
|
|
document: draft.document,
|
|
viewId: 'main',
|
|
})
|
|
expect(layer.attrs).toEqual({
|
|
'data-layer': 'annotations',
|
|
'data-view-id': 'main',
|
|
})
|
|
const main = draft.document.annotations.filter((a) => a.view_id === 'main')
|
|
for (const a of main) {
|
|
const els = byId(layer, a.id)
|
|
if (a.kind === 'point')
|
|
expect(els.map((e) => e.tag)).toEqual(['circle', 'circle'])
|
|
else expect(els).toHaveLength(1)
|
|
}
|
|
const [hitbox, visible] = byId(layer, 'hip')
|
|
expect(hitbox!.attrs).toMatchObject({
|
|
r: '34',
|
|
fill: 'transparent',
|
|
'pointer-events': 'all',
|
|
'data-anno-lateral': 'bilateral',
|
|
'data-anno-zone': 'hip',
|
|
})
|
|
expect(visible!.attrs).toMatchObject({
|
|
r: '22',
|
|
fill: '#ff9500',
|
|
'fill-opacity': '0.65',
|
|
stroke: '#000',
|
|
'stroke-width': '1.5',
|
|
'pointer-events': 'none',
|
|
})
|
|
expect(hitbox!.children[0]).toMatchObject({
|
|
tag: 'title',
|
|
text: 'Hanche (coxo-fémorale) / Hip joint',
|
|
})
|
|
const [spine] = byId(layer, 'cervical')
|
|
expect(spine!.tag).toBe('polygon')
|
|
expect(spine!.attrs).toMatchObject({
|
|
fill: '#e100ff',
|
|
'fill-opacity': '0.3',
|
|
'stroke-width': '2',
|
|
'data-anno-region': 'C',
|
|
})
|
|
const [trigger] = byId(layer, 'head-trigger')
|
|
expect(trigger!.attrs).toMatchObject({
|
|
'data-anno-kind': 'detail_trigger',
|
|
'data-anno-opens-view': 'head',
|
|
'stroke-dasharray': '8,4',
|
|
'fill-opacity': '0.06',
|
|
class: 'detail-zone',
|
|
})
|
|
expect(trigger!.attrs['data-anno-name-fr']).toBe(
|
|
'Tête (vue latérale gauche)',
|
|
)
|
|
})
|
|
|
|
it('uses the detail style, the annotation colour override and the view box on the head view', () => {
|
|
const layer = renderAnnotationLayer({
|
|
document: draft.document,
|
|
viewId: 'head',
|
|
})
|
|
const [c0c1Hit, c0c1Vis] = byId(layer, 'c0-c1')
|
|
expect(c0c1Hit!.attrs.r).toBe('27')
|
|
expect(c0c1Vis!.attrs).toMatchObject({
|
|
r: '15',
|
|
fill: '#ff0000',
|
|
'fill-opacity': '0.5',
|
|
})
|
|
expect(byId(layer, 'atm')[1]!.attrs.fill).toBe('#ff8800')
|
|
expect(byId(layer, 'hyoid')[1]!.attrs.fill).toBe('#d507c4')
|
|
expect(byId(layer, 'parietal')[1]!.attrs.fill).toBe('#1baa08')
|
|
expect(styleForView(draft.document.views[1]!).visibleFillOpacity).toBe(0.5)
|
|
expect(flatten(layer).some((n) => n.attrs['data-anno-id'] === 'hip')).toBe(
|
|
false,
|
|
)
|
|
})
|
|
|
|
it('applies states as data-state on both elements of a point', () => {
|
|
const states = new Map([['hip', 'current' as const]])
|
|
const layer = renderAnnotationLayer({
|
|
document: draft.document,
|
|
viewId: 'main',
|
|
states,
|
|
})
|
|
expect(
|
|
byId(layer, 'hip').every((e) => e.attrs['data-state'] === 'current'),
|
|
).toBe(true)
|
|
expect(
|
|
byId(layer, 'stifle').every((e) => e.attrs['data-state'] === undefined),
|
|
).toBe(true)
|
|
})
|
|
|
|
it('materialises through a DOM and serialises to the same markup', () => {
|
|
const layer = renderAnnotationLayer({
|
|
document: draft.document,
|
|
viewId: 'head',
|
|
})
|
|
const doc = new DOMParser().parseFromString(
|
|
'<svg xmlns="http://www.w3.org/2000/svg"/>',
|
|
'image/svg+xml',
|
|
)
|
|
const element = createLayerElement(doc as never, layer) as unknown as {
|
|
getAttribute(name: string): string | null
|
|
getElementsByTagName(tag: string): { length: number }
|
|
}
|
|
expect(element.getAttribute('data-layer')).toBe('annotations')
|
|
expect(element.getElementsByTagName('circle')).toHaveLength(18)
|
|
const text = serializeLayer(layer)
|
|
expect(text).toContain('<g data-layer="annotations" data-view-id="head">')
|
|
expect(text).toContain('<title>Appareil hyoïdien / Hyoid apparatus</title>')
|
|
expect(text).not.toContain('<script')
|
|
})
|
|
})
|
|
|
|
describe('annotation layer edge cases', () => {
|
|
it('skips annotations without geometry and falls back to grey without any colour', () => {
|
|
const sheet = miniSheet()
|
|
const layer = renderAnnotationLayer({ document: sheet, viewId: 'main' })
|
|
const ids = new Set(
|
|
flatten(layer)
|
|
.map((n) => n.attrs['data-anno-id'])
|
|
.filter(Boolean),
|
|
)
|
|
expect(ids.has('hip')).toBe(true)
|
|
expect(byId(layer, 'hip')[1]!.attrs.fill).toBe('#888888')
|
|
expect(() =>
|
|
renderAnnotationLayer({ document: sheet, viewId: 'nope' }),
|
|
).toThrow('unknown view')
|
|
})
|
|
})
|