feat(canvas): inline SVG load + delegated click + state classes
This commit is contained in:
1 parent
90d398656c
commit
bea3712851
2 files changed
+101
-2
No files matched your search
@@ -1,3 +1,63 @@
|
|||||||
export default function AnatomyCanvas() {
|
import { useEffect, useRef } from 'react'
|
||||||
return <div>AnatomyCanvas placeholder</div>
|
import { loadSvg } from '@/anatomy/svgLoader'
|
||||||
|
import { getBoneIdFromEvent } from '@/anatomy/svgConvention'
|
||||||
|
import type { BoneState } from '@/anatomy/types'
|
||||||
|
|
||||||
|
export interface AnatomyCanvasProps {
|
||||||
|
src: string
|
||||||
|
boneStates: Map<string, BoneState>
|
||||||
|
onZoneClick: (boneId: string) => void
|
||||||
|
className?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function AnatomyCanvas({
|
||||||
|
src,
|
||||||
|
boneStates,
|
||||||
|
onZoneClick,
|
||||||
|
className = '',
|
||||||
|
}: AnatomyCanvasProps) {
|
||||||
|
const containerRef = useRef<HTMLDivElement | null>(null)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let cancelled = false
|
||||||
|
const c = containerRef.current
|
||||||
|
if (!c) return
|
||||||
|
c.replaceChildren()
|
||||||
|
loadSvg(src).then((root) => {
|
||||||
|
if (cancelled || !c) return
|
||||||
|
c.appendChild(root)
|
||||||
|
})
|
||||||
|
return () => { cancelled = true }
|
||||||
|
}, [src])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const c = containerRef.current
|
||||||
|
if (!c) return
|
||||||
|
const onClick = (e: Event) => {
|
||||||
|
const id = getBoneIdFromEvent(e)
|
||||||
|
if (id) onZoneClick(id)
|
||||||
|
}
|
||||||
|
c.addEventListener('click', onClick)
|
||||||
|
return () => c.removeEventListener('click', onClick)
|
||||||
|
}, [onZoneClick])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const c = containerRef.current
|
||||||
|
if (!c) return
|
||||||
|
const all = c.querySelectorAll<Element>('[data-bone-id]')
|
||||||
|
all.forEach((el) => {
|
||||||
|
const id = el.getAttribute('data-bone-id') as string
|
||||||
|
const state = boneStates.get(id)
|
||||||
|
if (state && state !== 'default') el.setAttribute('data-state', state)
|
||||||
|
else el.removeAttribute('data-state')
|
||||||
|
})
|
||||||
|
}, [boneStates])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
data-testid="anatomy-canvas"
|
||||||
|
ref={containerRef}
|
||||||
|
className={`svg-canvas w-full h-full ${className}`}
|
||||||
|
/>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||||
|
import { render, screen, waitFor } from '@testing-library/react'
|
||||||
|
import AnatomyCanvas from '@/components/canvas/AnatomyCanvas'
|
||||||
|
|
||||||
|
vi.mock('@/anatomy/svgLoader', () => ({
|
||||||
|
loadSvg: vi.fn(async () => {
|
||||||
|
const doc = new DOMParser().parseFromString(
|
||||||
|
`<svg xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<circle data-bone-id="epaule" data-bone-fr="Épaule" data-bone-en="Shoulder"
|
||||||
|
data-category="articulation" class="bone osteo articulation hitbox bone-epaule" />
|
||||||
|
<circle data-bone-id="epaule" data-bone-fr="Épaule" data-bone-en="Shoulder"
|
||||||
|
data-category="articulation" class="bone osteo articulation visible bone-epaule" />
|
||||||
|
</svg>`,
|
||||||
|
'image/svg+xml',
|
||||||
|
)
|
||||||
|
return doc.documentElement as unknown as SVGSVGElement
|
||||||
|
}),
|
||||||
|
}))
|
||||||
|
|
||||||
|
beforeEach(() => vi.clearAllMocks())
|
||||||
|
|
||||||
|
describe('AnatomyCanvas', () => {
|
||||||
|
it('renders the loaded SVG inline', async () => {
|
||||||
|
render(<AnatomyCanvas src="/x.svg" boneStates={new Map()} onZoneClick={() => {}} />)
|
||||||
|
await waitFor(() =>
|
||||||
|
expect(document.querySelector('[data-bone-id="epaule"]')).not.toBeNull(),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('calls onZoneClick with the clicked bone id', async () => {
|
||||||
|
const onZoneClick = vi.fn()
|
||||||
|
render(<AnatomyCanvas src="/x.svg" boneStates={new Map()} onZoneClick={onZoneClick} />)
|
||||||
|
await waitFor(() => screen.getByTestId('anatomy-canvas'))
|
||||||
|
;(document.querySelector('[data-bone-id="epaule"]') as Element).dispatchEvent(
|
||||||
|
new Event('click', { bubbles: true }),
|
||||||
|
)
|
||||||
|
await waitFor(() => expect(onZoneClick).toHaveBeenCalledWith('epaule'))
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in new issue
Block a user