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
+125 -80

No files matched your search

+122 -78
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,14 +35,37 @@ 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="flex flex-col h-screen">
<header className="px-4 py-2 border-b border-[var(--color-border)] flex items-center justify-between shrink-0">
<div className="flex items-baseline gap-3">
<Link to="/" className="no-underline">
<Brand size="sm" />
</Link>
<span className="text-xs text-[var(--color-ink-muted)]">{cfg.tagline}</span>
</div>
<LanguageSwitcher />
</header>
<div className="svg-editor-root"> <div className="svg-editor-root">
<div ref={canvasRef} className="svg-editor-canvas mode-select"> <div
ref={canvasRef}
className="svg-editor-canvas mode-select"
data-drop-label={t('svg_editor.drop_svg_here')}
>
<div ref={canvasInnerRef} className="svg-editor-canvas-inner" /> <div ref={canvasInnerRef} className="svg-editor-canvas-inner" />
<div className="svg-editor-zoom-controls" id="svg-editor-zoom-controls" /> <div className="svg-editor-zoom-controls" id="svg-editor-zoom-controls" />
</div> </div>
@@ -46,54 +81,55 @@ export default function SvgEditorRoute() {
&larr; &larr;
</Link> </Link>
<div> <div>
<h1>SVG Anatomy Editor</h1> <h1>{t('svg_editor.title')}</h1>
<div className="sub">Points, zones &amp; clickable categories</div> <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>
<button className="primary" id="svg-editor-export-btn"> <button className="primary" id="svg-editor-export-btn">
Export SVG {t('svg_editor.export')}
</button> </button>
</div> </div>
<div className="section"> <div className="section">
<h3>Mode</h3> <h3>{t('svg_editor.mode')}</h3>
<button data-mode="select" className="active" id="svg-editor-mode-select"> <button data-mode="select" className="active" id="svg-editor-mode-select">
☝ Select / move &#9757; {t('svg_editor.mode_select')}
</button> </button>
<button data-mode="point" id="svg-editor-mode-point"> <button data-mode="point" id="svg-editor-mode-point">
● Add point &#9679; {t('svg_editor.mode_point')}
</button> </button>
<button data-mode="polygon" id="svg-editor-mode-polygon"> <button data-mode="polygon" id="svg-editor-mode-polygon">
▬ Add polygon &#9644; {t('svg_editor.mode_polygon')}
</button> </button>
<div id="svg-editor-polygon-controls" style={{ display: 'none' }}> <div id="svg-editor-polygon-controls" style={{ display: 'none' }}>
<button className="primary" id="svg-editor-finalize-polygon"> <button className="primary" id="svg-editor-finalize-polygon">
Validate polygon (Enter) {t('svg_editor.validate_polygon')}
</button> </button>
<button className="danger" id="svg-editor-cancel-polygon"> <button className="danger" id="svg-editor-cancel-polygon">
Cancel (Esc) {t('svg_editor.cancel_polygon')}
</button> </button>
</div> </div>
<label>Category for new elements</label> <label>{t('svg_editor.category_for_new')}</label>
<select id="svg-editor-default-category" /> <select id="svg-editor-default-category" />
</div> </div>
<div className="section"> <div className="section">
<h3> <h3>
Categories <span style={{ color: 'var(--color-ink-muted)' }}>(shared color)</span> {t('svg_editor.categories')}{' '}
<span style={{ color: 'var(--color-ink-muted)' }}>{t('svg_editor.shared_color')}</span>
</h3> </h3>
<div id="svg-editor-category-list" /> <div id="svg-editor-category-list" />
<div className="add-cat"> <div className="add-cat">
<input type="text" id="svg-editor-new-cat-name" placeholder="Category name" /> <input type="text" id="svg-editor-new-cat-name" placeholder={t('svg_editor.category_placeholder')} />
<input type="color" id="svg-editor-new-cat-color" defaultValue="#00aaff" /> <input type="color" id="svg-editor-new-cat-color" defaultValue="#00aaff" />
<button className="primary" id="svg-editor-add-cat-btn"> <button className="primary" id="svg-editor-add-cat-btn">
+ +
@@ -102,7 +138,7 @@ export default function SvgEditorRoute() {
</div> </div>
<div className="section" id="svg-editor-edit-section" style={{ display: 'none' }}> <div className="section" id="svg-editor-edit-section" style={{ display: 'none' }}>
<h3>Selected element</h3> <h3>{t('svg_editor.selected_element')}</h3>
<div className="coords" id="svg-editor-coords-display"> <div className="coords" id="svg-editor-coords-display">
&mdash; &mdash;
</div> </div>
@@ -114,22 +150,22 @@ export default function SvgEditorRoute() {
id="svg-editor-edit-opens-detail" id="svg-editor-edit-opens-detail"
style={{ margin: 0, width: 'auto' }} style={{ margin: 0, width: 'auto' }}
/> />
<span>This zone opens a detail view</span> <span>{t('svg_editor.opens_detail')}</span>
</label> </label>
<div id="svg-editor-detail-target-row" style={{ display: 'none', marginTop: '8px' }}> <div id="svg-editor-detail-target-row" style={{ display: 'none', marginTop: '8px' }}>
<label>Detail view ID (e.g. head, front-leg)</label> <label>{t('svg_editor.detail_id_label')}</label>
<input id="svg-editor-edit-detail-target" placeholder="head" /> <input id="svg-editor-edit-detail-target" placeholder="head" />
</div> </div>
</div> </div>
<div id="svg-editor-osteo-fields"> <div id="svg-editor-osteo-fields">
<label>French name</label> <label>{t('svg_editor.name_fr')}</label>
<input id="svg-editor-edit-fr" placeholder="ex: Hanche" /> <input id="svg-editor-edit-fr" placeholder="ex: Hanche" />
<label>English name</label> <label>{t('svg_editor.name_en')}</label>
<input id="svg-editor-edit-en" placeholder="ex: Hip joint" /> <input id="svg-editor-edit-en" placeholder="ex: Hip joint" />
<label>Category</label> <label>{t('svg_editor.category')}</label>
<select id="svg-editor-edit-category" /> <select id="svg-editor-edit-category" />
<label id="svg-editor-lbl-radius">Radius (circle)</label> <label id="svg-editor-lbl-radius">{t('svg_editor.radius')}</label>
<div className="slider-row" id="svg-editor-radius-row"> <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" type="range" min={1} max={80} defaultValue={15} />
<input <input
@@ -142,7 +178,7 @@ export default function SvgEditorRoute() {
</div> </div>
</div> </div>
<div id="svg-editor-opacity-block"> <div id="svg-editor-opacity-block">
<label>Fill opacity</label> <label>{t('svg_editor.fill_opacity')}</label>
<div className="slider-row"> <div className="slider-row">
<input <input
id="svg-editor-edit-opacity" id="svg-editor-edit-opacity"
@@ -173,33 +209,34 @@ export default function SvgEditorRoute() {
}} }}
> >
<div style={{ fontSize: '11px', color: '#92400e', marginBottom: '6px' }}> <div style={{ fontSize: '11px', color: '#92400e', marginBottom: '6px' }}>
<strong>Polygon editing:</strong> <strong>{t('svg_editor.polygon_editing')}</strong>
<br /> <br />
Drag a yellow handle to move it {t('svg_editor.polygon_drag')}
<br /> <br />
Click a white mid-handle to insert a vertex {t('svg_editor.polygon_insert')}
<br /> <br />
Alt+click or right-click a yellow handle to delete it {t('svg_editor.polygon_delete')}
</div> </div>
<button id="svg-editor-restart-polygon">Redraw this polygon</button> <button id="svg-editor-restart-polygon">{t('svg_editor.redraw_polygon')}</button>
</div> </div>
<div style={{ marginTop: '12px' }}> <div style={{ marginTop: '12px' }}>
<button className="danger" id="svg-editor-delete-btn"> <button className="danger" id="svg-editor-delete-btn">
Delete {t('svg_editor.delete')}
</button> </button>
</div> </div>
</div> </div>
<div className="section"> <div className="section">
<h3> <h3>
Elements (<span id="svg-editor-count">0</span>) {t('svg_editor.elements')} (<span id="svg-editor-count">0</span>)
</h3> </h3>
<input id="svg-editor-filter-input" placeholder="Filter..." /> <input id="svg-editor-filter-input" placeholder={t('svg_editor.filter_placeholder')} />
<div className="element-list" id="svg-editor-element-list" /> <div className="element-list" id="svg-editor-element-list" />
</div> </div>
</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;