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
|
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 { loadSvg } from '@/anatomy/svgLoader'
|
||||||
import { getBoneIdFromEvent } from '@/anatomy/svgConvention'
|
import {
|
||||||
|
getBoneIdFromEvent,
|
||||||
|
getAnnotatedElement,
|
||||||
|
} from '@/anatomy/svgConvention'
|
||||||
import type { BoneState } from '@/anatomy/types'
|
import type { BoneState } from '@/anatomy/types'
|
||||||
import { useLocaleStore } from '@/state/localeStore'
|
import { useLocaleStore } from '@/state/localeStore'
|
||||||
|
|
||||||
@@ -17,13 +20,15 @@ export default function AnatomyCanvas({
|
|||||||
onZoneClick,
|
onZoneClick,
|
||||||
className = '',
|
className = '',
|
||||||
}: AnatomyCanvasProps) {
|
}: 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 [loadedSrc, setLoadedSrc] = useState<string | null>(null)
|
||||||
const locale = useLocaleStore((s) => s.locale)
|
const locale = useLocaleStore((s) => s.locale)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let cancelled = false
|
let cancelled = false
|
||||||
const c = containerRef.current
|
const c = svgRef.current
|
||||||
if (!c) return
|
if (!c) return
|
||||||
c.replaceChildren()
|
c.replaceChildren()
|
||||||
setLoadedSrc(null)
|
setLoadedSrc(null)
|
||||||
@@ -36,7 +41,7 @@ export default function AnatomyCanvas({
|
|||||||
}, [src])
|
}, [src])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const c = containerRef.current
|
const c = svgRef.current
|
||||||
if (!c) return
|
if (!c) return
|
||||||
const onClick = (e: Event) => {
|
const onClick = (e: Event) => {
|
||||||
const id = getBoneIdFromEvent(e)
|
const id = getBoneIdFromEvent(e)
|
||||||
@@ -47,7 +52,7 @@ export default function AnatomyCanvas({
|
|||||||
}, [onZoneClick])
|
}, [onZoneClick])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const c = containerRef.current
|
const c = svgRef.current
|
||||||
if (!c || loadedSrc !== src) return
|
if (!c || loadedSrc !== src) return
|
||||||
const all = c.querySelectorAll<Element>('[data-anno-id]')
|
const all = c.querySelectorAll<Element>('[data-anno-id]')
|
||||||
all.forEach((el) => {
|
all.forEach((el) => {
|
||||||
@@ -58,38 +63,62 @@ export default function AnatomyCanvas({
|
|||||||
})
|
})
|
||||||
}, [boneStates, loadedSrc, src])
|
}, [boneStates, loadedSrc, src])
|
||||||
|
|
||||||
// Rewrite native SVG <title> tooltips to match the active locale. The
|
const showTooltip = useCallback(
|
||||||
// upstream SVG ships titles as "FR / EN" concatenations; we replace the
|
(el: Element, x: number, y: number) => {
|
||||||
// text content with just the locale-relevant label so the browser-native
|
const tip = tooltipRef.current
|
||||||
// hover tooltip stays in sync with the rest of the UI. The detail-popup
|
if (!tip) return
|
||||||
// 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 fr = el.getAttribute('data-anno-name-fr') ?? ''
|
const fr = el.getAttribute('data-anno-name-fr') ?? ''
|
||||||
const en = el.getAttribute('data-anno-name-en') ?? ''
|
const en = el.getAttribute('data-anno-name-en') ?? ''
|
||||||
const preferred = locale === 'en' ? en || fr : fr || en
|
const label = locale === 'en' ? en || fr : fr || en
|
||||||
if (!preferred) return
|
if (!label) { tip.style.opacity = '0'; return }
|
||||||
let titleEl = el.querySelector(':scope > title')
|
tip.textContent = label
|
||||||
if (!titleEl) {
|
tip.style.opacity = '1'
|
||||||
const doc = el.ownerDocument
|
tip.style.left = `${x + 12}px`
|
||||||
if (!doc) return
|
tip.style.top = `${y - 8}px`
|
||||||
titleEl = doc.createElementNS('http://www.w3.org/2000/svg', 'title')
|
},
|
||||||
el.insertBefore(titleEl, el.firstChild)
|
[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
|
}
|
||||||
})
|
const onLeave = () => hideTooltip()
|
||||||
}, [locale, loadedSrc, src])
|
c.addEventListener('mousemove', onMove)
|
||||||
|
c.addEventListener('mouseleave', onLeave)
|
||||||
|
return () => {
|
||||||
|
c.removeEventListener('mousemove', onMove)
|
||||||
|
c.removeEventListener('mouseleave', onLeave)
|
||||||
|
}
|
||||||
|
}, [loadedSrc, src, showTooltip, hideTooltip])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
data-testid="anatomy-canvas"
|
data-testid="anatomy-canvas"
|
||||||
ref={containerRef}
|
ref={wrapperRef}
|
||||||
className={`svg-canvas w-full h-full ${className}`}
|
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-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] { cursor: pointer; }
|
||||||
.svg-canvas [data-anno-id]:hover { filter: brightness(1.05); }
|
.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