/** * The resolved customization, as React context. The `layout` option and the * user's extra icons are resolved once in ; the layout at the * viewer's current size is resolved by (layout-root.tsx), and read * here by the shell, menus and strips — no component imports the default * layout directly, so "the host changed the layout" is invisible below this * line. * * The default context values are the default config and the default layout: * components render correctly in tests and stories without a provider. */ import { createContext, useContext } from 'react'; import { EXPANDED_MIN_WIDTH_PX, barsAt, compileBar, menuOf, type LayoutData, type LayoutEnv, type LayoutOption, type MenuSection, type SelectionTarget, } from '@embedpdf/core-ui'; import type { BarGroup, BarSchema } from '@embedpdf/react/toolbar'; import { DEFAULT_LAYOUT } from './config/chrome'; import type { IconDef } from './ui/icons'; import { NO_VIEWER_COMMANDS, type ViewerCommands } from './config/viewer-commands'; import type { ViewerSignatureSettings, ViewerStampSettings } from './plugin-settings'; /** * A font of the app's, beyond the standard 14 (the viewer's `fonts` setting): registered on a * local engine under its key, which the style panel then offers for text boxes, form fields and * redaction labels. */ export interface ViewerFont { /** What a `fontFamily` names it by, and the engine's key for it. Not one of the 14 standard names. */ readonly key: string; /** Where the TTF or OTF file comes from (same-origin or CORS-enabled). */ readonly url?: string; /** The file's bytes, in place of `url`. */ readonly data?: Uint8Array | ArrayBuffer; /** The family's name, written into the PDF and matched by rich text; inferred from the file when left out. */ readonly familyName?: string; readonly weight?: number; readonly italic?: boolean; /** What the style panel calls it; else its family's name. */ readonly label?: string; } /** What `onPanelResize` hears: the panel, and the width the user gave it. */ export interface PanelResizeEvent { /** The panel's id. */ readonly id: string; /** Its width now, in px. */ readonly width: number; } export interface ResolvedViewerConfig { /** The `layout` option as given: the default without one. */ readonly layout?: LayoutOption; /** The `size` option as given: what the env's size is, or `'auto'` (or nothing) to measure. */ readonly size?: unknown; /** The `touch` option as given: what the env's touch is, or `'auto'` (or nothing) to ask. */ readonly touch?: unknown; /** User-registered icons — additive over the built-in set. */ readonly icons: Readonly>; /** Your commands over the built-in ones, for the commands the layout brings too. */ readonly viewerCommands: ViewerCommands; /** What the viewer adds to `plugins.stamp`: the standard stamps, your own, the quick stamps. */ readonly stamp: ViewerStampSettings; /** What the viewer adds to `plugins.signature`: which marks people keep, their fonts. */ readonly signature: ViewerSignatureSettings; /** The engine's fonts the style panel offers (see {@link ViewerFont}). */ readonly fonts: readonly ViewerFont[]; /** Whether leaving the page asks first while a document has unsaved changes. */ readonly warnBeforeLeaving: boolean; /** Hears each width the user gives a panel, by dragging its edge or with its keys. */ readonly onPanelResize?: (event: PanelResizeEvent) => void; } const DEFAULT_CONFIG: ResolvedViewerConfig = { icons: {}, viewerCommands: NO_VIEWER_COMMANDS, stamp: {}, signature: {}, fonts: [], warnBeforeLeaving: true, }; const ViewerConfigContext = createContext(DEFAULT_CONFIG); export const ViewerConfigProvider = ViewerConfigContext.Provider; /** The `layout` option, for to resolve at the viewer's size. */ export function useLayoutOption(): LayoutOption | undefined { return useContext(ViewerConfigContext).layout; } /** The `size` and `touch` options as given, for to read into the env. */ export function useEnvOptions(): { readonly size: unknown; readonly touch: unknown } { const { size, touch } = useContext(ViewerConfigContext); return { size, touch }; } /** Whether leaving the page asks first while a document has unsaved changes. */ export function useWarnBeforeLeaving(): boolean { return useContext(ViewerConfigContext).warnBeforeLeaving; } /** Tells the app a panel's new width: what the panel frame calls once the user has resized it. */ export function usePanelResize(): (event: PanelResizeEvent) => void { const { onPanelResize } = useContext(ViewerConfigContext); return (event) => onPanelResize?.(event); } // ── the layout at the viewer's current size ────────────────────────────────── /** The layout as the viewer shows it now: at its env, with what shows nothing left out. */ export interface ResolvedLayout { /** The layout's data at this env. */ readonly data: LayoutData; readonly env: LayoutEnv; /** A bar as its toolbar fits it, or `null` for a bar the layout doesn't have. */ bar(id: string): BarSchema | null; /** The bars over a selection of this kind, as one bar, or `null` when there is none. */ strip(target: SelectionTarget): BarSchema | null; /** A menu's sections, or `null` for a menu the layout doesn't have. */ menu(id: string): readonly MenuSection[] | null; } /** The env before the viewer is measured, and without a . */ export const DEFAULT_ENV: LayoutEnv = { size: 'expanded', width: EXPANDED_MIN_WIDTH_PX, touch: false, dir: 'ltr', }; /** * The bars over a selection of this kind as one bar, read through `bar`: one bar as it is, or * several as one, their groups in bar order and named after their bar so the names stay unique. */ export function stripOf( data: LayoutData, target: SelectionTarget, bar: (id: string) => BarSchema | null, ): BarSchema | null { const bars = barsAt(data, target) .map(bar) .filter((each): each is BarSchema => each !== null); if (bars.length <= 1) return bars[0] ?? null; const sections: { -readonly [Name in 'start' | 'center' | 'end']?: readonly BarGroup[] } = {}; for (const name of ['start', 'center', 'end'] as const) { const groups = bars.flatMap((each) => (each.sections[name] ?? []).map((group) => ({ ...group, id: `${each.id}.${group.id}` })), ); if (groups.length > 0) sections[name] = groups; } return { id: target, sections }; } /** The layout as plain reads of its data: no bar leaves anything out. */ export function staticResolvedLayout(data: LayoutData, env: LayoutEnv): ResolvedLayout { const bar = (id: string) => compileBar(data, id); return { data, env, bar, strip: (target) => stripOf(data, target, bar), menu: (id) => menuOf(data, id), }; } const ResolvedLayoutContext = createContext( staticResolvedLayout(DEFAULT_LAYOUT, DEFAULT_ENV), ); export const ResolvedLayoutProvider = ResolvedLayoutContext.Provider; export function useResolvedLayout(): ResolvedLayout { return useContext(ResolvedLayoutContext); } /** The viewer's env: its size, width, whether it's touched, and its direction. */ export function useLayoutEnv(): LayoutEnv { return useContext(ResolvedLayoutContext).env; } /** A menu's sections from the layout, or `null` for a menu it doesn't have. */ export function useMenu(id: string): readonly MenuSection[] | null { return useContext(ResolvedLayoutContext).menu(id); } /** The bars over a selection of this kind, as one bar; `undefined` when the layout has none. */ export function useStripSchema(target: SelectionTarget): BarSchema | undefined { return useContext(ResolvedLayoutContext).strip(target) ?? undefined; } /** Your commands over the built-in ones: what the layout's panel and mode commands merge under. */ export function useViewerCommands(): ViewerCommands { return useContext(ViewerConfigContext).viewerCommands; } export function useCustomIcons(): Readonly> { return useContext(ViewerConfigContext).icons; } export function useStampSettings(): ViewerStampSettings { return useContext(ViewerConfigContext).stamp; } export function useSignatureSettings(): ViewerSignatureSettings { return useContext(ViewerConfigContext).signature; } export function useConfiguredFonts(): readonly ViewerFont[] { return useContext(ViewerConfigContext).fonts; }