Files
hyperframes/packages/studio/src/utils/studioFontHelpers.ts
T
James 7e0a447325 refactor: drop unused exports detected by fallow auto-fix
Run `fallow fix --auto-fixable` to remove `export` keywords from symbols
fallow's reachability analysis identifies as unused. Keeps only the cases
where the symbol is still referenced internally in its own file (so
removing `export` doesn't surface a new oxlint `no-unused-vars` error).

Result: fallow dead-code findings drop from 276 → 208 (68 fewer unused
exports), with no behavior change — each symbol is still defined and used
exactly the same way within its file.

Reverted ~20 files where fallow's auto-fix would have created cascading
"declared but never used" lint errors — those are cases where the symbol
isn't used at all, and properly cleaning them up means deleting the
declaration, not just dropping `export`. Better to land that as a
separate, narrower PR rather than mixing it into a mechanical de-export.

Also reverted four false positives where fallow missed real consumers:
- `captureCost.ts` (renderOrchestrator has two separate import blocks
  from the same module; fallow only saw the first)
- `propertyPanelHelpers.ts`, `domEditingLayers.ts` (real internal uses
  fallow's reachability missed)
- `render.ts` (functions imported via `await import()` dynamic import,
  which fallow's static analysis doesn't follow)

Test plan: bun run --filter '*' typecheck (clean), oxlint + oxfmt clean,
cli/core/studio/engine vitest suites pass (335 + 917 + 576 + 605 tests).
2026-05-19 00:51:56 +00:00

84 lines
2.5 KiB
TypeScript

import { googleFontStylesheetUrl } from "../components/editor/fontCatalog";
import { importedFontFaceCss, type ImportedFontAsset } from "../components/editor/fontAssets";
import { toRelativeProjectAssetPath } from "./studioHelpers";
const GENERIC_FONT_FAMILIES = new Set([
"inherit",
"initial",
"revert",
"revert-layer",
"serif",
"sans-serif",
"monospace",
"cursive",
"fantasy",
"system-ui",
"ui-sans-serif",
"ui-serif",
"ui-monospace",
"ui-rounded",
"emoji",
"math",
"fangsong",
]);
function primaryFontFamilyFromCss(value: string): string {
const first = value.split(",")[0] ?? "";
return first.trim().replace(/^["']|["']$/g, "");
}
export function primaryFontFamilyValue(value: string): string {
return (
value
.split(",")[0]
?.trim()
.replace(/^["']|["']$/g, "")
.trim() ?? ""
);
}
export function injectPreviewGoogleFont(doc: Document, fontFamilyValue: string): void {
const family = primaryFontFamilyFromCss(fontFamilyValue);
if (!family || GENERIC_FONT_FAMILIES.has(family.toLowerCase())) return;
const id = `studio-preview-google-font-${family.toLowerCase().replace(/[^a-z0-9]+/g, "-")}`;
if (doc.getElementById(id)) return;
const link = doc.createElement("link");
link.id = id;
link.rel = "stylesheet";
link.href = googleFontStylesheetUrl(family);
doc.head.appendChild(link);
}
export function injectPreviewImportedFont(doc: Document, asset: ImportedFontAsset): void {
const id = `studio-imported-font-${asset.family.toLowerCase().replace(/[^a-z0-9]+/g, "-")}`;
if (doc.getElementById(id)) return;
const style = doc.createElement("style");
style.id = id;
style.textContent = importedFontFaceCss(asset);
doc.head.appendChild(style);
}
export function ensureImportedFontFace(
html: string,
asset: ImportedFontAsset,
sourceFile: string,
): string {
const css = importedFontFaceCss(asset, toRelativeProjectAssetPath(sourceFile, asset.path));
if (html.includes(css)) return html;
const styleRe = /<style\b[^>]*data-hf-studio-fonts=(["'])true\1[^>]*>([\s\S]*?)<\/style>/i;
const styleMatch = styleRe.exec(html);
if (styleMatch) {
const nextCss = `${styleMatch[2].trim()}\n${css}`.trim();
return html.replace(styleMatch[0], `<style data-hf-studio-fonts="true">\n${nextCss}\n</style>`);
}
const styleTag = `<style data-hf-studio-fonts="true">\n${css}\n</style>`;
if (/<\/head>/i.test(html)) {
return html.replace(/<\/head>/i, ` ${styleTag}\n </head>`);
}
return `${styleTag}\n${html}`;
}