Repository Public
1import type { BlockOf, Locale } from "@/schema";2import { inlineToPlain } from "../inline";3import { measureText, wrapText } from "../diagram/text-measure";4import { t } from "../i18n";5import { V, toneSoftVar, toneVar } from "../tone";67type TimelineSpec = Pick<BlockOf<"timeline">, "items" | "orientation">;89/** Vector drawing of a timeline, for standalone export (the page itself renders a timeline as HTML). Pure SVG. */10export function TimelineSvg({ timeline, locale, label, desc }: { timeline: TimelineSpec; locale: Locale; label?: string; desc?: string }) {11 const name = label ?? t(locale, "timeline");12 const horizontal = timeline.orientation === "horizontal" && timeline.items.length <= 6;13 const colW = horizontal ? 190 : 440;14 const items = timeline.items.map((it) => ({15 when: it.when, tone: it.tone,16 title: wrapText(inlineToPlain(it.title), colW, 14, { weight: 600, maxLines: 3 }),17 text: it.text ? wrapText(inlineToPlain(it.text), colW, 12.5, { maxLines: 8 }) : [],18 }));19 const blockH = (it: (typeof items)[number]) => it.title.length * 19 + (it.text.length ? 4 + it.text.length * 17 : 0);20 if (horizontal) {21 const step = colW + 26, width = 24 + items.length * step, height = 86 + Math.max(...items.map(blockH)) + 16;22 return (23 <svg className="sh-timeline-svg" viewBox={`0 0 ${width} ${height}`} width={width} height={height} role="img" aria-label={name} fontFamily={V.fontBody}>24 <title>{name}</title>{desc ? <desc>{desc}</desc> : null}25 <path d={`M24 50H${width - 24}`} stroke={V.line} strokeWidth={2} strokeLinecap="round" />26 {items.map((it, i) => {27 const x = 24 + i * step;28 return (29 <g key={i}>30 <text x={x} y={28} fontSize={12} fontWeight={650} letterSpacing="0.04em" fill={toneVar(it.tone)}>{it.when}</text>31 <circle cx={x + 7} cy={50} r={7} fill={toneSoftVar(it.tone)} stroke={toneVar(it.tone)} strokeWidth={2} />32 <text fontSize={14} fontWeight={600} fill={V.ink}>{it.title.map((l, k) => <tspan key={k} x={x} y={86 + k * 19}>{l}</tspan>)}</text>33 <text fontSize={12.5} fill={V.muted}>{it.text.map((l, k) => <tspan key={k} x={x} y={86 + it.title.length * 19 + 4 + k * 17}>{l}</tspan>)}</text>34 </g>35 );36 })}37 </svg>38 );39 }40 const whenW = Math.min(150, Math.ceil(Math.max(...items.map((it) => measureText(it.when, 12, 650) * 1.05))));41 const axisX = 16 + whenW + 18, textX = axisX + 24, width = textX + colW + 16;42 const tops: number[] = []; let y = 20;43 for (const it of items) { tops.push(y); y += blockH(it) + 26; }44 const height = y - 6;45 return (46 <svg className="sh-timeline-svg" viewBox={`0 0 ${width} ${height}`} width={width} height={height} role="img" aria-label={name} fontFamily={V.fontBody}>47 <title>{name}</title>{desc ? <desc>{desc}</desc> : null}48 <path d={`M${axisX} ${tops[0] + 8}V${tops[tops.length - 1] + 8}`} stroke={V.line} strokeWidth={2} strokeLinecap="round" />49 {items.map((it, i) => (50 <g key={i}>51 <text x={axisX - 18} y={tops[i] + 13} textAnchor="end" fontSize={12} fontWeight={650} letterSpacing="0.04em" fill={toneVar(it.tone)}>{it.when}</text>52 <circle cx={axisX} cy={tops[i] + 8} r={7} fill={toneSoftVar(it.tone)} stroke={toneVar(it.tone)} strokeWidth={2} />53 <text fontSize={14} fontWeight={600} fill={V.ink}>{it.title.map((l, k) => <tspan key={k} x={textX} y={tops[i] + 13 + k * 19}>{l}</tspan>)}</text>54 <text fontSize={12.5} fill={V.muted}>{it.text.map((l, k) => <tspan key={k} x={textX} y={tops[i] + 13 + it.title.length * 19 + 4 + k * 17}>{l}</tspan>)}</text>55 </g>56 ))}57 </svg>58 );59}60