Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | 10x 10x 10x 10x 5x 38x 38x 11x 11x 10x 10x 11x 11x 11x 3x 3x 7x 1x 1x 10x 4x 4x 38x 1x 1x 1x 38x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 5x 5x 11x 4x 11x 1x 1x 1x 1x 1x 1x 1x 11x 2x 11x 2x 11x 11x 4x 4x 4x 11x 4x 4x 4x 4x 2x 1x 1x 1x 2x 4x 4x 4x 4x 4x 4x 4x 4x 11x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 4x 4x 4x 4x 4x | "use client";
/**
* DocFigureViewer: the full-screen view of a diagram in a published report.
*
* The reports' Markdown is rendered to HTML at build time, so the figures are
* plain `<a class="doc-figure"><img></a>` elements and not React components.
* This wrapper listens for a click on one of them, or on a bare <img> such
* as a screenshot, and opens the image in an overlay that fills the viewport, with zoom (buttons, mouse wheel, pinch,
* double click, keyboard) and drag to pan. A click with a modifier key keeps
* the link's own behaviour, so the file still opens in a new tab on request.
*/
import {
useCallback,
useEffect,
useRef,
useState,
type MouseEvent as ReactMouseEvent,
type PointerEvent as ReactPointerEvent,
type ReactNode,
type WheelEvent as ReactWheelEvent,
} from "react";
import { createPortal } from "react-dom";
import {
ExternalLink,
Maximize2,
Minimize2,
Minus,
Plus,
Scan,
X,
} from "lucide-react";
import { useT } from "@/lib/i18n/translate";
const MIN_SCALE = 0.1;
const MAX_SCALE = 8;
const STEP = 1.25;
interface Figure {
src: string;
alt: string;
/** The element that opened the viewer, to give focus back on close. */
opener: HTMLElement | null;
}
interface View {
scale: number;
x: number;
y: number;
}
function clampScale(s: number): number {
return Math.min(MAX_SCALE, Math.max(MIN_SCALE, s));
}
export function DocFigureViewer({ children }: { children: ReactNode }) {
const [figure, setFigure] = useState<Figure | null>(null);
const onClick = useCallback((e: ReactMouseEvent<HTMLDivElement>) => {
Iif (e.defaultPrevented || e.button !== 0) return;
if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
const target = e.target as HTMLElement;
const anchor = target.closest?.("a.doc-figure");
let img: HTMLImageElement | null = null;
let opener: HTMLElement | null = null;
if (anchor instanceof HTMLAnchorElement) {
// A diagram: the link around it opens the file, a plain click the viewer.
img = anchor.querySelector("img");
opener = anchor;
} else if (target instanceof HTMLImageElement && !target.closest("a")) {
// A screenshot written as a bare <img> in the Markdown.
img = target;
opener = target;
}
if (!img) return;
e.preventDefault();
setFigure({
src: img.getAttribute("src") ?? img.src,
alt: img.alt,
opener,
});
}, []);
const close = useCallback(() => {
setFigure((f) => {
f?.opener?.focus?.();
return null;
});
}, []);
return (
<div onClick={onClick}>
{children}
{figure ? <FigureOverlay figure={figure} onClose={close} /> : null}
</div>
);
}
function FigureOverlay({
figure,
onClose,
}: {
figure: Figure;
onClose: () => void;
}) {
const t = useT("docsCommon");
const name = figure.alt || t("Figure");
const dialogRef = useRef<HTMLDivElement>(null);
const stageRef = useRef<HTMLDivElement>(null);
const closeRef = useRef<HTMLButtonElement>(null);
const [natural, setNatural] = useState<{ w: number; h: number } | null>(null);
const [view, setView] = useState<View>({ scale: 1, x: 0, y: 0 });
const [fullscreen, setFullscreen] = useState(false);
const pointers = useRef(new Map<number, { x: number; y: number }>());
const pinch = useRef<{ dist: number; scale: number } | null>(null);
const drag = useRef<{ x: number; y: number; vx: number; vy: number } | null>(
null,
);
const stageSize = useCallback(() => {
const el = stageRef.current;
return el
? { w: el.clientWidth, h: el.clientHeight }
: { w: window.innerWidth, h: window.innerHeight };
}, []);
/** The scale at which the whole diagram fits the stage, centred. */
const fit = useCallback(() => {
Eif (!natural) return;
const { w, h } = stageSize();
const scale = clampScale(Math.min(w / natural.w, h / natural.h) * 0.96);
setView({
scale,
x: (w - natural.w * scale) / 2,
y: (h - natural.h * scale) / 2,
});
}, [natural, stageSize]);
/** Zoom to `next`, keeping the stage point (px, py) over the same spot. */
const zoomTo = useCallback(
(next: number, px?: number, py?: number) => {
setView((v) => {
const scale = clampScale(next);
const { w, h } = stageSize();
const cx = px ?? w / 2;
const cy = py ?? h / 2;
const k = scale / v.scale;
return { scale, x: cx - (cx - v.x) * k, y: cy - (cy - v.y) * k };
});
},
[stageSize],
);
const zoomIn = useCallback(
() => setView((v) => applyZoom(v, v.scale * STEP, stageSize())),
[stageSize],
);
const zoomOut = useCallback(
() => setView((v) => applyZoom(v, v.scale / STEP, stageSize())),
[stageSize],
);
const actualSize = useCallback(() => {
if (!natural) return;
const { w, h } = stageSize();
setView({
scale: 1,
x: Math.max(0, (w - natural.w) / 2),
y: Math.max(0, (h - natural.h) / 2),
});
}, [natural, stageSize]);
// Fit the diagram once its size is known, and again when the stage resizes.
useEffect(() => {
fit();
window.addEventListener("resize", fit);
return () => window.removeEventListener("resize", fit);
}, [fit]);
// Keyboard, scroll lock, focus, and the browser's fullscreen state.
useEffect(() => {
closeRef.current?.focus();
const prev = document.body.style.overflow;
document.body.style.overflow = "hidden";
const onKey = (e: KeyboardEvent) => {
if (e.key === "Escape") {
Iif (document.fullscreenElement) {
document.exitFullscreen?.().catch(() => undefined);
return;
}
onClose();
} else if (e.key === "+" || e.key === "=") zoomIn();
else Eif (e.key === "-" || e.key === "_") zoomOut();
else if (e.key === "0") fit();
else if (e.key === "1") actualSize();
else if (e.key.startsWith("Arrow")) {
const d = 60;
setView((v) => ({
...v,
x:
v.x + (e.key === "ArrowLeft" ? d : e.key === "ArrowRight" ? -d : 0),
y: v.y + (e.key === "ArrowUp" ? d : e.key === "ArrowDown" ? -d : 0),
}));
} else return;
e.preventDefault();
};
const onFs = () => setFullscreen(Boolean(document.fullscreenElement));
window.addEventListener("keydown", onKey);
document.addEventListener("fullscreenchange", onFs);
return () => {
window.removeEventListener("keydown", onKey);
document.removeEventListener("fullscreenchange", onFs);
document.body.style.overflow = prev;
Iif (document.fullscreenElement) {
document.exitFullscreen?.().catch(() => undefined);
}
};
}, [onClose, zoomIn, zoomOut, fit, actualSize]);
const onWheel = (e: ReactWheelEvent<HTMLDivElement>) => {
const rect = stageRef.current?.getBoundingClientRect();
const px = e.clientX - (rect?.left ?? 0);
const py = e.clientY - (rect?.top ?? 0);
const factor = Math.exp(-e.deltaY * (e.ctrlKey ? 0.01 : 0.002));
zoomTo(view.scale * factor, px, py);
};
const onPointerDown = (e: ReactPointerEvent<HTMLDivElement>) => {
const stage = stageRef.current;
if (!stage) return;
stage.setPointerCapture?.(e.pointerId);
pointers.current.set(e.pointerId, { x: e.clientX, y: e.clientY });
if (pointers.current.size === 1) {
drag.current = { x: e.clientX, y: e.clientY, vx: view.x, vy: view.y };
} else if (pointers.current.size === 2) {
const [a, b] = [...pointers.current.values()];
pinch.current = {
dist: Math.hypot(a.x - b.x, a.y - b.y),
scale: view.scale,
};
drag.current = null;
}
};
const onPointerMove = (e: ReactPointerEvent<HTMLDivElement>) => {
if (!pointers.current.has(e.pointerId)) return;
pointers.current.set(e.pointerId, { x: e.clientX, y: e.clientY });
if (pointers.current.size === 2 && pinch.current) {
const [a, b] = [...pointers.current.values()];
const dist = Math.hypot(a.x - b.x, a.y - b.y);
const rect = stageRef.current?.getBoundingClientRect();
const mx = (a.x + b.x) / 2 - (rect?.left ?? 0);
const my = (a.y + b.y) / 2 - (rect?.top ?? 0);
zoomTo((pinch.current.scale * dist) / pinch.current.dist, mx, my);
} else if (drag.current) {
const d = drag.current;
setView((v) => ({
...v,
x: d.vx + (e.clientX - d.x),
y: d.vy + (e.clientY - d.y),
}));
}
};
const onPointerUp = (e: ReactPointerEvent<HTMLDivElement>) => {
pointers.current.delete(e.pointerId);
stageRef.current?.releasePointerCapture?.(e.pointerId);
pinch.current = null;
if (pointers.current.size === 1) {
const [p] = [...pointers.current.values()];
drag.current = { x: p.x, y: p.y, vx: view.x, vy: view.y };
} else drag.current = null;
};
const onDoubleClick = (e: ReactMouseEvent<HTMLDivElement>) => {
const rect = stageRef.current?.getBoundingClientRect();
const px = e.clientX - (rect?.left ?? 0);
const py = e.clientY - (rect?.top ?? 0);
if (!natural) return;
const { w, h } = stageSize();
const fitScale = Math.min(w / natural.w, h / natural.h) * 0.96;
if (view.scale > fitScale * 1.05) fit();
else zoomTo(fitScale * 2.5, px, py);
};
const toggleFullscreen = () => {
const el = dialogRef.current;
if (!el) return;
if (document.fullscreenElement) {
document.exitFullscreen?.().catch(() => undefined);
} else {
el.requestFullscreen?.().catch(() => undefined);
}
};
const percent = Math.round(view.scale * 100);
const btn =
"grid place-items-center h-9 min-w-9 px-2 rounded-md text-gray-200 hover:bg-white/15 focus-visible:outline focus-visible:outline-2 focus-visible:outline-white disabled:opacity-40";
return createPortal(
<div
ref={dialogRef}
role="dialog"
aria-modal="true"
aria-label={name}
data-testid="doc-figure-viewer"
className="fixed inset-0 z-100 flex flex-col bg-gray-950 text-gray-100 select-none"
>
<div className="flex items-center gap-1 px-2 py-1.5 border-b border-white/10 bg-gray-900/95 text-sm">
<div className="flex items-center gap-1">
<button
type="button"
className={btn}
onClick={zoomOut}
aria-label={t("Zoom out")}
title={t("Zoom out ({key})", { key: "-" })}
disabled={view.scale <= MIN_SCALE}
>
<Minus size={16} />
</button>
<span
className="tabular-nums w-14 text-center text-gray-300"
aria-live="polite"
data-testid="doc-figure-zoom"
>
{percent}%
</span>
<button
type="button"
className={btn}
onClick={zoomIn}
aria-label={t("Zoom in")}
title={t("Zoom in ({key})", { key: "+" })}
disabled={view.scale >= MAX_SCALE}
>
<Plus size={16} />
</button>
<button
type="button"
className={btn}
onClick={fit}
aria-label={t("Fit to screen")}
title={t("Fit to screen ({key})", { key: "0" })}
>
<Scan size={16} />
</button>
<button
type="button"
className={`${btn} text-xs font-medium`}
onClick={actualSize}
title={t("Actual size ({key})", { key: "1" })}
>
1:1
</button>
</div>
<p className="flex-1 min-w-0 truncate px-3 text-gray-400 hidden sm:block">
{name}
</p>
<div className="ml-auto flex items-center gap-1">
<a
href={figure.src}
target="_blank"
rel="noopener noreferrer"
className={btn}
aria-label={t("Open the file in a new tab")}
title={t("Open the file in a new tab")}
>
<ExternalLink size={16} />
</a>
<button
type="button"
className={btn}
onClick={toggleFullscreen}
aria-label={fullscreen ? t("Leave fullscreen") : t("Fullscreen")}
title={fullscreen ? t("Leave fullscreen") : t("Fullscreen")}
>
{fullscreen ? <Minimize2 size={16} /> : <Maximize2 size={16} />}
</button>
<button
ref={closeRef}
type="button"
className={btn}
onClick={onClose}
aria-label={t("Close")}
title={t("Close ({key})", { key: "Esc" })}
>
<X size={18} />
</button>
</div>
</div>
<div
ref={stageRef}
className="relative flex-1 min-h-0 overflow-hidden touch-none cursor-grab active:cursor-grabbing bg-white"
data-testid="doc-figure-stage"
onWheel={onWheel}
onPointerDown={onPointerDown}
onPointerMove={onPointerMove}
onPointerUp={onPointerUp}
onPointerCancel={onPointerUp}
onDoubleClick={onDoubleClick}
>
{/* The diagram is drawn at natural width times the scale, so a vector
file stays sharp at every zoom level, and moved with a transform. */}
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={figure.src}
alt={name}
draggable={false}
data-testid="doc-figure-image"
onLoad={(e) => {
const img = e.currentTarget;
const w = img.naturalWidth || img.clientWidth || 1200;
const h = img.naturalHeight || img.clientHeight || 800;
setNatural({ w, h });
}}
className="absolute top-0 left-0 max-w-none pointer-events-none"
style={{
width: natural ? natural.w * view.scale : undefined,
height: natural ? natural.h * view.scale : undefined,
transform: `translate(${view.x}px, ${view.y}px)`,
visibility: natural ? "visible" : "hidden",
}}
/>
{!natural && (
<p className="absolute inset-0 grid place-items-center text-gray-500">
{t("Loading the diagram")}
</p>
)}
</div>
<p className="px-3 py-1 text-xs text-gray-500 border-t border-white/10 bg-gray-900/95 hidden sm:block">
{t(
"Scroll or pinch to zoom, drag to move, double click to zoom in, 0 fits the screen, Esc closes.",
)}
</p>
</div>,
document.body,
);
}
function applyZoom(
v: View,
next: number,
stage: { w: number; h: number },
): View {
const scale = clampScale(next);
const cx = stage.w / 2;
const cy = stage.h / 2;
const k = scale / v.scale;
return { scale, x: cx - (cx - v.x) * k, y: cy - (cy - v.y) * k };
}
|