chore: remove dead code and duplicate logic

- drop unused assertNever, canvg dependency (never imported),
  readonly re-export shim in VertebraPicker, stale equine named export
- unexport validateBoneElement (internal only)
- getBoneIdFromEvent now delegates to getAnnotatedElement (dup walk-up)
- rasterizeSvg: drop ignored _scale param (2 call sites + tests)
- extract byte-identical word-wrap into pdf/render/text.ts
- extract mergeBones helper in EditorRoute (block duplicated twice)
- fix stale canvg comment in observationPage
Checks: typecheck, lint, 174/174 tests, build all green
This commit is contained in:
Bastien Chanot committed 2026-07-12 04:47:54 +02:00
1 parent c55da07697
commit bbdc0e851a
14 files changed
+55 -141

No files matched your search

+2 -8
View File
@@ -12,7 +12,7 @@ import type { Bone, BoneCategory } from './types'
* string is accepted. New SVGs are free to introduce new categories
* without touching this parser.
*/
export function validateBoneElement(el: Element): void {
function validateBoneElement(el: Element): void {
const boneId = el.getAttribute('data-anno-id') ?? ''
if (!boneId) {
throw new SvgConventionError(
@@ -94,13 +94,7 @@ export function parseSvgBones(svgRoot: SVGSVGElement): Map<string, Bone> {
* annotated element.
*/
export function getBoneIdFromEvent(event: Event): string | null {
let node = event.target as Element | null
while (node && node.nodeType === 1) {
const id = node.getAttribute?.('data-anno-id')
if (id) return id
node = node.parentElement
}
return null
return getAnnotatedElement(event)?.getAttribute('data-anno-id') || null
}
export function getAnnotatedElement(event: Event): Element | null {
+13 -17
View File
@@ -3,9 +3,8 @@ import { useNavigate } from 'react-router-dom'
import AnatomyCanvas from '@/components/canvas/AnatomyCanvas'
import DetailModal from '@/components/canvas/DetailModal'
import ObservationList from '@/components/observation/ObservationList'
import VertebraPicker, {
regionFromLabel,
} from '@/components/observation/VertebraPicker'
import VertebraPicker from '@/components/observation/VertebraPicker'
import { regionFromLabel } from '@/lib/spineZones'
import LateralityPicker from '@/components/observation/LateralityPicker'
import type { LateralSide } from '@/domain/schema/observation'
import ConclusionPanel from '@/components/report/ConclusionPanel'
@@ -32,6 +31,15 @@ import { boneLabel } from '@/anatomy/labels'
const DISCLAIMER_FLAG = 'osteo:disclaimer-shown'
/** Merges newly parsed bones into the accumulated map without overwriting. */
function mergeBones(prev: Map<string, Bone>, parsed: Map<string, Bone>) {
const merged = new Map(prev)
parsed.forEach((b, id) => {
if (!merged.has(id)) merged.set(id, b)
})
return merged
}
export default function EditorRoute() {
const navigate = useNavigate()
const t = useT()
@@ -135,13 +143,7 @@ export default function EditorRoute() {
loadSvg(path)
.then((root) => {
const parsed = parseSvgBones(root)
setDetailBones((prev) => {
const merged = new Map(prev)
parsed.forEach((b, id) => {
if (!merged.has(id)) merged.set(id, b)
})
return merged
})
setDetailBones((prev) => mergeBones(prev, parsed))
})
.catch((err) => {
console.warn(`EditorRoute: failed to preload detail SVG ${path}`, err)
@@ -162,13 +164,7 @@ export default function EditorRoute() {
loadSvg(detail.src).then((root) => {
if (cancelled) return
const parsed = parseSvgBones(root)
setDetailBones((prev) => {
const merged = new Map(prev)
parsed.forEach((b, id) => {
if (!merged.has(id)) merged.set(id, b)
})
return merged
})
setDetailBones((prev) => mergeBones(prev, parsed))
})
return () => {
cancelled = true
@@ -6,8 +6,6 @@ import type { Vertebra, VertebralRegion } from '@/domain/schema/observation'
import type { VertebralFormula } from '@/domain/species/types'
import { useT } from '@/i18n/useT'
export { regionFromLabel } from '@/lib/spineZones'
const REGIONS: VertebralRegion[] = ['C', 'T', 'L', 'S', 'Cd']
const REGION_KEY: Record<VertebralRegion, string> = {
C: 'vertebra.region.C',
+2 -3
View File
@@ -1,7 +1,7 @@
import type { SpeciesManifest } from '@/domain/species/types'
import { EQUINE_VERTEBRAL_FORMULA } from './vertebrae'
export const equine: SpeciesManifest = {
const equine: SpeciesManifest = {
id: 'equine',
labelFr: 'Cheval',
mainSvg: '/species/equine/Horse.svg',
@@ -13,6 +13,5 @@ export const equine: SpeciesManifest = {
}
// Default export is the convention picked up by the registry's
// import.meta.glob() auto-discovery. Keep the named export for any
// code that still imports the manifest by name.
// import.meta.glob() auto-discovery.
export default equine
-8
View File
@@ -7,11 +7,3 @@ export function assert(condition: unknown, message: string): asserts condition {
throw new Error(`Assertion failed: ${message}`)
}
}
/**
* Marks a code path as unreachable for exhaustive switch coverage.
* TypeScript will error at compile time if the path is reachable.
*/
export function assertNever(value: never, message?: string): never {
throw new Error(message ?? `Unexpected value: ${JSON.stringify(value)}`)
}
+1 -1
View File
@@ -130,7 +130,7 @@ export async function exportReportToPdf(
const resp = await fetch('/Paws_in_harmony.svg')
if (!resp.ok) throw new Error(`Paws logo fetch failed: HTTP ${resp.status}`)
const svgText = await resp.text()
const pawsPngBytes = await rasterizeSvg(svgText, 1)
const pawsPngBytes = await rasterizeSvg(svgText)
pawsLogoPdf = await pdfDoc.embedPng(pawsPngBytes)
} catch (err) {
console.warn('Paws in Harmony logo embed failed:', err)
+3 -19
View File
@@ -1,9 +1,10 @@
import { rgb, PageSizes, type PDFDocument, type PDFFont } from 'pdf-lib'
import { rgb, PageSizes, type PDFDocument } from 'pdf-lib'
import type { EmbeddedFonts } from '@/pdf/fonts'
import type { Report } from '@/domain/schema/report'
import type { Locale } from '@/i18n/types'
import { getDict } from '@/i18n'
import { drawHeader, type HeaderAssets } from './header'
import { wrapLines } from './text'
import { formatDate } from '@/lib/dates'
const MARGIN_X = 36
@@ -20,23 +21,6 @@ const COLOR_INK = rgb(0.12, 0.11, 0.09)
const COLOR_MUTED = rgb(0.42, 0.39, 0.35)
const COLOR_ACCENT = rgb(0.72, 0.35, 0.25)
function wrap(text: string, font: PDFFont, size: number, max: number): string[] {
const words = text.split(/\s+/)
const lines: string[] = []
let line = ''
for (const w of words) {
const test = line ? `${line} ${w}` : w
if (font.widthOfTextAtSize(test, size) > max && line) {
lines.push(line)
line = w
} else {
line = test
}
}
if (line) lines.push(line)
return lines
}
export async function addConclusionPage(
pdfDoc: PDFDocument,
report: Report,
@@ -101,7 +85,7 @@ export async function addConclusionPage(
color: COLOR_ACCENT,
})
cursorY -= SECTION_LABEL_GAP + 10
wrap(body, fonts.bodyRegular, 11, max).forEach((line) => {
wrapLines(body, fonts.bodyRegular, 11, max).forEach((line) => {
page.drawText(line, {
x: MARGIN_X,
y: cursorY,
+4 -20
View File
@@ -1,4 +1,4 @@
import { rgb, PageSizes, type PDFDocument, type PDFFont, type PDFPage } from 'pdf-lib'
import { rgb, PageSizes, type PDFDocument, type PDFPage } from 'pdf-lib'
import type { EmbeddedFonts } from '@/pdf/fonts'
import type { Report } from '@/domain/schema/report'
import type { Finding, Observation } from '@/domain/schema/observation'
@@ -8,6 +8,7 @@ import { getDict } from '@/i18n'
import type { ZoneLabelPair } from '@/pdf/export'
import { drawHeader, type HeaderAssets } from './header'
import { rasterizeSvg } from './svgToPng'
import { wrapLines } from './text'
import { loadSvg } from '@/anatomy/svgLoader'
import { parseSvgBones } from '@/anatomy/svgConvention'
import { getSpecies } from '@/domain/species/registry'
@@ -141,23 +142,6 @@ function drawSegmentedText(
return y - TEXT_LINE_HEIGHT
}
function wrapLines(text: string, font: PDFFont, size: number, max: number): string[] {
const words = text.split(/\s+/)
const lines: string[] = []
let line = ''
for (const w of words) {
const test = line ? `${line} ${w}` : w
if (font.widthOfTextAtSize(test, size) > max && line) {
lines.push(line)
line = w
} else {
line = test
}
}
if (line) lines.push(line)
return lines
}
const SVG_NS = 'http://www.w3.org/2000/svg'
const HALO_FILTER_ID = 'osteo-halo'
@@ -203,7 +187,7 @@ function ensureHaloFilter(root: SVGSVGElement): void {
const merge = doc.createElementNS(SVG_NS, 'feMerge')
// Stack the wide halo three times so its contribution survives the
// canvg → PNG → pdf-lib pipeline (each pass attenuates alpha) and
// SVG → PNG → pdf-lib pipeline (each pass attenuates alpha) and
// still reads as a strong glow at print scale.
for (let i = 0; i < 3; i += 1) {
const n = doc.createElementNS(SVG_NS, 'feMergeNode')
@@ -359,7 +343,7 @@ export async function addObservationPage(
const rendered = await Promise.all(
items.map(async (it) => {
const svgStr = await colorizedSvgString(it.src, observation, it.sourceId)
const png = await rasterizeSvg(svgStr, 2)
const png = await rasterizeSvg(svgStr)
const img = await pdfDoc.embedPng(png)
const aspect = img.width > 0 ? img.height / img.width : 0.6
return { label: it.label, img, aspect }
+1 -7
View File
@@ -113,14 +113,8 @@ async function canvasToPngBytes(canvas: HTMLCanvasElement): Promise<Uint8Array>
* the inline anatomy canvas.
*
* @param svgString Serialized SVG (must declare `xmlns`)
* @param _scale Reserved — current implementation renders at a fixed
* TARGET_WIDTH derived from the viewBox; kept for API
* compatibility.
*/
export async function rasterizeSvg(
svgString: string,
_scale: number,
): Promise<Uint8Array> {
export async function rasterizeSvg(svgString: string): Promise<Uint8Array> {
const { widthPx, heightPx } = readViewBox(svgString)
const canvas = await rasterizeWithImageElement(svgString, widthPx, heightPx)
return canvasToPngBytes(canvas)
+27
View File
@@ -0,0 +1,27 @@
import type { PDFFont } from 'pdf-lib'
/**
* Greedy word-wrap: splits `text` into lines whose rendered width at
* `size` points stays within `max` for the given font.
*/
export function wrapLines(
text: string,
font: PDFFont,
size: number,
max: number,
): string[] {
const words = text.split(/\s+/)
const lines: string[] = []
let line = ''
for (const w of words) {
const test = line ? `${line} ${w}` : w
if (font.widthOfTextAtSize(test, size) > max && line) {
lines.push(line)
line = w
} else {
line = test
}
}
if (line) lines.push(line)
return lines
}