import { useEffect, useRef } from 'react';
import { Viewer, DocumentGate } from '@embedpdf/react/runtime';
import type { OpenInput } from '@embedpdf/react/runtime';
import { Stage, stagePlugin, useStage } from '@embedpdf/react/stage';
import { RenderLayer, renderPlugin } from '@embedpdf/react/render';
import { interactionPlugin } from '@embedpdf/react/interaction';
import {
AnnotationLayer,
annotationPlugin,
useAnnotation,
useAnnotationProperties,
useAnnotationState,
type AnnotationProperty,
type LineEnding,
} from '@embedpdf/react/annotation';
import { localEngine } from '@embedpdf/engine';
import './style-panel.css';
const engine = localEngine();
const plugins = [stagePlugin(), renderPlugin(), interactionPlugin(), annotationPlugin()];
const layout = async (): Promise => {
const response = await fetch('https://www.embedpdf.com/pdfs/layout.pdf');
return { kind: 'bytes', id: 'layout', bytes: new Uint8Array(await response.arrayBuffer()) };
};
// On load: the document's square, selected, so the panel shows its style.
function SelectSquare() {
const annotation = useAnnotation();
const stage = useStage();
const ready = useAnnotationState((state) => state.status === 'ready');
const done = useRef(false);
useEffect(() => {
if (!ready || done.current) return;
done.current = true;
const square = annotation.list().find((each) => each.nm === 'square');
if (!square) return;
annotation.selection.select([square.ref]);
stage.reveal(square.page, { rect: square.rect });
}, [annotation, stage, ready]);
return null;
}
// One control per property, by the control it asks for.
function Control({ property }: { property: AnnotationProperty }) {
const annotation = useAnnotation();
const { values, mixed } = useAnnotationProperties();
const value = values[property.key];
const isMixed = mixed.includes(property.key);
const update = (next: unknown) => annotation.selection.update({ [property.key]: next });
switch (property.control) {
case 'color':
return (
update(event.target.value)}
/>
{property.allowsNone && (
)}
);
case 'number':
return (
update(Number(event.target.value))}
/>
);
case 'choice':
// Line endings are a pair: this sets the end, and each line keeps its own start.
if (property.key === 'lineEndings') {
const end = (value as { end?: string } | undefined)?.end ?? 'none';
return (
);
}
// The border is three fields: its style, its dashes, and how cloudy it is.
if (property.key === 'borderStyle') {
const border = values.cloudyIntensity ? 'cloudy' : String(value);
return (
);
}
return (
);
case 'flag':
return (
update(event.target.checked)}
/>
);
case 'textFormat':
return (
);
case 'text':
return (
update(event.target.value)}
/>
);
case 'link':
return (
);
}
}
// What the selection can change, with its current values.
function StylePanel() {
const { properties } = useAnnotationProperties();
if (properties.length === 0) return Select an annotation
;
return (
{properties.map((property) => (
- {property.label}
-
))}
);
}
export default function App() {
return (
Loading…}>
);
}