import { useEffect, useState } from 'react'; import { Viewer, DocumentGate } from '@embedpdf/react/runtime'; import type { OpenInput } from '@embedpdf/react/runtime'; import { Stage, stagePlugin } from '@embedpdf/react/stage'; import { RenderLayer, renderPlugin } from '@embedpdf/react/render'; import { shellPlugin, useShell, useShellState, useSurface } from '@embedpdf/react/shell'; import type { ShellSnapshot } from '@embedpdf/react/shell'; import { cloudEngine } from '@cloudpdf/engine'; import './remember.css'; const engine = cloudEngine(); const plugins = [stagePlugin(), renderPlugin(), shellPlugin()]; const welcome: OpenInput = { kind: 'grant', grant: 'shr_gvpJcG1ULUMon19C0D1wODSY' }; // The browser may refuse storage, in a private window: then nothing is remembered. const loadLayout = (): ShellSnapshot | null => { try { return JSON.parse(localStorage.getItem('panels') ?? 'null') as ShellSnapshot | null; } catch { return null; } }; const saveLayout = (snapshot: ShellSnapshot) => { try { localStorage.setItem('panels', JSON.stringify(snapshot)); } catch { // Not remembered this time. } }; const TIPS = [ 'Ctrl or ⌘ and scroll to zoom', 'Pinch to zoom on a touch screen', 'Drag between pages to scroll', ]; function PanelButton({ id, label, side }: { id: string; label: string; side: 'left' | 'right' }) { const panel = useSurface(id); return ( ); } function TipsPanel() { const panel = useSurface('tips'); if (!panel.isOpen) return null; return ( ); } function NotesPanel() { const panel = useSurface('notes'); if (!panel.isOpen) return null; return ( ); } function Workspace() { const shell = useShell(); const { openSurfaces } = useShellState(); const [saved, setSaved] = useState(loadLayout); // The layout saved last time, or the tips the first time. useEffect(() => { const layout = loadLayout(); if (layout) shell.applySnapshot(layout); else shell.open('tips', { exclusive: 'left' }); }, [shell]); const save = () => { const layout = shell.getSnapshot(); saveLayout(layout); setSaved(layout); }; const open = openSurfaces.map((surface) => surface.id).join(', '); return ( <>
Open: {open || 'no panels'}