feat(editor): locale-aware hover tooltip on anatomy zones
Custom floating tooltip replaces native SVG <title> — follows cursor, shows zone name in active locale (FR/EN), dark pill style with 120ms fade. getAnnotatedElement() helper added to svgConvention.ts. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
7855220d52
commit
569e57197f
3 files changed
+85
-32
No files matched your search
@@ -94,3 +94,12 @@ export function getBoneIdFromEvent(event: Event): string | null {
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export function getAnnotatedElement(event: Event): Element | null {
|
||||
let node = event.target as Element | null
|
||||
while (node && node.nodeType === 1) {
|
||||
if (node.hasAttribute('data-anno-id')) return node
|
||||
node = node.parentElement
|
||||
}
|
||||
return null
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||
import { loadSvg } from '@/anatomy/svgLoader'
|
||||
import { getBoneIdFromEvent } from '@/anatomy/svgConvention'
|
||||
import {
|
||||
getBoneIdFromEvent,
|
||||
getAnnotatedElement,
|
||||
} from '@/anatomy/svgConvention'
|
||||
import type { BoneState } from '@/anatomy/types'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
|
||||
@@ -17,13 +20,15 @@ export default function AnatomyCanvas({
|
||||
onZoneClick,
|
||||
className = '',
|
||||
}: AnatomyCanvasProps) {
|
||||
const containerRef = useRef<HTMLDivElement | null>(null)
|
||||
const wrapperRef = useRef<HTMLDivElement | null>(null)
|
||||
const svgRef = useRef<HTMLDivElement | null>(null)
|
||||
const tooltipRef = useRef<HTMLDivElement | null>(null)
|
||||
const [loadedSrc, setLoadedSrc] = useState<string | null>(null)
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false
|
||||
const c = containerRef.current
|
||||
const c = svgRef.current
|
||||
if (!c) return
|
||||
c.replaceChildren()
|
||||
setLoadedSrc(null)
|
||||
@@ -36,7 +41,7 @@ export default function AnatomyCanvas({
|
||||
}, [src])
|
||||
|
||||
useEffect(() => {
|
||||
const c = containerRef.current
|
||||
const c = svgRef.current
|
||||
if (!c) return
|
||||
const onClick = (e: Event) => {
|
||||
const id = getBoneIdFromEvent(e)
|
||||
@@ -47,7 +52,7 @@ export default function AnatomyCanvas({
|
||||
}, [onZoneClick])
|
||||
|
||||
useEffect(() => {
|
||||
const c = containerRef.current
|
||||
const c = svgRef.current
|
||||
if (!c || loadedSrc !== src) return
|
||||
const all = c.querySelectorAll<Element>('[data-anno-id]')
|
||||
all.forEach((el) => {
|
||||
@@ -58,38 +63,62 @@ export default function AnatomyCanvas({
|
||||
})
|
||||
}, [boneStates, loadedSrc, src])
|
||||
|
||||
// Rewrite native SVG <title> tooltips to match the active locale. The
|
||||
// upstream SVG ships titles as "FR / EN" concatenations; we replace the
|
||||
// text content with just the locale-relevant label so the browser-native
|
||||
// hover tooltip stays in sync with the rest of the UI. The detail-popup
|
||||
// hint ("🔍 Zoom: head") is left intact since it carries an emoji affordance,
|
||||
// not a translatable label.
|
||||
useEffect(() => {
|
||||
const c = containerRef.current
|
||||
if (!c || loadedSrc !== src) return
|
||||
const annotated = c.querySelectorAll<Element>('[data-anno-id]')
|
||||
annotated.forEach((el) => {
|
||||
if (el.hasAttribute('data-anno-opens-detail')) return
|
||||
const showTooltip = useCallback(
|
||||
(el: Element, x: number, y: number) => {
|
||||
const tip = tooltipRef.current
|
||||
if (!tip) return
|
||||
const fr = el.getAttribute('data-anno-name-fr') ?? ''
|
||||
const en = el.getAttribute('data-anno-name-en') ?? ''
|
||||
const preferred = locale === 'en' ? en || fr : fr || en
|
||||
if (!preferred) return
|
||||
let titleEl = el.querySelector(':scope > title')
|
||||
if (!titleEl) {
|
||||
const doc = el.ownerDocument
|
||||
if (!doc) return
|
||||
titleEl = doc.createElementNS('http://www.w3.org/2000/svg', 'title')
|
||||
el.insertBefore(titleEl, el.firstChild)
|
||||
const label = locale === 'en' ? en || fr : fr || en
|
||||
if (!label) { tip.style.opacity = '0'; return }
|
||||
tip.textContent = label
|
||||
tip.style.opacity = '1'
|
||||
tip.style.left = `${x + 12}px`
|
||||
tip.style.top = `${y - 8}px`
|
||||
},
|
||||
[locale],
|
||||
)
|
||||
|
||||
const hideTooltip = useCallback(() => {
|
||||
const tip = tooltipRef.current
|
||||
if (tip) tip.style.opacity = '0'
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
const c = svgRef.current
|
||||
if (!c || loadedSrc !== src) return
|
||||
const onMove = (e: MouseEvent) => {
|
||||
const el = getAnnotatedElement(e)
|
||||
if (el && !el.hasAttribute('data-anno-opens-detail')) {
|
||||
const w = wrapperRef.current
|
||||
if (!w) return
|
||||
const rect = w.getBoundingClientRect()
|
||||
showTooltip(el, e.clientX - rect.left, e.clientY - rect.top)
|
||||
} else {
|
||||
hideTooltip()
|
||||
}
|
||||
titleEl.textContent = preferred
|
||||
})
|
||||
}, [locale, loadedSrc, src])
|
||||
}
|
||||
const onLeave = () => hideTooltip()
|
||||
c.addEventListener('mousemove', onMove)
|
||||
c.addEventListener('mouseleave', onLeave)
|
||||
return () => {
|
||||
c.removeEventListener('mousemove', onMove)
|
||||
c.removeEventListener('mouseleave', onLeave)
|
||||
}
|
||||
}, [loadedSrc, src, showTooltip, hideTooltip])
|
||||
|
||||
return (
|
||||
<div
|
||||
data-testid="anatomy-canvas"
|
||||
ref={containerRef}
|
||||
className={`svg-canvas w-full h-full ${className}`}
|
||||
/>
|
||||
ref={wrapperRef}
|
||||
className={`svg-canvas relative w-full h-full ${className}`}
|
||||
>
|
||||
<div ref={svgRef} className="w-full h-full" />
|
||||
<div
|
||||
ref={tooltipRef}
|
||||
className="zone-tooltip"
|
||||
style={{ opacity: 0 }}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -14,3 +14,18 @@ body {
|
||||
.svg-canvas [data-state='multi'] { fill: var(--color-accent) !important; fill-opacity: 0.65 !important; stroke: var(--color-ink); stroke-width: 1.5; }
|
||||
.svg-canvas [data-anno-id] { cursor: pointer; }
|
||||
.svg-canvas [data-anno-id]:hover { filter: brightness(1.05); }
|
||||
|
||||
.zone-tooltip {
|
||||
position: absolute;
|
||||
pointer-events: none;
|
||||
padding: 4px 10px;
|
||||
border-radius: 6px;
|
||||
font-size: 13px;
|
||||
font-weight: 500;
|
||||
white-space: nowrap;
|
||||
background: var(--color-ink);
|
||||
color: var(--color-bg);
|
||||
box-shadow: 0 2px 8px rgba(0,0,0,.18);
|
||||
transition: opacity 120ms ease;
|
||||
z-index: 50;
|
||||
}
|
||||
Reference in new issue
Block a user