import { useEffect, useRef, 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, useInteraction, useToolCursor } from '@embedpdf/react/interaction';
import { localEngine } from '@embedpdf/engine';
import './cursor.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()) };
};
const COLORS = ['#e5484d', '#2f80ed', '#30a46c', '#1a2748'];
// A pen in the current color, with its tip at the bottom left.
const penIcon = (color: string) =>
``;
interface Dot {
readonly id: number;
readonly page: PageRef;
readonly point: { readonly x: number; readonly y: number };
readonly color: string;
}
// While this is mounted, the dot tool's crosshair is a pen in `color`.
function PenCursor({ color }: { color: string }) {
useToolCursor({
toolId: 'dot',
cursors: { crosshair: { svg: penIcon(color), hotspot: { x: 3, y: 21 } } },
});
return null;
}
function DotLayer({ page, dots }: { page: PageContextValue; dots: readonly Dot[] }) {
return (
<>
{dots
.filter((dot) => pageRefsEqual(dot.page, page.ref))
.map((dot) => {
const { x, y } = page.transform.toPixels(dot.point);
return (
);
})}
>
);
}
function Dots() {
const interaction = useInteraction();
const [color, setColor] = useState(COLORS[0]);
const [penCursor, setPenCursor] = useState(true);
const [dots, setDots] = useState([]);
// The tool reads the color at the moment of the click.
const colorRef = useRef(color);
colorRef.current = color;
useEffect(() => {
const remove = interaction.registerTool({
id: 'dot',
cursor: 'crosshair',
onPointerDown: ({ page, point }) => {
setDots((current) => [
...current,
{ id: current.length + 1, page, point, color: colorRef.current },
]);
return true;
},
});
interaction.activateTool('dot');
return remove;
}, [interaction]);
return (
<>
{penCursor && }
{(page) => (
<>
>
)}
>
);
}
export default function App() {
return (
Loading…
}>
);
}