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, useInteraction } from '@embedpdf/react/interaction';
import { cloudEngine } from '@cloudpdf/engine';
import './ruler.css';
const engine = cloudEngine();
const plugins = [stagePlugin(), renderPlugin(), interactionPlugin()];
const welcome: OpenInput = { kind: 'grant', grant: 'shr_gvpJcG1ULUMon19C0D1wODSY' };
interface Point {
readonly x: number;
readonly y: number;
}
interface Measurement {
readonly page: PageRef;
readonly from: Point;
readonly to: Point;
}
// Page coordinates are points, 72 to the inch.
const lengthOf = ({ from, to }: Measurement) => {
const inches = Math.hypot(to.x - from.x, to.y - from.y) / 72;
return `${inches.toFixed(2)} in · ${(inches * 2.54).toFixed(1)} cm`;
};
// The measured line on its page, placed in the page's pixels.
function RulerLayer({
page,
measurement,
}: {
page: PageContextValue;
measurement: Measurement | null;
}) {
if (!measurement || !pageRefsEqual(measurement.page, page.ref)) return null;
const from = page.transform.toPixels(measurement.from);
const to = page.transform.toPixels(measurement.to);
return (
<>
{lengthOf(measurement)}
>
);
}
function Ruler() {
const interaction = useInteraction();
const [measurement, setMeasurement] = useState