feat(anatomy): parse SVG convention into Bone map with validation
This commit is contained in:
1 parent
3dc7153361
commit
ac50ac5c2c
2 files changed
+156
-29
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { SvgConventionError } from './types'
|
||||
import type { Bone, BoneCategory } from './types'
|
||||
|
||||
const VALID_CATEGORIES: BoneCategory[] = [
|
||||
const VALID_CATEGORIES: readonly BoneCategory[] = [
|
||||
'articulation',
|
||||
'charniere',
|
||||
'spine-zone',
|
||||
@@ -10,33 +11,74 @@ const VALID_CATEGORIES: BoneCategory[] = [
|
||||
'cadran-crane',
|
||||
]
|
||||
|
||||
/**
|
||||
* Parses an inline SVG document and returns a map of boneId → Bone.
|
||||
* Throws SvgConventionError if a required attribute is missing or invalid.
|
||||
*/
|
||||
export function parseSvgBones(_svgRoot: SVGSVGElement): Map<string, Bone> {
|
||||
throw new Error('not implemented')
|
||||
}
|
||||
|
||||
/**
|
||||
* Walks up from event.target to find the nearest element with data-bone-id.
|
||||
* Returns null if no bone element is found.
|
||||
*/
|
||||
export function getBoneIdFromEvent(_event: Event): string | null {
|
||||
throw new Error('not implemented')
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates that all required data attributes are present on an element.
|
||||
* Throws SvgConventionError on the first missing attribute.
|
||||
*/
|
||||
export function validateBoneElement(_el: Element): void {
|
||||
throw new Error('not implemented')
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the category string is a valid BoneCategory.
|
||||
*/
|
||||
export function isValidCategory(value: string): value is BoneCategory {
|
||||
return (VALID_CATEGORIES as string[]).includes(value)
|
||||
return (VALID_CATEGORIES as readonly string[]).includes(value)
|
||||
}
|
||||
|
||||
function classList(el: Element): string[] {
|
||||
return (el.getAttribute('class') ?? '').split(/\s+/).filter(Boolean)
|
||||
}
|
||||
|
||||
export function validateBoneElement(el: Element): void {
|
||||
const boneId = el.getAttribute('data-bone-id') ?? ''
|
||||
for (const attr of ['data-bone-fr', 'data-bone-en', 'data-category']) {
|
||||
if (!el.getAttribute(attr)) {
|
||||
throw new SvgConventionError(
|
||||
`Element data-bone-id="${boneId}" missing attribute ${attr}`,
|
||||
boneId,
|
||||
attr,
|
||||
)
|
||||
}
|
||||
}
|
||||
const cat = el.getAttribute('data-category') ?? ''
|
||||
if (!isValidCategory(cat)) {
|
||||
throw new SvgConventionError(
|
||||
`Element data-bone-id="${boneId}" has unknown data-category="${cat}"`,
|
||||
boneId,
|
||||
'data-category',
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export function parseSvgBones(svgRoot: SVGSVGElement): Map<string, Bone> {
|
||||
const map = new Map<string, Bone>()
|
||||
const all = svgRoot.querySelectorAll<Element>('[data-bone-id]')
|
||||
all.forEach((el) => {
|
||||
validateBoneElement(el)
|
||||
const id = el.getAttribute('data-bone-id') as string
|
||||
const fr = el.getAttribute('data-bone-fr') as string
|
||||
const en = el.getAttribute('data-bone-en') as string
|
||||
const category = el.getAttribute('data-category') as BoneCategory
|
||||
const opensDetail = el.getAttribute('data-opens-detail') ?? undefined
|
||||
const classes = classList(el)
|
||||
const isHitbox = classes.includes('hitbox')
|
||||
|
||||
const existing = map.get(id)
|
||||
if (existing) {
|
||||
if (isHitbox) existing.hitboxEls.push(el)
|
||||
else existing.visibleEls.push(el)
|
||||
if (opensDetail && !existing.opensDetail) existing.opensDetail = opensDetail
|
||||
} else {
|
||||
map.set(id, {
|
||||
id,
|
||||
fr,
|
||||
en,
|
||||
category,
|
||||
opensDetail,
|
||||
hitboxEls: isHitbox ? [el] : [],
|
||||
visibleEls: isHitbox ? [] : [el],
|
||||
})
|
||||
}
|
||||
})
|
||||
return map
|
||||
}
|
||||
|
||||
export function getBoneIdFromEvent(event: Event): string | null {
|
||||
let node = event.target as Element | null
|
||||
while (node && node.nodeType === 1) {
|
||||
const id = node.getAttribute?.('data-bone-id')
|
||||
if (id) return id
|
||||
node = node.parentElement
|
||||
}
|
||||
return null
|
||||
}
|
||||
Reference in new issue
Block a user