/**
* The viewer shell — the layout's bars around the pages, driven entirely by
* the commands + measured-toolbar system.
*
* ── header socket (empty unless a child fills it) ──────────────────────
* ── document tabs (or the `tabs` region) ───────────────────────────────
* ── the top edge's bars, outside in (main, its mode bar) ───────────────
* start │ start │ │ end │ end
* bars │ panel │ pages, the floating bars │ panel │ bars
* │ │ over them (with no document: │ │
* │ │ the `empty` region) │ │
* ── the bottom edge's bars, outside in from the bottom ─────────────────
* ── footer socket (empty unless a child fills it) ──────────────────────
*
* The top and bottom bars run the full width; the start and end bars stand
* between them at the viewer's edges, and a docked panel sits inside them,
* beside the pages (places.tsx draws the bars). The workspace/document split
* is structural: header + toolbars are workspace-scoped and render at t≈0
* (translated, measured) while the wasm engine still boots; everything
* document-scoped sits inside , whose fallback is the
* empty-workspace state, shown under a live toolbar rather than replacing the
* whole app. The floating bars float over the pages, so they come with them.
*
* Which mode bar shows is not stored anywhere: it's a projection of
* plugin-shell's exclusive 'mode' surface, read null-safely so the bar simply
* doesn't exist without a document.
*/
import { tabsOf } from '@embedpdf/core-ui';
import { useActionsUiAdapter } from '@embedpdf/react/actions';
import { AnnotationLayer, useFilePickerProvider } from '@embedpdf/react/annotation';
import { useCommandShortcuts } from '@embedpdf/react/commands';
import { FormLayer } from '@embedpdf/react/form';
import { useT } from '@embedpdf/react/i18n';
import { LinkLayer } from '@embedpdf/react/link';
import { useMeasurementState } from '@embedpdf/react/measurement';
import { RenderLayer, SnapshotLayer } from '@embedpdf/react/render';
import { DocumentGate, useDocument, useDocumentId } from '@embedpdf/react/runtime';
import { SearchLayer } from '@embedpdf/react/search';
import { SelectionClipboard, SelectionHandles, SelectionLayer } from '@embedpdf/react/selection';
import { useSurface } from '@embedpdf/react/shell';
import { Scrollbar, Stage } from '@embedpdf/react/stage';
import { useRef, type ReactNode } from 'react';
import { OUTLINE_PROPERTIES_DIALOG } from './config/outline';
import { useLayoutEnv, useResolvedLayout } from './config-context';
import { LayoutRoot } from './layout-root';
import { EdgeBars, FloatingBars } from './places';
import { AppFonts } from './ui/app-fonts';
import { AnnotationStrip } from './ui/annotation-strip';
import { DocumentError, PasswordPrompt } from './ui/document-boot';
import { DropToOpen } from './ui/drop-to-open';
import { GoToPageDialog } from './ui/go-to-page';
import { FeaturePrefetch, lazyComponent } from './ui/lazy';
import { PageLoading } from './ui/page-loading';
import { SidePanel } from './ui/panels';
import { PlacedComponentsHost, type PlacedComponents } from './ui/placed-components';
import { useRefusalNotice } from './ui/refusal-notice';
import { RotationBadge } from './ui/rotation-badge';
import { SelectionStrip } from './ui/selection-strip';
import { SnapshotStrip } from './ui/snapshot-strip';
import { ShortcutsSheet } from './ui/shortcuts-sheet';
import { SidebarReads } from './ui/sidebar-reads';
import { SignatureBridge } from './ui/signature-bridge';
import { StampLibraryStore } from './ui/stamp-store';
import { TabBar } from './ui/tab-bar';
import { ArmedToolCursor } from './ui/tool-cursor';
import { LeaveWarning } from './ui/unsaved-changes';
// The dialogs and the inspector of a feature: each loads the first time it opens.
const useRedactConfirmOpen = () => useSurface('redact-confirm').isOpen;
const useCalibrating = () => useMeasurementState((state) => state.calibrationRequest !== null);
const useSignDialogOpen = () => useSurface('signature-sign').isOpen;
const useSignatureMakerOpen = () => useSurface('signature-maker').isOpen;
const useSignatureInspectorOpen = () => useSurface('signature-inspector').isOpen;
const usePrintDialogOpen = () => useSurface('print').isOpen;
const RedactConfirmModal = lazyComponent(
() => import('./ui/redact-confirm').then((ui) => ui.RedactConfirmModal),
{ shownWhile: useRedactConfirmOpen },
);
const CalibrationDialog = lazyComponent(
() => import('./ui/measurement').then((ui) => ui.CalibrationDialog),
{ shownWhile: useCalibrating },
);
const SignDialog = lazyComponent(() => import('./ui/sign-dialog').then((ui) => ui.SignDialog), {
shownWhile: useSignDialogOpen,
});
const SignatureMakerModal = lazyComponent(
() => import('./ui/signature-maker').then((ui) => ui.SignatureMakerModal),
{ shownWhile: useSignatureMakerOpen },
);
const SignatureInspector = lazyComponent(
() => import('./ui/signature-inspector').then((ui) => ui.SignatureInspector),
{ shownWhile: useSignatureInspectorOpen },
);
const PrintDialog = lazyComponent(() => import('./ui/print-dialog').then((ui) => ui.PrintDialog), {
shownWhile: usePrintDialogOpen,
});
const useOutlinePropertiesOpen = () => useSurface(OUTLINE_PROPERTIES_DIALOG).isOpen;
// A grant's passphrase or questions: only documents from grants ask, so these load
// when they first show, never in the background.
const GrantPrompt = lazyComponent(() => import('./ui/grant-prompt').then((ui) => ui.GrantPrompt), {
prefetch: false,
});
const useIdentityAsked = () => useDocument((document) => document.identityRequest !== undefined);
const VisitorIdentityDialog = lazyComponent(
() => import('./ui/grant-prompt').then((ui) => ui.VisitorIdentityDialog),
{ shownWhile: useIdentityAsked, prefetch: false },
);
const OutlinePropertiesDialog = lazyComponent(
() => import('./ui/outline-properties').then((ui) => ui.OutlinePropertiesDialog),
{ shownWhile: useOutlinePropertiesOpen },
);
/** The document tabs, as the layout says: always, while more than one document is open, or never. */
function DocumentTabs() {
const visibility = tabsOf(useResolvedLayout().data);
if (visibility === 'never') return null;
return (
);
}
/** The empty-workspace state: shown while the initial documents (and the
* engine behind them) are still loading — or if the user closes every tab. */
function OpeningDocuments() {
const t = useT();
return (
);
}
/**
* The document area's lifecycle switch. With no document open it shows the
* `empty` region, or nothing. A `locked` tab shows its password prompt, an
* `error` tab its error pane, or the grant's passphrase or questions it
* waits for — per tab (keyed by document), so several can coexist and tab
* switching stays free. Everything else is the ready-gated Stage; the gate's
* fallback covers `loading`. A ready document whose grant asks again shows its
* passphrase or questions over the Stage.
*/
function DocumentArea({ children }: { children: ReactNode }) {
const docId = useDocumentId();
const status = useDocument((document) => document.status);
const grantAsks = useDocument((document) => document.openRequest !== undefined);
if (docId === null) {
return (
);
}
if (status === 'locked') return ;
if (status === 'error') {
return grantAsks ? : ;
}
return (
}>
{children}
{/* a passphrase or questions the grant got after the document opened: over the document,
which goes on once they're given */}
{grantAsks && (
)}
);
}
/**
* What's drawn over the pages: it follows the viewer's direction, while the pages under it keep
* their own left-to-right layout (the Stage is `direction: ltr`).
*/
function OverPages({ children }: { children: ReactNode }) {
const { dir } = useLayoutEnv();
return (
{children}
);
}
export function Shell({ components }: { components?: PlacedComponents }) {
// A shortcut or Copy that's refused says why in a notice.
const refusals = useRefusalNotice();
// The keys and copies reach this viewer only while focus is inside it (its own components,
// and yours slotted into it), so two viewers on a page never both take one, and the page
// keeps its own.
const root = useRef(null);
useCommandShortcuts({ target: root, onRefused: refusals.onShortcutRefused });
// Click-then-pick tools (stamp, file attachment) → click a page → file dialog
// opens → the file lands where you clicked. One line: the default provider is
// the built-in file picker honouring each tool's `accept` filter (swap it for
// a custom picker, or pass null to disable). The plugin stays DOM-free.
useFilePickerProvider();
// The one UI port for the action engine and every script-produced effect:
// sanitized URI opens, Named `Print`, script alerts (boot/lifecycle nags
// suppressed by default), and script page navigation.
useActionsUiAdapter();
// Regions render as named sockets with the built-ins as fallback —
// light-DOM children of replace them; in plain light DOM a
// slot just displays its fallback (UA display:contents), so this package's
// direct React consumers see no difference. A region nobody fills shows
// nothing, or the viewer's own (the tabs). The bars are measured and fit
// with zero config, and sit outside the gate: they render before any
// document. The root measures the viewer for the layout (layout-root.tsx).
return (
<>
{/* the app's fonts, registered once on a local engine */}
{/* the viewer's own components in the page (, ), each drawn
in its own shadow root */}
{components && }
{/* the user's stamp + signature libraries: restored once, persisted for
the workspace's lifetime (workspace-scoped, like the stamp plugin) */}
{/* leaving the page asks first while a document has unsaved changes */}
{/* The header socket. The chrome ships NO header of its own — branding,
locale pickers and theme switches are the embedder's chrome, not the
viewer's — so this renders nothing until a child fills the slot. */}
{/* the document tab bar — the kernel's document registry IS the tab model */}
{refusals.element}
{/* a PDF dropped here opens as a new tab, as Open in the document menu does */}
{/* the armed tool's cursor: its toolbar icon as a real CSS cursor
(zero-lag; the hub hides it over annotations/fields/gaps) */}
{/* ctrl/cmd+C and native Edit→Copy for the text selection, while focus is
inside the viewer — prefetches on commit so the copy event answers
synchronously. Renders nothing; mount once per document view. */}
{/* the floating bars: over the pages, under the bars over a selection */}
{/* the angle a selection is being turned to, by the pointer */}
{/* copy or save the box the snapshot tool holds */}
{/* a signed field's facts, anchored at its widget */}
{/* touch: draggable start/end selection handles (long-press
selects a word; the lollipops grow it from there) */}
{/* headless scrollbars: geometry/behavior from the stage's
scroller contract; the look is index.css (data-attrs) */}
}
className="h-full w-full"
style={{ background: 'var(--epdf-canvas)', direction: 'ltr' }}
>
{() => (
<>
{/* Base + deep-zoom tiles in one layer. Tiles engage by
demand arithmetic when the view wants more pixels than
the base budget supplies — the thumbnail rail's demand
never does, so it mounts the same layer for free. */}
{/* a spinner while the page's picture is slow to come, a sentence
when it can't */}
{/* Clickable links (nav plane): anchors under the default
pointer/pan tools; stands down whenever an authoring
tool is active (the annotation plane owns links then).
Below the AnnotationLayer so an editing free-text box
wins pointer hits; the annotation layer's own surface
is pointer-events: none, so clicks fall through to the
anchors everywhere else. */}
{/* The fields' pictures, and fill controls over them
while the active tool fills forms; the render layer
leaves the fields to it, as it leaves the annotations
to the annotation layer. */}
{/* The snapshot tool's box, over everything on the page. */}
>
)}
{/* signatures: plugin intents → surfaces; the dialogs they open */}
{/* the outline and the attachments, read so their panels' buttons know */}
{/* once a document shows: the features' panels and dialogs, fetched while idle */}
{/* The footer socket: below everything, empty until a child fills it. */}
>
);
}