Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

786 B
Raw Blame
import { useEffect, useMemo, useState } from "react";
import { getLocalFontFallbackConfig } from "@editor/services/pdfiumFontFallback";
import { subscribeToFontBaseUrl } from "@editor/services/fontBaseUrl";

/**
 * PDFium fallback-font configuration, recomputed when the font base URL changes.
 * The desktop webview only learns the backend port after startup, so a config
 * memoized at mount would keep an empty base URL; `subscribeToFontBaseUrl` is a
 * no-op in the web build, where the base is known immediately.
 */
export function useFontFallbackConfig() {
  const [revision, setRevision] = useState(0);

  useEffect(
    () => subscribeToFontBaseUrl(() => setRevision((current) => current + 1)),
    [],
  );

  return useMemo(() => getLocalFontFallbackConfig(), [revision]);
}