import { useEffect, useRef } 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 { interactionPlugin, useInteraction, useInteractionState, } from '@embedpdf/react/interaction'; import { AnnotationLayer, annotationPlugin } from '@embedpdf/react/annotation'; import { measurementPlugin, useMeasurement, usePageScale } from '@embedpdf/react/measurement'; import { cloudEngine } from '@cloudpdf/engine'; import './basic.css'; const engine = cloudEngine(); const plugins = [ stagePlugin(), renderPlugin(), interactionPlugin(), annotationPlugin(), measurementPlugin(), ]; const sketch: OpenInput = { kind: 'grant', grant: 'shr_IyOgSdO4AmJqV4BoMv5V0nYU' }; const TOOLS = [ { id: 'pointer', label: 'Select' }, { id: 'distance', label: 'Distance' }, { id: 'perimeter', label: 'Perimeter' }, { id: 'area', label: 'Area' }, ]; function MeasureToolbar() { const interaction = useInteraction(); const { activeToolId } = useInteractionState(); const measurement = useMeasurement(); const scale = usePageScale(0); // the floor plan's scale const started = useRef(false); // On load: the floor plan at 1:100, the building's width measured, and the distance tool on. useEffect(() => { if (!scale.ready || started.current) return; started.current = true; void measurement .setPreset(0, 'metric-100') .then(() => measurement.createMeasurement({ kind: 'distance', page: 0, points: [ { x: 62, y: 150 }, { x: 550, y: 150 }, ], }), ) .then(() => interaction.activateTool('distance')); }, [measurement, interaction, scale.ready]); return (