import { useState } 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, annotationKey, annotationPlugin, useAnnotation, useAnnotationState, useComments, useCommentThreads, type CommentThreadView, } from '@embedpdf/react/annotation'; import { localEngine } from '@embedpdf/engine'; import './comments.css'; const engine = localEngine(); const plugins = [ stagePlugin(), renderPlugin(), interactionPlugin(), selectionPlugin(), annotationPlugin(), ]; // [!doc-source proposal] const proposal = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/proposal.pdf'); return { kind: 'bytes', id: 'proposal', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // [!/doc-source] // One thread: the first comment, its replies, a reply box, and a button that shows it. function Thread({ thread }: { thread: CommentThreadView }) { const comments = useComments(); const annotation = useAnnotation(); const stage = useStage(); const [text, setText] = useState(''); function show() { stage.reveal(thread.page, { rect: thread.root.rect }); annotation.selection.select([thread.root.ref]); // and select it } return (
Page {thread.pageLabel}

{thread.root.author} {thread.root.contents}

{thread.replies.map((reply) => (

{reply.author} {reply.contents}

))} {comments.canReply(thread.root.ref) && (
{ event.preventDefault(); if (!text.trim()) return; void comments.reply(thread.root.ref, text.trim()); setText(''); }} > setText(event.target.value)} />
)}
); } // Every thread in the document, in reading order. function Comments() { const threads = useCommentThreads(); const loading = useAnnotationState((state) => state.status === 'loading'); return (
{loading &&

Loading comments…

} {!loading && threads.length === 0 &&

No comments

} {threads.map((thread) => ( ))}
); } export default function App() { return ( Loading…

}>
{() => ( <> )}
); }