feat(i18n): SVG editor header navbar + full FR/EN translation

Compact header (Brand + LanguageSwitcher) above the SVG editor.
All 58 hardcoded strings in JSX template and vanilla JS replaced with
t() calls via TFn passed to initSvgEditor. Editor re-inits on locale
change. CSS drop overlay uses data-drop-label attr for translated text.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Bastien ChanotandClaude committed 2026-05-22 06:16:49 +02:00
1 parent 5e24c2f3d7
commit 1673302b09
2 files changed
+246 -201

No files matched your search

+243 -199
View File
@@ -1,11 +1,23 @@
import { useEffect, useRef } from 'react' import { useEffect, useRef } from 'react'
import { Link } from 'react-router-dom' import { Link } from 'react-router-dom'
import Brand from '@/components/brand/Brand'
import LanguageSwitcher from '@/components/brand/LanguageSwitcher'
import { useConfig } from '@/app/providers/ConfigProvider'
import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
import { getDict } from '@/i18n/index'
import '@/styles/svg-editor.css' import '@/styles/svg-editor.css'
type TFn = (key: string) => string
export default function SvgEditorRoute() { export default function SvgEditorRoute() {
const canvasRef = useRef<HTMLDivElement>(null) const canvasRef = useRef<HTMLDivElement>(null)
const canvasInnerRef = useRef<HTMLDivElement>(null) const canvasInnerRef = useRef<HTMLDivElement>(null)
const sidebarRef = useRef<HTMLDivElement>(null) const sidebarRef = useRef<HTMLDivElement>(null)
const cleanupRef = useRef<(() => void) | null>(null)
const cfg = useConfig()
const t = useT()
const locale = useLocaleStore((s) => s.locale)
useEffect(() => { useEffect(() => {
const meta = document.createElement('meta') const meta = document.createElement('meta')
@@ -23,179 +35,204 @@ export default function SvgEditorRoute() {
const sidebar = sidebarRef.current const sidebar = sidebarRef.current
if (!canvas || !canvasInner || !sidebar) return if (!canvas || !canvasInner || !sidebar) return
const cleanup = initSvgEditor(canvas, canvasInner, sidebar) const dict = getDict(locale)
const tFn: TFn = (key: string) => dict[key] ?? key
const cleanup = initSvgEditor(canvas, canvasInner, sidebar, tFn)
cleanupRef.current = cleanup
return cleanup return () => {
}, []) cleanup()
cleanupRef.current = null
}
// Re-init when locale changes so all vanilla JS strings update
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [locale])
return ( return (
<div className="svg-editor-root"> <div className="flex flex-col h-screen">
<div ref={canvasRef} className="svg-editor-canvas mode-select"> <header className="px-4 py-2 border-b border-[var(--color-border)] flex items-center justify-between shrink-0">
<div ref={canvasInnerRef} className="svg-editor-canvas-inner" /> <div className="flex items-baseline gap-3">
<div className="svg-editor-zoom-controls" id="svg-editor-zoom-controls" /> <Link to="/" className="no-underline">
</div> <Brand size="sm" />
</Link>
<span className="text-xs text-[var(--color-ink-muted)]">{cfg.tagline}</span>
</div>
<LanguageSwitcher />
</header>
<div ref={sidebarRef} className="svg-editor-sidebar"> <div className="svg-editor-root">
<header> <div
<div className="flex items-center gap-3"> ref={canvasRef}
<Link className="svg-editor-canvas mode-select"
to="/" data-drop-label={t('svg_editor.drop_svg_here')}
className="text-white/80 hover:text-white transition text-sm" >
style={{ textDecoration: 'none' }} <div ref={canvasInnerRef} className="svg-editor-canvas-inner" />
> <div className="svg-editor-zoom-controls" id="svg-editor-zoom-controls" />
&larr; </div>
</Link>
<div> <div ref={sidebarRef} className="svg-editor-sidebar">
<h1>SVG Anatomy Editor</h1> <header>
<div className="sub">Points, zones &amp; clickable categories</div> <div className="flex items-center gap-3">
<Link
to="/"
className="text-white/80 hover:text-white transition text-sm"
style={{ textDecoration: 'none' }}
>
&larr;
</Link>
<div>
<h1>{t('svg_editor.title')}</h1>
<div className="sub">{t('svg_editor.subtitle')}</div>
</div>
</div> </div>
</div> </header>
</header>
<div className="svg-editor-sidebar-content"> <div className="svg-editor-sidebar-content">
<div className="section"> <div className="section">
<h3>File</h3> <h3>{t('svg_editor.file')}</h3>
<input type="file" id="svg-editor-file-import" accept=".svg" style={{ display: 'none' }} /> <input type="file" id="svg-editor-file-import" accept=".svg" style={{ display: 'none' }} />
<button className="primary" id="svg-editor-file-btn"> <button className="primary" id="svg-editor-file-btn">
Choose an SVG file {t('svg_editor.choose_file')}
</button>
<button className="primary" id="svg-editor-export-btn">
Export SVG
</button>
</div>
<div className="section">
<h3>Mode</h3>
<button data-mode="select" className="active" id="svg-editor-mode-select">
☝ Select / move
</button>
<button data-mode="point" id="svg-editor-mode-point">
● Add point
</button>
<button data-mode="polygon" id="svg-editor-mode-polygon">
▬ Add polygon
</button>
<div id="svg-editor-polygon-controls" style={{ display: 'none' }}>
<button className="primary" id="svg-editor-finalize-polygon">
Validate polygon (Enter)
</button> </button>
<button className="danger" id="svg-editor-cancel-polygon"> <button className="primary" id="svg-editor-export-btn">
Cancel (Esc) {t('svg_editor.export')}
</button> </button>
</div> </div>
<label>Category for new elements</label>
<select id="svg-editor-default-category" />
</div>
<div className="section"> <div className="section">
<h3> <h3>{t('svg_editor.mode')}</h3>
Categories <span style={{ color: 'var(--color-ink-muted)' }}>(shared color)</span> <button data-mode="select" className="active" id="svg-editor-mode-select">
</h3> &#9757; {t('svg_editor.mode_select')}
<div id="svg-editor-category-list" />
<div className="add-cat">
<input type="text" id="svg-editor-new-cat-name" placeholder="Category name" />
<input type="color" id="svg-editor-new-cat-color" defaultValue="#00aaff" />
<button className="primary" id="svg-editor-add-cat-btn">
+
</button> </button>
</div> <button data-mode="point" id="svg-editor-mode-point">
</div> &#9679; {t('svg_editor.mode_point')}
</button>
<div className="section" id="svg-editor-edit-section" style={{ display: 'none' }}> <button data-mode="polygon" id="svg-editor-mode-polygon">
<h3>Selected element</h3> &#9644; {t('svg_editor.mode_polygon')}
<div className="coords" id="svg-editor-coords-display"> </button>
&mdash; <div id="svg-editor-polygon-controls" style={{ display: 'none' }}>
<button className="primary" id="svg-editor-finalize-polygon">
{t('svg_editor.validate_polygon')}
</button>
<button className="danger" id="svg-editor-cancel-polygon">
{t('svg_editor.cancel_polygon')}
</button>
</div>
<label>{t('svg_editor.category_for_new')}</label>
<select id="svg-editor-default-category" />
</div> </div>
<div className="detail-toggle" id="svg-editor-detail-toggle-block"> <div className="section">
<label style={{ display: 'flex', alignItems: 'center', gap: '6px', cursor: 'pointer' }}> <h3>
<input {t('svg_editor.categories')}{' '}
type="checkbox" <span style={{ color: 'var(--color-ink-muted)' }}>{t('svg_editor.shared_color')}</span>
id="svg-editor-edit-opens-detail" </h3>
style={{ margin: 0, width: 'auto' }} <div id="svg-editor-category-list" />
/> <div className="add-cat">
<span>This zone opens a detail view</span> <input type="text" id="svg-editor-new-cat-name" placeholder={t('svg_editor.category_placeholder')} />
</label> <input type="color" id="svg-editor-new-cat-color" defaultValue="#00aaff" />
<div id="svg-editor-detail-target-row" style={{ display: 'none', marginTop: '8px' }}> <button className="primary" id="svg-editor-add-cat-btn">
<label>Detail view ID (e.g. head, front-leg)</label> +
<input id="svg-editor-edit-detail-target" placeholder="head" /> </button>
</div> </div>
</div> </div>
<div id="svg-editor-osteo-fields"> <div className="section" id="svg-editor-edit-section" style={{ display: 'none' }}>
<label>French name</label> <h3>{t('svg_editor.selected_element')}</h3>
<input id="svg-editor-edit-fr" placeholder="ex: Hanche" /> <div className="coords" id="svg-editor-coords-display">
<label>English name</label> &mdash;
<input id="svg-editor-edit-en" placeholder="ex: Hip joint" />
<label>Category</label>
<select id="svg-editor-edit-category" />
<label id="svg-editor-lbl-radius">Radius (circle)</label>
<div className="slider-row" id="svg-editor-radius-row">
<input id="svg-editor-edit-radius" type="range" min={1} max={80} defaultValue={15} />
<input
id="svg-editor-edit-radius-num"
type="number"
min={1}
max={200}
defaultValue={15}
/>
</div> </div>
</div>
<div id="svg-editor-opacity-block">
<label>Fill opacity</label>
<div className="slider-row">
<input
id="svg-editor-edit-opacity"
type="range"
min={0}
max={100}
defaultValue={65}
/>
<input
id="svg-editor-edit-opacity-num"
type="number"
min={0}
max={100}
step={1}
defaultValue={65}
/>
</div>
</div>
<div
id="svg-editor-polygon-edit-actions"
style={{
display: 'none',
marginTop: '12px',
padding: '10px',
background: '#fff8e1',
borderRadius: '6px',
border: '1px solid #ffd54f',
}}
>
<div style={{ fontSize: '11px', color: '#92400e', marginBottom: '6px' }}>
<strong>Polygon editing:</strong>
<br />
Drag a yellow handle to move it
<br />
Click a white mid-handle to insert a vertex
<br />
Alt+click or right-click a yellow handle to delete it
</div>
<button id="svg-editor-restart-polygon">Redraw this polygon</button>
</div>
<div style={{ marginTop: '12px' }}>
<button className="danger" id="svg-editor-delete-btn">
Delete
</button>
</div>
</div>
<div className="section"> <div className="detail-toggle" id="svg-editor-detail-toggle-block">
<h3> <label style={{ display: 'flex', alignItems: 'center', gap: '6px', cursor: 'pointer' }}>
Elements (<span id="svg-editor-count">0</span>) <input
</h3> type="checkbox"
<input id="svg-editor-filter-input" placeholder="Filter..." /> id="svg-editor-edit-opens-detail"
<div className="element-list" id="svg-editor-element-list" /> style={{ margin: 0, width: 'auto' }}
/>
<span>{t('svg_editor.opens_detail')}</span>
</label>
<div id="svg-editor-detail-target-row" style={{ display: 'none', marginTop: '8px' }}>
<label>{t('svg_editor.detail_id_label')}</label>
<input id="svg-editor-edit-detail-target" placeholder="head" />
</div>
</div>
<div id="svg-editor-osteo-fields">
<label>{t('svg_editor.name_fr')}</label>
<input id="svg-editor-edit-fr" placeholder="ex: Hanche" />
<label>{t('svg_editor.name_en')}</label>
<input id="svg-editor-edit-en" placeholder="ex: Hip joint" />
<label>{t('svg_editor.category')}</label>
<select id="svg-editor-edit-category" />
<label id="svg-editor-lbl-radius">{t('svg_editor.radius')}</label>
<div className="slider-row" id="svg-editor-radius-row">
<input id="svg-editor-edit-radius" type="range" min={1} max={80} defaultValue={15} />
<input
id="svg-editor-edit-radius-num"
type="number"
min={1}
max={200}
defaultValue={15}
/>
</div>
</div>
<div id="svg-editor-opacity-block">
<label>{t('svg_editor.fill_opacity')}</label>
<div className="slider-row">
<input
id="svg-editor-edit-opacity"
type="range"
min={0}
max={100}
defaultValue={65}
/>
<input
id="svg-editor-edit-opacity-num"
type="number"
min={0}
max={100}
step={1}
defaultValue={65}
/>
</div>
</div>
<div
id="svg-editor-polygon-edit-actions"
style={{
display: 'none',
marginTop: '12px',
padding: '10px',
background: '#fff8e1',
borderRadius: '6px',
border: '1px solid #ffd54f',
}}
>
<div style={{ fontSize: '11px', color: '#92400e', marginBottom: '6px' }}>
<strong>{t('svg_editor.polygon_editing')}</strong>
<br />
{t('svg_editor.polygon_drag')}
<br />
{t('svg_editor.polygon_insert')}
<br />
{t('svg_editor.polygon_delete')}
</div>
<button id="svg-editor-restart-polygon">{t('svg_editor.redraw_polygon')}</button>
</div>
<div style={{ marginTop: '12px' }}>
<button className="danger" id="svg-editor-delete-btn">
{t('svg_editor.delete')}
</button>
</div>
</div>
<div className="section">
<h3>
{t('svg_editor.elements')} (<span id="svg-editor-count">0</span>)
</h3>
<input id="svg-editor-filter-input" placeholder={t('svg_editor.filter_placeholder')} />
<div className="element-list" id="svg-editor-element-list" />
</div>
</div> </div>
</div> </div>
</div> </div>
@@ -207,6 +244,7 @@ function initSvgEditor(
canvas: HTMLDivElement, canvas: HTMLDivElement,
canvasInner: HTMLDivElement, canvasInner: HTMLDivElement,
_sidebar: HTMLDivElement, _sidebar: HTMLDivElement,
t: TFn,
): () => void { ): () => void {
const ac = new AbortController() const ac = new AbortController()
const sig = { signal: ac.signal } const sig = { signal: ac.signal }
@@ -232,8 +270,8 @@ function initSvgEditor(
const CATEGORY_TEMPLATES: Record<string, { color: string; name: string }> = { const CATEGORY_TEMPLATES: Record<string, { color: string; name: string }> = {
articulation: { color: '#fb5607', name: 'Articulations' }, articulation: { color: '#fb5607', name: 'Articulations' },
charniere: { color: '#d00000', name: 'Charnières vertébrales' }, charniere: { color: '#d00000', name: 'Charnieres vertebrales' },
'cadran-crane': { color: '#06a77d', name: 'Cadrans du crâne' }, 'cadran-crane': { color: '#06a77d', name: 'Cadrans du crane' },
'os-overlay': { color: '#f72585', name: 'Os overlay' }, 'os-overlay': { color: '#f72585', name: 'Os overlay' },
'spine-zone': { color: '#7209b7', name: 'Zones colonne' }, 'spine-zone': { color: '#7209b7', name: 'Zones colonne' },
} }
@@ -294,22 +332,22 @@ function initSvgEditor(
icon.textContent = '📐' icon.textContent = '📐'
const h2 = document.createElement('h2') const h2 = document.createElement('h2')
h2.textContent = 'SVG Anatomy Editor' h2.textContent = t('svg_editor.welcome_title')
const p = document.createElement('p') const p = document.createElement('p')
p.textContent = 'Upload an SVG file to begin. If your file already has points/zones with ' p.textContent = t('svg_editor.welcome_text')
const code1 = document.createElement('code') const code1 = document.createElement('code')
code1.textContent = 'data-anno-id' code1.textContent = 'data-anno-id'
const code2 = document.createElement('code') const code2 = document.createElement('code')
code2.textContent = 'data-anno-category' code2.textContent = 'data-anno-category'
p.appendChild(code1) p.appendChild(code1)
p.appendChild(document.createTextNode(' and ')) p.appendChild(document.createTextNode(' & '))
p.appendChild(code2) p.appendChild(code2)
p.appendChild(document.createTextNode(', they will be detected and configured automatically.')) p.appendChild(document.createTextNode(t('svg_editor.welcome_text_suffix')))
const btn = document.createElement('button') const btn = document.createElement('button')
btn.className = 'primary welcome-btn' btn.className = 'primary welcome-btn'
btn.textContent = 'Choose an SVG file' btn.textContent = t('svg_editor.choose_file')
btn.addEventListener('click', () => { btn.addEventListener('click', () => {
const fi = document.getElementById('svg-editor-file-import') as HTMLInputElement | null const fi = document.getElementById('svg-editor-file-import') as HTMLInputElement | null
fi?.click() fi?.click()
@@ -317,7 +355,7 @@ function initSvgEditor(
const hint = document.createElement('div') const hint = document.createElement('div')
hint.className = 'welcome-hint' hint.className = 'welcome-hint'
hint.textContent = 'or drag & drop an SVG file anywhere' hint.textContent = t('svg_editor.welcome_drag_hint')
welcome.append(icon, h2, p, btn, hint) welcome.append(icon, h2, p, btn, hint)
canvasInner.appendChild(welcome) canvasInner.appendChild(welcome)
@@ -332,7 +370,7 @@ function initSvgEditor(
getEl('svg-editor-count').textContent = '0' getEl('svg-editor-count').textContent = '0'
getEl('svg-editor-element-list').textContent = '' getEl('svg-editor-element-list').textContent = ''
getEl('svg-editor-edit-section').style.display = 'none' getEl('svg-editor-edit-section').style.display = 'none'
getEl('svg-editor-file-btn').textContent = 'Choose an SVG file' getEl('svg-editor-file-btn').textContent = t('svg_editor.choose_file')
} }
function setupGlobalDragDrop() { function setupGlobalDragDrop() {
@@ -351,11 +389,11 @@ function initSvgEditor(
const reader = new FileReader() const reader = new FileReader()
reader.onload = (ev) => { reader.onload = (ev) => {
loadSVG(ev.target?.result as string) loadSVG(ev.target?.result as string)
getEl('svg-editor-file-btn').textContent = 'Change SVG file' getEl('svg-editor-file-btn').textContent = t('svg_editor.change_file')
} }
reader.readAsText(file, 'UTF-8') reader.readAsText(file, 'UTF-8')
} else { } else {
alert('Drop an SVG file.') alert(t('svg_editor.alert_drop_svg'))
} }
}, sig) }, sig)
} }
@@ -365,7 +403,7 @@ function initSvgEditor(
const doc = parser.parseFromString(svgText, 'image/svg+xml') const doc = parser.parseFromString(svgText, 'image/svg+xml')
const parsedSvg = doc.querySelector('svg') const parsedSvg = doc.querySelector('svg')
if (!parsedSvg) { if (!parsedSvg) {
alert('Invalid file: no SVG element found.') alert(t('svg_editor.alert_invalid_svg'))
showWelcomeScreen() showWelcomeScreen()
return return
} }
@@ -374,7 +412,7 @@ function initSvgEditor(
canvasInner.appendChild(document.importNode(parsedSvg, true)) canvasInner.appendChild(document.importNode(parsedSvg, true))
svgRoot = canvasInner.querySelector('svg') svgRoot = canvasInner.querySelector('svg')
if (!svgRoot) { if (!svgRoot) {
alert('Invalid file: no SVG element found.') alert(t('svg_editor.alert_invalid_svg'))
showWelcomeScreen() showWelcomeScreen()
return return
} }
@@ -580,7 +618,7 @@ function initSvgEditor(
selected.setAttribute('points', pointsToStr(points)) selected.setAttribute('points', pointsToStr(points))
showPolygonHandles(selected) showPolygonHandles(selected)
} else { } else {
alert('A polygon must have at least 3 vertices.') alert(t('svg_editor.alert_min_3_vertices'))
} }
e.preventDefault() e.preventDefault()
e.stopPropagation() e.stopPropagation()
@@ -736,7 +774,7 @@ function initSvgEditor(
const reader = new FileReader() const reader = new FileReader()
reader.onload = (ev) => { reader.onload = (ev) => {
loadSVG(ev.target?.result as string) loadSVG(ev.target?.result as string)
getEl('svg-editor-file-btn').textContent = 'Change SVG file' getEl('svg-editor-file-btn').textContent = t('svg_editor.change_file')
} }
reader.readAsText(file, 'UTF-8') reader.readAsText(file, 'UTF-8')
;(e.target as HTMLInputElement).value = '' ;(e.target as HTMLInputElement).value = ''
@@ -771,7 +809,7 @@ function initSvgEditor(
function setMode(m: string) { function setMode(m: string) {
if (!svgRoot && m !== 'select') { if (!svgRoot && m !== 'select') {
alert('Upload an SVG first.') alert(t('svg_editor.alert_upload_first'))
return return
} }
mode = m mode = m
@@ -940,7 +978,7 @@ function initSvgEditor(
.getAttribute('points')! .getAttribute('points')!
.split(/\s+/) .split(/\s+/)
.filter((p) => p).length .filter((p) => p).length
txt = `polygon (${pts} vertices)` txt = t('svg_editor.polygon_vertices').replace('{count}', String(pts))
} }
getEl('svg-editor-coords-display').textContent = txt getEl('svg-editor-coords-display').textContent = txt
} }
@@ -1045,7 +1083,7 @@ function initSvgEditor(
function deleteSelected() { function deleteSelected() {
if (!selected) return if (!selected) return
if (!confirm('Delete this element?')) return if (!confirm(t('svg_editor.confirm_delete_element'))) return
const id = selected.getAttribute('data-anno-id')! const id = selected.getAttribute('data-anno-id')!
const cat = selected.getAttribute('data-anno-category') const cat = selected.getAttribute('data-anno-category')
const parent = selected.parentNode as Element | null const parent = selected.parentNode as Element | null
@@ -1073,6 +1111,8 @@ function initSvgEditor(
} }
const id = 'point-' + Date.now().toString(36) const id = 'point-' + Date.now().toString(36)
const frName = t('svg_editor.new_point_fr')
const enName = t('svg_editor.new_point_en')
const hit = document.createElementNS('http://www.w3.org/2000/svg', 'circle') const hit = document.createElementNS('http://www.w3.org/2000/svg', 'circle')
hit.setAttribute('cx', String(Math.round(x))) hit.setAttribute('cx', String(Math.round(x)))
@@ -1081,14 +1121,14 @@ function initSvgEditor(
hit.setAttribute('fill', 'transparent') hit.setAttribute('fill', 'transparent')
hit.setAttribute('pointer-events', 'all') hit.setAttribute('pointer-events', 'all')
hit.setAttribute('data-anno-id', id) hit.setAttribute('data-anno-id', id)
hit.setAttribute('data-anno-name-fr', 'Nouveau point') hit.setAttribute('data-anno-name-fr', frName)
hit.setAttribute('data-anno-name-en', 'New point') hit.setAttribute('data-anno-name-en', enName)
hit.setAttribute('data-anno-category', cat) hit.setAttribute('data-anno-category', cat)
if (CATEGORIES[cat]?.name) hit.setAttribute('data-anno-category-name', CATEGORIES[cat].name) if (CATEGORIES[cat]?.name) hit.setAttribute('data-anno-category-name', CATEGORIES[cat].name)
if (CATEGORIES[cat]?.special) hit.setAttribute('data-anno-special', CATEGORIES[cat].special!) if (CATEGORIES[cat]?.special) hit.setAttribute('data-anno-special', CATEGORIES[cat].special!)
hit.setAttribute('style', 'cursor:pointer') hit.setAttribute('style', 'cursor:pointer')
const t1 = document.createElementNS('http://www.w3.org/2000/svg', 'title') const t1 = document.createElementNS('http://www.w3.org/2000/svg', 'title')
t1.textContent = 'Nouveau point' t1.textContent = frName
hit.appendChild(t1) hit.appendChild(t1)
overlayG.appendChild(hit) overlayG.appendChild(hit)
@@ -1103,13 +1143,13 @@ function initSvgEditor(
vis.setAttribute('pointer-events', 'none') vis.setAttribute('pointer-events', 'none')
vis.setAttribute('id', id) vis.setAttribute('id', id)
vis.setAttribute('data-anno-id', id) vis.setAttribute('data-anno-id', id)
vis.setAttribute('data-anno-name-fr', 'Nouveau point') vis.setAttribute('data-anno-name-fr', frName)
vis.setAttribute('data-anno-name-en', 'New point') vis.setAttribute('data-anno-name-en', enName)
vis.setAttribute('data-anno-category', cat) vis.setAttribute('data-anno-category', cat)
if (CATEGORIES[cat]?.name) vis.setAttribute('data-anno-category-name', CATEGORIES[cat].name) if (CATEGORIES[cat]?.name) vis.setAttribute('data-anno-category-name', CATEGORIES[cat].name)
if (CATEGORIES[cat]?.special) vis.setAttribute('data-anno-special', CATEGORIES[cat].special!) if (CATEGORIES[cat]?.special) vis.setAttribute('data-anno-special', CATEGORIES[cat].special!)
const t2 = document.createElementNS('http://www.w3.org/2000/svg', 'title') const t2 = document.createElementNS('http://www.w3.org/2000/svg', 'title')
t2.textContent = 'Nouveau point' t2.textContent = frName
vis.appendChild(t2) vis.appendChild(t2)
overlayG.appendChild(vis) overlayG.appendChild(vis)
@@ -1159,7 +1199,7 @@ function initSvgEditor(
function finalizePolygon() { function finalizePolygon() {
if (!svgRoot) return if (!svgRoot) return
if (polygonPoints.length < 3) { if (polygonPoints.length < 3) {
alert('Need at least 3 points.') alert(t('svg_editor.alert_min_3'))
return return
} }
@@ -1179,6 +1219,8 @@ function initSvgEditor(
} }
const id = memo ? memo.id : 'zone-' + Date.now().toString(36) const id = memo ? memo.id : 'zone-' + Date.now().toString(36)
const frName = memo ? memo.fr || t('svg_editor.new_zone_fr') : t('svg_editor.new_zone_fr')
const enName = memo ? memo.en || t('svg_editor.new_zone_en') : t('svg_editor.new_zone_en')
const poly = document.createElementNS('http://www.w3.org/2000/svg', 'polygon') const poly = document.createElementNS('http://www.w3.org/2000/svg', 'polygon')
poly.setAttribute( poly.setAttribute(
'points', 'points',
@@ -1192,17 +1234,17 @@ function initSvgEditor(
poly.setAttribute('style', 'cursor:pointer') poly.setAttribute('style', 'cursor:pointer')
poly.setAttribute('id', id) poly.setAttribute('id', id)
poly.setAttribute('data-anno-id', id) poly.setAttribute('data-anno-id', id)
poly.setAttribute('data-anno-name-fr', memo ? memo.fr || 'Nouvelle zone' : 'Nouvelle zone') poly.setAttribute('data-anno-name-fr', frName)
poly.setAttribute('data-anno-name-en', memo ? memo.en || 'New zone' : 'New zone') poly.setAttribute('data-anno-name-en', enName)
poly.setAttribute('data-anno-category', cat) poly.setAttribute('data-anno-category', cat)
if (CATEGORIES[cat]?.name) if (CATEGORIES[cat]?.name)
poly.setAttribute('data-anno-category-name', CATEGORIES[cat].name) poly.setAttribute('data-anno-category-name', CATEGORIES[cat].name)
if (CATEGORIES[cat]?.special) if (CATEGORIES[cat]?.special)
poly.setAttribute('data-anno-special', CATEGORIES[cat].special!) poly.setAttribute('data-anno-special', CATEGORIES[cat].special!)
const t = document.createElementNS('http://www.w3.org/2000/svg', 'title') const titleEl = document.createElementNS('http://www.w3.org/2000/svg', 'title')
t.textContent = titleEl.textContent =
poly.getAttribute('data-anno-name-fr') + ' / ' + poly.getAttribute('data-anno-name-en') poly.getAttribute('data-anno-name-fr') + ' / ' + poly.getAttribute('data-anno-name-en')
poly.appendChild(t) poly.appendChild(titleEl)
overlayG.appendChild(poly) overlayG.appendChild(poly)
cancelPolygon() cancelPolygon()
@@ -1243,7 +1285,7 @@ function initSvgEditor(
if (Object.keys(CATEGORIES).length === 0) { if (Object.keys(CATEGORIES).length === 0) {
const emptyMsg = document.createElement('div') const emptyMsg = document.createElement('div')
emptyMsg.style.cssText = 'font-size:12px; color:var(--color-ink-muted); padding:8px; text-align:center;' emptyMsg.style.cssText = 'font-size:12px; color:var(--color-ink-muted); padding:8px; text-align:center;'
emptyMsg.textContent = 'No categories yet. Use the form below to add one.' emptyMsg.textContent = t('svg_editor.no_categories')
list.appendChild(emptyMsg) list.appendChild(emptyMsg)
return return
} }
@@ -1394,7 +1436,7 @@ Current: ${current || '(none)'}`
const name = (getEl('svg-editor-new-cat-name') as HTMLInputElement).value.trim() const name = (getEl('svg-editor-new-cat-name') as HTMLInputElement).value.trim()
const color = (getEl('svg-editor-new-cat-color') as HTMLInputElement).value const color = (getEl('svg-editor-new-cat-color') as HTMLInputElement).value
if (!name) { if (!name) {
alert('Give the category a name.') alert(t('svg_editor.alert_cat_name_required'))
return return
} }
const slug = name const slug = name
@@ -1404,12 +1446,12 @@ Current: ${current || '(none)'}`
.replace(/[^a-z0-9]+/g, '-') .replace(/[^a-z0-9]+/g, '-')
.replace(/^-|-$/g, '') .replace(/^-|-$/g, '')
if (!slug) { if (!slug) {
alert('Invalid name.') alert(t('svg_editor.alert_cat_invalid_name'))
return return
} }
if (CATEGORIES[slug]) { if (CATEGORIES[slug]) {
alert('This category already exists.') alert(t('svg_editor.alert_cat_exists'))
return return
} }
@@ -1424,8 +1466,10 @@ Current: ${current || '(none)'}`
const count = svgRoot.querySelectorAll( const count = svgRoot.querySelectorAll(
`[data-anno-category="${cat}"]:not([fill="transparent"])`, `[data-anno-category="${cat}"]:not([fill="transparent"])`,
).length ).length
let msg = `Delete category "${CATEGORIES[cat].name}"?` let msg = t('svg_editor.confirm_delete_cat').replace('{name}', CATEGORIES[cat].name)
if (count > 0) msg += `\n\nIt contains ${count} element(s) that will also be deleted.` if (count > 0) {
msg += t('svg_editor.confirm_delete_cat_count').replace('{count}', String(count))
}
if (!confirm(msg)) return if (!confirm(msg)) return
svgRoot.querySelectorAll(`[data-anno-category="${cat}"]`).forEach((svgEl) => { svgRoot.querySelectorAll(`[data-anno-category="${cat}"]`).forEach((svgEl) => {
@@ -1566,9 +1610,9 @@ Current: ${current || '(none)'}`
m.setAttribute('r', '6') m.setAttribute('r', '6')
m.classList.add('polygon-midhandle') m.classList.add('polygon-midhandle')
m.setAttribute('data-edge-idx', String(i)) m.setAttribute('data-edge-idx', String(i))
const t = document.createElementNS('http://www.w3.org/2000/svg', 'title') const titleEl = document.createElementNS('http://www.w3.org/2000/svg', 'title')
t.textContent = 'Click to insert a vertex here' titleEl.textContent = t('svg_editor.handle_insert')
m.appendChild(t) m.appendChild(titleEl)
handlesG.appendChild(m) handlesG.appendChild(m)
} }
@@ -1579,9 +1623,9 @@ Current: ${current || '(none)'}`
h.setAttribute('r', '8') h.setAttribute('r', '8')
h.classList.add('polygon-handle') h.classList.add('polygon-handle')
h.setAttribute('data-vertex-idx', String(i)) h.setAttribute('data-vertex-idx', String(i))
const t = document.createElementNS('http://www.w3.org/2000/svg', 'title') const titleEl = document.createElementNS('http://www.w3.org/2000/svg', 'title')
t.textContent = 'Drag to move | Alt+click or right-click to delete' titleEl.textContent = t('svg_editor.handle_drag')
h.appendChild(t) h.appendChild(titleEl)
handlesG!.appendChild(h) handlesG!.appendChild(h)
}) })
} }
@@ -1593,7 +1637,7 @@ Current: ${current || '(none)'}`
function restartCurrentPolygon() { function restartCurrentPolygon() {
if (!selected || selected.tagName.toLowerCase() !== 'polygon') return if (!selected || selected.tagName.toLowerCase() !== 'polygon') return
if (!confirm('Erase this polygon and redraw it?')) return if (!confirm(t('svg_editor.confirm_redraw'))) return
_polygonRestartMemo = { _polygonRestartMemo = {
id: selected.getAttribute('data-anno-id') || '', id: selected.getAttribute('data-anno-id') || '',
fr: selected.getAttribute('data-anno-name-fr') || '', fr: selected.getAttribute('data-anno-name-fr') || '',
@@ -1646,7 +1690,7 @@ Current: ${current || '(none)'}`
function exportSVG() { function exportSVG() {
if (!svgRoot) { if (!svgRoot) {
alert('No SVG loaded. Upload a file first.') alert(t('svg_editor.alert_no_svg_loaded'))
return return
} }
const clone = svgRoot.cloneNode(true) as SVGSVGElement const clone = svgRoot.cloneNode(true) as SVGSVGElement
+3 -2
View File
@@ -1,6 +1,7 @@
.svg-editor-root { .svg-editor-root {
display: flex; display: flex;
height: 100vh; flex: 1;
min-height: 0;
overflow: hidden; overflow: hidden;
font-family: var(--font-sans); font-family: var(--font-sans);
color: var(--color-ink); color: var(--color-ink);
@@ -426,7 +427,7 @@
.svg-editor-canvas.dragging-over { background: color-mix(in srgb, var(--color-accent) 8%, var(--color-surface)); } .svg-editor-canvas.dragging-over { background: color-mix(in srgb, var(--color-accent) 8%, var(--color-surface)); }
.svg-editor-canvas.dragging-over .welcome { color: var(--color-accent); } .svg-editor-canvas.dragging-over .welcome { color: var(--color-accent); }
.svg-editor-canvas.dragging-over::after { .svg-editor-canvas.dragging-over::after {
content: 'Drop SVG here'; content: attr(data-drop-label);
position: absolute; position: absolute;
inset: 10px; inset: 10px;
display: flex; display: flex;