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 { SelectionLayer, selectionPlugin } from '@embedpdf/react/selection'; import { interactionPlugin } from '@embedpdf/react/interaction'; import { AnnotationLayer, annotationPlugin, useAnnotation, useAnnotationProperties, useAnnotationState, } from '@embedpdf/react/annotation'; import { cloudEngine } from '@cloudpdf/engine'; import './rich-text.css'; const engine = cloudEngine(); const plugins = [ stagePlugin(), renderPlugin(), interactionPlugin(), selectionPlugin(), annotationPlugin(), ]; const notice: OpenInput = { kind: 'grant', grant: 'shr_LfeeUer1ogBI9mz8eYkWXa0d' }; // On load: the document's rich text box, selected. function SelectRichText() { 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 richText = annotation.list().find((each) => each.nm === 'rich-text'); if (!richText) return; annotation.selection.select([richText.ref]); stage.reveal(richText.page, { rect: richText.rect }); }, [annotation, stage, ready]); return null; } type Format = 'bold' | 'italic' | 'underline'; // The same calls style the selected words while typing, and the whole box otherwise. function RichTextToolbar() { const annotation = useAnnotation(); const { properties, values, mixed } = useAnnotationProperties(); const hasText = properties.some((property) => property.control === 'textFormat'); // On when every selected word has it; `mixed` lists what the words disagree on. const isOn = (format: Format) => values[format] === true && !mixed.includes(format); return (
Ctrl or Cmd with B, I or U works while you type