Repository Public
1"use client";23import { useEffect, useRef } from "react";45/**6 * A display formula wider than its box is reduced until it fits (down to 55 % — below that it scrolls, as before).7 * One instance per sheet: it watches the sheet for size and content changes. Renders nothing visible.8 *9 * The size is computed, never searched: the width of the typeset formula is read at the current size and scaled10 * back to the natural size, so a pass never has to undo the previous one (no flicker, same answer every time).11 */12const TARGETS = ".sh-equation-math, :not(.sh-equation-math):not(.fx-math) > .sh-math--display, .fx-math";13const MIN = 0.55;1415/** Width of the typeset formula inside `el`, in px at its current font size; 0 when there is none. */16function contentWidth(el: HTMLElement): number {17 // The union of the top-level boxes KaTeX laid out (they report their real position even where they overflow).18 let left = Infinity, right = -Infinity;19 for (const box of el.querySelectorAll<HTMLElement>(".katex-html > *")) {20 const r = box.getBoundingClientRect();21 if (r.width === 0) continue;22 if (r.left < left) left = r.left;23 if (r.right > right) right = r.right;24 }25 return right > left ? right - left : 0;26}2728export function FitMath() {29 const ref = useRef<HTMLSpanElement>(null);30 useEffect(() => {31 const root = ref.current?.closest<HTMLElement>("[data-fitroot], .sheet");32 if (!root || typeof ResizeObserver === "undefined") return;33 let raf = 0;34 const fit = () => {35 raf = 0;36 const els = [...root.querySelectorAll<HTMLElement>(TARGETS)];37 // Read everything, then write everything: one layout pass.38 const plan = els.map((el) => {39 const cs = getComputedStyle(el);40 const cur = parseFloat(cs.fontSize);41 // The natural size is the one in force while no size is set here; it is remembered per element.42 if (!el.style.fontSize) el.dataset.fitBase = String(cur);43 const base = Number(el.dataset.fitBase) || cur;44 const have = el.clientWidth - parseFloat(cs.paddingLeft) - parseFloat(cs.paddingRight);45 const natural = contentWidth(el) * (base / cur);46 const k = have > 0 && natural > have ? Math.max(MIN, (have / natural) * 0.98) : 1;47 return { el, size: k < 1 ? `${(base * k).toFixed(2)}px` : "" };48 });49 for (const { el, size } of plan) {50 if (el.style.fontSize !== size) { el.style.transition = "none"; el.style.fontSize = size; }51 }52 };53 const schedule = () => { if (!raf) raf = requestAnimationFrame(fit); };54 const ro = new ResizeObserver(schedule);55 ro.observe(root);56 const mo = new MutationObserver(schedule);57 mo.observe(root, { childList: true, subtree: true });58 // The maths fonts arrive after the first layout and are wider than the fallback: measure again when they land.59 void document.fonts?.ready.then(schedule);60 document.fonts?.addEventListener("loadingdone", schedule);61 schedule();62 return () => { ro.disconnect(); mo.disconnect(); document.fonts?.removeEventListener("loadingdone", schedule); if (raf) cancelAnimationFrame(raf); };63 }, []);64 return <span ref={ref} hidden />;65}66