import { useEffect, useState } from 'react';
import { Viewer, DocumentGate, usePageList } 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 } from '@embedpdf/react/interaction';
import {
SelectionClipboard,
SelectionLayer,
copySelection,
selectionPlugin,
useSelection,
useSelectionState,
} from '@embedpdf/react/selection';
import { localEngine } from '@embedpdf/engine';
import './copy.css';
const engine = localEngine();
const plugins = [stagePlugin(), renderPlugin(), interactionPlugin(), selectionPlugin()];
// [!doc-source welcome]
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()) };
};
// [!/doc-source]
function CopyButton() {
const selection = useSelection();
const hasSelection = useSelectionState((state) => state.hasSelection);
const [status, setStatus] = useState('');
const copy = () => {
copySelection(selection).then(
(text) => setStatus(`Copied ${text.length} characters`),
// The browser can refuse the clipboard, for example in a frame that doesn't allow it.
() => setStatus("The browser didn't allow copying"),
);
};
return (
);
}
// The selected text, read with readText() each time the selection settles.
function SelectedText() {
const selection = useSelection();
const { range, isSelecting } = useSelectionState();
const [text, setText] = useState('');
useEffect(() => {
if (isSelecting || !selection.canCopy()) return;
// A newer selection cancels a read that hasn't finished.
const controller = new AbortController();
selection.readText({ signal: controller.signal }).then(setText, () => {});
return () => controller.abort();
}, [selection, range, isSelecting]);
return {text || 'Select some text to read it here.'}
;
}
// Something selected on load: "Documents", the first word on the cover.
function SelectTitle() {
const selection = useSelection();
const cover = usePageList()[0]?.ref;
useEffect(() => {
// [!doc-range welcome firstWord]
if (cover) selection.select({ page: cover, start: 28, count: 9 });
// [!/doc-range]
}, [selection, cover]);
return null;
}
export default function App() {
return (
Loading…
}>
{() => (
<>
>
)}
);
}