import { useEffect, useState } from 'react';
import { Viewer, DocumentGate, pageRefsEqual } from '@embedpdf/react/runtime';
import type { OpenInput, PageContextValue, PageRef } from '@embedpdf/react/runtime';
import { Stage, stagePlugin } from '@embedpdf/react/stage';
import { RenderLayer, renderPlugin } from '@embedpdf/react/render';
import {
interactionPlugin,
svgCursor,
useInteraction,
useInteractionState,
} from '@embedpdf/react/interaction';
import { localEngine } from '@embedpdf/engine';
import './pin.css';
const engine = localEngine();
const plugins = [stagePlugin(), renderPlugin(), interactionPlugin()];
const welcome = async (): Promise => {
const response = await fetch('https://www.embedpdf.com/pdfs/welcome.pdf');
return { kind: 'bytes', id: 'welcome', bytes: new Uint8Array(await response.arrayBuffer()) };
};
interface Pin {
readonly id: number;
readonly page: PageRef;
readonly point: { readonly x: number; readonly y: number };
}
// The cursor is the pin itself; its tip is the point that clicks.
const PIN_CURSOR = svgCursor({
svg: ``,
hotspot: { x: 12, y: 23 },
fallback: 'copy',
});
// The pins on one page, placed in its pixels at the last moment.
function PinLayer({ page, pins }: { page: PageContextValue; pins: readonly Pin[] }) {
return (
<>
{pins
.filter((pin) => pageRefsEqual(pin.page, page.ref))
.map((pin) => {
const { x, y } = page.transform.toPixels(pin.point);
return (
{pin.id}
);
})}
>
);
}
function PinBoard() {
const interaction = useInteraction();
const { activeToolId } = useInteractionState();
const [pins, setPins] = useState([]);
// A tool of your own: a click on a page drops a pin there, in page coordinates.
useEffect(() => {
const remove = interaction.registerTool({
id: 'pin',
cursor: PIN_CURSOR,
onPointerDown: ({ page, point }) => {
setPins((current) => [...current, { id: current.length + 1, page, point }]);
return true; // this tool handled the click
},
});
interaction.activateTool('pin');
return remove;
}, [interaction]);
return (
<>
{(page) => (
<>
>
)}
>
);
}
export default function App() {
return (
Loading…
}>
);
}