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:
1 parent
5e24c2f3d7
commit
1673302b09
2 files changed
+246
-201
No files matched your search
+243
-199
@@ -1,11 +1,23 @@
|
||||
import { useEffect, useRef } from 'react'
|
||||
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'
|
||||
|
||||
type TFn = (key: string) => string
|
||||
|
||||
export default function SvgEditorRoute() {
|
||||
const canvasRef = useRef<HTMLDivElement>(null)
|
||||
const canvasInnerRef = 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(() => {
|
||||
const meta = document.createElement('meta')
|
||||
@@ -23,179 +35,204 @@ export default function SvgEditorRoute() {
|
||||
const sidebar = sidebarRef.current
|
||||
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 (
|
||||
<div className="svg-editor-root">
|
||||
<div ref={canvasRef} className="svg-editor-canvas mode-select">
|
||||
<div ref={canvasInnerRef} className="svg-editor-canvas-inner" />
|
||||
<div className="svg-editor-zoom-controls" id="svg-editor-zoom-controls" />
|
||||
</div>
|
||||
<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 ref={sidebarRef} className="svg-editor-sidebar">
|
||||
<header>
|
||||
<div className="flex items-center gap-3">
|
||||
<Link
|
||||
to="/"
|
||||
className="text-white/80 hover:text-white transition text-sm"
|
||||
style={{ textDecoration: 'none' }}
|
||||
>
|
||||
←
|
||||
</Link>
|
||||
<div>
|
||||
<h1>SVG Anatomy Editor</h1>
|
||||
<div className="sub">Points, zones & clickable categories</div>
|
||||
<div className="svg-editor-root">
|
||||
<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 className="svg-editor-zoom-controls" id="svg-editor-zoom-controls" />
|
||||
</div>
|
||||
|
||||
<div ref={sidebarRef} className="svg-editor-sidebar">
|
||||
<header>
|
||||
<div className="flex items-center gap-3">
|
||||
<Link
|
||||
to="/"
|
||||
className="text-white/80 hover:text-white transition text-sm"
|
||||
style={{ textDecoration: 'none' }}
|
||||
>
|
||||
←
|
||||
</Link>
|
||||
<div>
|
||||
<h1>{t('svg_editor.title')}</h1>
|
||||
<div className="sub">{t('svg_editor.subtitle')}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
</header>
|
||||
|
||||
<div className="svg-editor-sidebar-content">
|
||||
<div className="section">
|
||||
<h3>File</h3>
|
||||
<input type="file" id="svg-editor-file-import" accept=".svg" style={{ display: 'none' }} />
|
||||
<button className="primary" id="svg-editor-file-btn">
|
||||
Choose an SVG 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)
|
||||
<div className="svg-editor-sidebar-content">
|
||||
<div className="section">
|
||||
<h3>{t('svg_editor.file')}</h3>
|
||||
<input type="file" id="svg-editor-file-import" accept=".svg" style={{ display: 'none' }} />
|
||||
<button className="primary" id="svg-editor-file-btn">
|
||||
{t('svg_editor.choose_file')}
|
||||
</button>
|
||||
<button className="danger" id="svg-editor-cancel-polygon">
|
||||
Cancel (Esc)
|
||||
<button className="primary" id="svg-editor-export-btn">
|
||||
{t('svg_editor.export')}
|
||||
</button>
|
||||
</div>
|
||||
<label>Category for new elements</label>
|
||||
<select id="svg-editor-default-category" />
|
||||
</div>
|
||||
|
||||
<div className="section">
|
||||
<h3>
|
||||
Categories <span style={{ color: 'var(--color-ink-muted)' }}>(shared color)</span>
|
||||
</h3>
|
||||
<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">
|
||||
+
|
||||
<div className="section">
|
||||
<h3>{t('svg_editor.mode')}</h3>
|
||||
<button data-mode="select" className="active" id="svg-editor-mode-select">
|
||||
☝ {t('svg_editor.mode_select')}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="section" id="svg-editor-edit-section" style={{ display: 'none' }}>
|
||||
<h3>Selected element</h3>
|
||||
<div className="coords" id="svg-editor-coords-display">
|
||||
—
|
||||
<button data-mode="point" id="svg-editor-mode-point">
|
||||
● {t('svg_editor.mode_point')}
|
||||
</button>
|
||||
<button data-mode="polygon" id="svg-editor-mode-polygon">
|
||||
▬ {t('svg_editor.mode_polygon')}
|
||||
</button>
|
||||
<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 className="detail-toggle" id="svg-editor-detail-toggle-block">
|
||||
<label style={{ display: 'flex', alignItems: 'center', gap: '6px', cursor: 'pointer' }}>
|
||||
<input
|
||||
type="checkbox"
|
||||
id="svg-editor-edit-opens-detail"
|
||||
style={{ margin: 0, width: 'auto' }}
|
||||
/>
|
||||
<span>This zone opens a detail view</span>
|
||||
</label>
|
||||
<div id="svg-editor-detail-target-row" style={{ display: 'none', marginTop: '8px' }}>
|
||||
<label>Detail view ID (e.g. head, front-leg)</label>
|
||||
<input id="svg-editor-edit-detail-target" placeholder="head" />
|
||||
<div className="section">
|
||||
<h3>
|
||||
{t('svg_editor.categories')}{' '}
|
||||
<span style={{ color: 'var(--color-ink-muted)' }}>{t('svg_editor.shared_color')}</span>
|
||||
</h3>
|
||||
<div id="svg-editor-category-list" />
|
||||
<div className="add-cat">
|
||||
<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" />
|
||||
<button className="primary" id="svg-editor-add-cat-btn">
|
||||
+
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="svg-editor-osteo-fields">
|
||||
<label>French name</label>
|
||||
<input id="svg-editor-edit-fr" placeholder="ex: Hanche" />
|
||||
<label>English name</label>
|
||||
<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 className="section" id="svg-editor-edit-section" style={{ display: 'none' }}>
|
||||
<h3>{t('svg_editor.selected_element')}</h3>
|
||||
<div className="coords" id="svg-editor-coords-display">
|
||||
—
|
||||
</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">
|
||||
<h3>
|
||||
Elements (<span id="svg-editor-count">0</span>)
|
||||
</h3>
|
||||
<input id="svg-editor-filter-input" placeholder="Filter..." />
|
||||
<div className="element-list" id="svg-editor-element-list" />
|
||||
<div className="detail-toggle" id="svg-editor-detail-toggle-block">
|
||||
<label style={{ display: 'flex', alignItems: 'center', gap: '6px', cursor: 'pointer' }}>
|
||||
<input
|
||||
type="checkbox"
|
||||
id="svg-editor-edit-opens-detail"
|
||||
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>
|
||||
@@ -207,6 +244,7 @@ function initSvgEditor(
|
||||
canvas: HTMLDivElement,
|
||||
canvasInner: HTMLDivElement,
|
||||
_sidebar: HTMLDivElement,
|
||||
t: TFn,
|
||||
): () => void {
|
||||
const ac = new AbortController()
|
||||
const sig = { signal: ac.signal }
|
||||
@@ -232,8 +270,8 @@ function initSvgEditor(
|
||||
|
||||
const CATEGORY_TEMPLATES: Record<string, { color: string; name: string }> = {
|
||||
articulation: { color: '#fb5607', name: 'Articulations' },
|
||||
charniere: { color: '#d00000', name: 'Charnières vertébrales' },
|
||||
'cadran-crane': { color: '#06a77d', name: 'Cadrans du crâne' },
|
||||
charniere: { color: '#d00000', name: 'Charnieres vertebrales' },
|
||||
'cadran-crane': { color: '#06a77d', name: 'Cadrans du crane' },
|
||||
'os-overlay': { color: '#f72585', name: 'Os overlay' },
|
||||
'spine-zone': { color: '#7209b7', name: 'Zones colonne' },
|
||||
}
|
||||
@@ -294,22 +332,22 @@ function initSvgEditor(
|
||||
icon.textContent = '📐'
|
||||
|
||||
const h2 = document.createElement('h2')
|
||||
h2.textContent = 'SVG Anatomy Editor'
|
||||
h2.textContent = t('svg_editor.welcome_title')
|
||||
|
||||
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')
|
||||
code1.textContent = 'data-anno-id'
|
||||
const code2 = document.createElement('code')
|
||||
code2.textContent = 'data-anno-category'
|
||||
p.appendChild(code1)
|
||||
p.appendChild(document.createTextNode(' and '))
|
||||
p.appendChild(document.createTextNode(' & '))
|
||||
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')
|
||||
btn.className = 'primary welcome-btn'
|
||||
btn.textContent = 'Choose an SVG file'
|
||||
btn.textContent = t('svg_editor.choose_file')
|
||||
btn.addEventListener('click', () => {
|
||||
const fi = document.getElementById('svg-editor-file-import') as HTMLInputElement | null
|
||||
fi?.click()
|
||||
@@ -317,7 +355,7 @@ function initSvgEditor(
|
||||
|
||||
const hint = document.createElement('div')
|
||||
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)
|
||||
canvasInner.appendChild(welcome)
|
||||
@@ -332,7 +370,7 @@ function initSvgEditor(
|
||||
getEl('svg-editor-count').textContent = '0'
|
||||
getEl('svg-editor-element-list').textContent = ''
|
||||
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() {
|
||||
@@ -351,11 +389,11 @@ function initSvgEditor(
|
||||
const reader = new FileReader()
|
||||
reader.onload = (ev) => {
|
||||
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')
|
||||
} else {
|
||||
alert('Drop an SVG file.')
|
||||
alert(t('svg_editor.alert_drop_svg'))
|
||||
}
|
||||
}, sig)
|
||||
}
|
||||
@@ -365,7 +403,7 @@ function initSvgEditor(
|
||||
const doc = parser.parseFromString(svgText, 'image/svg+xml')
|
||||
const parsedSvg = doc.querySelector('svg')
|
||||
if (!parsedSvg) {
|
||||
alert('Invalid file: no SVG element found.')
|
||||
alert(t('svg_editor.alert_invalid_svg'))
|
||||
showWelcomeScreen()
|
||||
return
|
||||
}
|
||||
@@ -374,7 +412,7 @@ function initSvgEditor(
|
||||
canvasInner.appendChild(document.importNode(parsedSvg, true))
|
||||
svgRoot = canvasInner.querySelector('svg')
|
||||
if (!svgRoot) {
|
||||
alert('Invalid file: no SVG element found.')
|
||||
alert(t('svg_editor.alert_invalid_svg'))
|
||||
showWelcomeScreen()
|
||||
return
|
||||
}
|
||||
@@ -580,7 +618,7 @@ function initSvgEditor(
|
||||
selected.setAttribute('points', pointsToStr(points))
|
||||
showPolygonHandles(selected)
|
||||
} else {
|
||||
alert('A polygon must have at least 3 vertices.')
|
||||
alert(t('svg_editor.alert_min_3_vertices'))
|
||||
}
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
@@ -736,7 +774,7 @@ function initSvgEditor(
|
||||
const reader = new FileReader()
|
||||
reader.onload = (ev) => {
|
||||
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')
|
||||
;(e.target as HTMLInputElement).value = ''
|
||||
@@ -771,7 +809,7 @@ function initSvgEditor(
|
||||
|
||||
function setMode(m: string) {
|
||||
if (!svgRoot && m !== 'select') {
|
||||
alert('Upload an SVG first.')
|
||||
alert(t('svg_editor.alert_upload_first'))
|
||||
return
|
||||
}
|
||||
mode = m
|
||||
@@ -940,7 +978,7 @@ function initSvgEditor(
|
||||
.getAttribute('points')!
|
||||
.split(/\s+/)
|
||||
.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
|
||||
}
|
||||
@@ -1045,7 +1083,7 @@ function initSvgEditor(
|
||||
|
||||
function deleteSelected() {
|
||||
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 cat = selected.getAttribute('data-anno-category')
|
||||
const parent = selected.parentNode as Element | null
|
||||
@@ -1073,6 +1111,8 @@ function initSvgEditor(
|
||||
}
|
||||
|
||||
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')
|
||||
hit.setAttribute('cx', String(Math.round(x)))
|
||||
@@ -1081,14 +1121,14 @@ function initSvgEditor(
|
||||
hit.setAttribute('fill', 'transparent')
|
||||
hit.setAttribute('pointer-events', 'all')
|
||||
hit.setAttribute('data-anno-id', id)
|
||||
hit.setAttribute('data-anno-name-fr', 'Nouveau point')
|
||||
hit.setAttribute('data-anno-name-en', 'New point')
|
||||
hit.setAttribute('data-anno-name-fr', frName)
|
||||
hit.setAttribute('data-anno-name-en', enName)
|
||||
hit.setAttribute('data-anno-category', cat)
|
||||
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!)
|
||||
hit.setAttribute('style', 'cursor:pointer')
|
||||
const t1 = document.createElementNS('http://www.w3.org/2000/svg', 'title')
|
||||
t1.textContent = 'Nouveau point'
|
||||
t1.textContent = frName
|
||||
hit.appendChild(t1)
|
||||
overlayG.appendChild(hit)
|
||||
|
||||
@@ -1103,13 +1143,13 @@ function initSvgEditor(
|
||||
vis.setAttribute('pointer-events', 'none')
|
||||
vis.setAttribute('id', id)
|
||||
vis.setAttribute('data-anno-id', id)
|
||||
vis.setAttribute('data-anno-name-fr', 'Nouveau point')
|
||||
vis.setAttribute('data-anno-name-en', 'New point')
|
||||
vis.setAttribute('data-anno-name-fr', frName)
|
||||
vis.setAttribute('data-anno-name-en', enName)
|
||||
vis.setAttribute('data-anno-category', cat)
|
||||
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!)
|
||||
const t2 = document.createElementNS('http://www.w3.org/2000/svg', 'title')
|
||||
t2.textContent = 'Nouveau point'
|
||||
t2.textContent = frName
|
||||
vis.appendChild(t2)
|
||||
overlayG.appendChild(vis)
|
||||
|
||||
@@ -1159,7 +1199,7 @@ function initSvgEditor(
|
||||
function finalizePolygon() {
|
||||
if (!svgRoot) return
|
||||
if (polygonPoints.length < 3) {
|
||||
alert('Need at least 3 points.')
|
||||
alert(t('svg_editor.alert_min_3'))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1179,6 +1219,8 @@ function initSvgEditor(
|
||||
}
|
||||
|
||||
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')
|
||||
poly.setAttribute(
|
||||
'points',
|
||||
@@ -1192,17 +1234,17 @@ function initSvgEditor(
|
||||
poly.setAttribute('style', 'cursor:pointer')
|
||||
poly.setAttribute('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-en', memo ? memo.en || 'New zone' : 'New zone')
|
||||
poly.setAttribute('data-anno-name-fr', frName)
|
||||
poly.setAttribute('data-anno-name-en', enName)
|
||||
poly.setAttribute('data-anno-category', cat)
|
||||
if (CATEGORIES[cat]?.name)
|
||||
poly.setAttribute('data-anno-category-name', CATEGORIES[cat].name)
|
||||
if (CATEGORIES[cat]?.special)
|
||||
poly.setAttribute('data-anno-special', CATEGORIES[cat].special!)
|
||||
const t = document.createElementNS('http://www.w3.org/2000/svg', 'title')
|
||||
t.textContent =
|
||||
const titleEl = document.createElementNS('http://www.w3.org/2000/svg', 'title')
|
||||
titleEl.textContent =
|
||||
poly.getAttribute('data-anno-name-fr') + ' / ' + poly.getAttribute('data-anno-name-en')
|
||||
poly.appendChild(t)
|
||||
poly.appendChild(titleEl)
|
||||
overlayG.appendChild(poly)
|
||||
|
||||
cancelPolygon()
|
||||
@@ -1243,7 +1285,7 @@ function initSvgEditor(
|
||||
if (Object.keys(CATEGORIES).length === 0) {
|
||||
const emptyMsg = document.createElement('div')
|
||||
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)
|
||||
return
|
||||
}
|
||||
@@ -1394,7 +1436,7 @@ Current: ${current || '(none)'}`
|
||||
const name = (getEl('svg-editor-new-cat-name') as HTMLInputElement).value.trim()
|
||||
const color = (getEl('svg-editor-new-cat-color') as HTMLInputElement).value
|
||||
if (!name) {
|
||||
alert('Give the category a name.')
|
||||
alert(t('svg_editor.alert_cat_name_required'))
|
||||
return
|
||||
}
|
||||
const slug = name
|
||||
@@ -1404,12 +1446,12 @@ Current: ${current || '(none)'}`
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/^-|-$/g, '')
|
||||
if (!slug) {
|
||||
alert('Invalid name.')
|
||||
alert(t('svg_editor.alert_cat_invalid_name'))
|
||||
return
|
||||
}
|
||||
|
||||
if (CATEGORIES[slug]) {
|
||||
alert('This category already exists.')
|
||||
alert(t('svg_editor.alert_cat_exists'))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1424,8 +1466,10 @@ Current: ${current || '(none)'}`
|
||||
const count = svgRoot.querySelectorAll(
|
||||
`[data-anno-category="${cat}"]:not([fill="transparent"])`,
|
||||
).length
|
||||
let msg = `Delete category "${CATEGORIES[cat].name}"?`
|
||||
if (count > 0) msg += `\n\nIt contains ${count} element(s) that will also be deleted.`
|
||||
let msg = t('svg_editor.confirm_delete_cat').replace('{name}', CATEGORIES[cat].name)
|
||||
if (count > 0) {
|
||||
msg += t('svg_editor.confirm_delete_cat_count').replace('{count}', String(count))
|
||||
}
|
||||
if (!confirm(msg)) return
|
||||
|
||||
svgRoot.querySelectorAll(`[data-anno-category="${cat}"]`).forEach((svgEl) => {
|
||||
@@ -1566,9 +1610,9 @@ Current: ${current || '(none)'}`
|
||||
m.setAttribute('r', '6')
|
||||
m.classList.add('polygon-midhandle')
|
||||
m.setAttribute('data-edge-idx', String(i))
|
||||
const t = document.createElementNS('http://www.w3.org/2000/svg', 'title')
|
||||
t.textContent = 'Click to insert a vertex here'
|
||||
m.appendChild(t)
|
||||
const titleEl = document.createElementNS('http://www.w3.org/2000/svg', 'title')
|
||||
titleEl.textContent = t('svg_editor.handle_insert')
|
||||
m.appendChild(titleEl)
|
||||
handlesG.appendChild(m)
|
||||
}
|
||||
|
||||
@@ -1579,9 +1623,9 @@ Current: ${current || '(none)'}`
|
||||
h.setAttribute('r', '8')
|
||||
h.classList.add('polygon-handle')
|
||||
h.setAttribute('data-vertex-idx', String(i))
|
||||
const t = document.createElementNS('http://www.w3.org/2000/svg', 'title')
|
||||
t.textContent = 'Drag to move | Alt+click or right-click to delete'
|
||||
h.appendChild(t)
|
||||
const titleEl = document.createElementNS('http://www.w3.org/2000/svg', 'title')
|
||||
titleEl.textContent = t('svg_editor.handle_drag')
|
||||
h.appendChild(titleEl)
|
||||
handlesG!.appendChild(h)
|
||||
})
|
||||
}
|
||||
@@ -1593,7 +1637,7 @@ Current: ${current || '(none)'}`
|
||||
|
||||
function restartCurrentPolygon() {
|
||||
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 = {
|
||||
id: selected.getAttribute('data-anno-id') || '',
|
||||
fr: selected.getAttribute('data-anno-name-fr') || '',
|
||||
@@ -1646,7 +1690,7 @@ Current: ${current || '(none)'}`
|
||||
|
||||
function exportSVG() {
|
||||
if (!svgRoot) {
|
||||
alert('No SVG loaded. Upload a file first.')
|
||||
alert(t('svg_editor.alert_no_svg_loaded'))
|
||||
return
|
||||
}
|
||||
const clone = svgRoot.cloneNode(true) as SVGSVGElement
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
.svg-editor-root {
|
||||
display: flex;
|
||||
height: 100vh;
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
overflow: hidden;
|
||||
font-family: var(--font-sans);
|
||||
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 .welcome { color: var(--color-accent); }
|
||||
.svg-editor-canvas.dragging-over::after {
|
||||
content: 'Drop SVG here';
|
||||
content: attr(data-drop-label);
|
||||
position: absolute;
|
||||
inset: 10px;
|
||||
display: flex;
|
||||
|
||||
Reference in new issue
Block a user