Files
sign/apps/web/components/editor/pdf-viewer.jsx

169 lines
6.3 KiB
React
Raw Normal View History

2023-02-08 14:20:55 +01:00
import { Fragment, useState } from "react";
import EditableField from "./editable-field";
2023-02-24 18:05:32 +01:00
import SignableField from "./signable-field";
2023-03-19 11:54:26 +01:00
import { FieldType } from "@prisma/client";
2023-04-04 22:02:32 +00:00
import { Document, Page } from "react-pdf/dist/esm/entry.webpack5";
import short from "short-uuid";
2023-01-19 19:24:01 +01:00
2023-01-23 16:32:06 +01:00
export default function PDFViewer(props) {
2023-01-19 19:24:01 +01:00
const [numPages, setNumPages] = useState(null);
2023-01-24 19:10:24 +01:00
const [loading, setLoading] = useState(true);
2023-02-27 18:15:10 +01:00
const [pageHeight, setPageHeight] = useState(0);
2023-02-09 15:48:26 +01:00
function onPositionChangedHandler(position, id) {
2023-02-13 18:04:44 +01:00
props.onPositionChanged(position, id);
2023-02-09 15:48:26 +01:00
}
2023-01-19 19:24:01 +01:00
2023-02-14 17:04:53 +01:00
function onDeleteHandler(id) {
props.onDelete(id);
}
2023-01-19 19:24:01 +01:00
function onDocumentLoadSuccess({ numPages: nextNumPages }) {
setNumPages(nextNumPages);
}
2023-01-19 20:11:02 +01:00
const options = {
cMapUrl: "cmaps/",
cMapPacked: true,
standardFontDataUrl: "standard_fonts/",
};
2023-01-19 19:24:01 +01:00
return (
<>
2023-04-04 22:10:30 +00:00
<div
hidden={loading}
onMouseUp={props.onMouseUp}
style={{ height: numPages * pageHeight + 1000 }}>
2023-04-04 22:02:32 +00:00
<div className="mt-6 max-w-xs"></div>
2023-01-19 20:24:49 +01:00
<Document
2023-01-23 16:32:06 +01:00
file={props.pdfUrl}
2023-01-19 20:24:49 +01:00
onLoadSuccess={onDocumentLoadSuccess}
options={options}
2023-02-22 17:23:51 +01:00
renderMode="canvas"
2023-04-04 22:02:32 +00:00
className="absolute left-0 right-0 mx-auto w-auto">
2023-01-19 20:24:49 +01:00
{Array.from({ length: numPages }, (_, index) => (
2023-02-09 13:14:40 +01:00
<Fragment key={short.generate().toString()}>
2023-02-08 14:20:55 +01:00
<div
2023-03-09 18:37:50 +01:00
onMouseDown={(e) => {
2023-03-09 14:09:16 +01:00
if (e.button === 0) props.onMouseDown(e, index);
2023-02-22 17:23:51 +01:00
}}
2023-02-22 20:09:30 +01:00
onMouseUp={(e) => {
2023-03-09 14:09:16 +01:00
if (e.button === 0) props.onMouseUp(e, index);
2023-02-22 20:09:30 +01:00
}}
2023-02-09 13:14:40 +01:00
key={short.generate().toString()}
2023-02-08 14:20:55 +01:00
style={{
position: "relative",
2023-03-07 16:34:51 +01:00
...props.style,
2023-02-08 14:20:55 +01:00
}}
2023-04-04 22:02:32 +00:00
className="mx-auto w-fit">
2023-02-13 18:13:04 +01:00
<Page
className="mt-5"
key={`page_${index + 1}`}
pageNumber={index + 1}
renderAnnotationLayer={false}
renderTextLayer={false}
2023-02-27 18:15:10 +01:00
onLoadSuccess={(e) => {
if (e.height) setPageHeight(e.height);
2023-02-22 17:54:54 +01:00
setLoading(false);
}}
2023-04-04 22:02:32 +00:00
onRenderError={() => setLoading(false)}></Page>
2023-02-13 18:13:04 +01:00
{props?.fields
2023-03-19 11:54:26 +01:00
.filter((field) => field.page === index)
.map((field) =>
props.readonly ? (
2023-02-24 18:05:32 +01:00
<SignableField
onClick={props.onClick}
2023-03-19 11:54:26 +01:00
key={field.id}
field={field}
className="absolute"
2023-04-04 22:02:32 +00:00
onDelete={onDeleteHandler}></SignableField>
) : (
<EditableField
2023-04-04 22:10:30 +00:00
hidden={
field.Signature ||
field.inserted ||
field.type === FieldType.FREE_SIGNATURE
}
2023-03-19 11:54:26 +01:00
key={field.id}
field={field}
className="absolute"
onPositionChanged={onPositionChangedHandler}
2023-04-04 22:02:32 +00:00
onDelete={onDeleteHandler}></EditableField>
)
)}
2023-02-08 14:20:55 +01:00
</div>
</Fragment>
2023-01-19 20:24:49 +01:00
))}
</Document>
</div>
2023-02-13 18:14:48 +01:00
<div className="mx-auto mt-10 w-[600px]" hidden={!loading}>
2023-01-24 19:10:24 +01:00
<div className="ph-item">
<div className="ph-col-12">
<div className="ph-picture"></div>
<div className="ph-row">
<div className="ph-col-6 big"></div>
<div className="ph-col-4 empty big"></div>
<div className="ph-col-2 big"></div>
<div className="ph-col-4"></div>
<div className="ph-col-8 empty"></div>
<div className="ph-col-6"></div>
<div className="ph-col-6 empty"></div>
<div className="ph-col-12"></div>
</div>
2023-01-24 20:09:40 +01:00
<div className="ph-row">
<div className="ph-col-6 big"></div>
<div className="ph-col-4 empty big"></div>
<div className="ph-col-2 big"></div>
<div className="ph-col-4"></div>
<div className="ph-col-8 empty"></div>
<div className="ph-col-6"></div>
<div className="ph-col-6 empty"></div>
<div className="ph-col-12"></div>
</div>
<div className="ph-row">
<div className="ph-col-6 big"></div>
<div className="ph-col-4 empty big"></div>
<div className="ph-col-2 big"></div>
<div className="ph-col-4"></div>
<div className="ph-col-8 empty"></div>
<div className="ph-col-6"></div>
<div className="ph-col-6 empty"></div>
<div className="ph-col-12"></div>
</div>
<div className="ph-row">
<div className="ph-col-6 big"></div>
<div className="ph-col-4 empty big"></div>
<div className="ph-col-2 big"></div>
<div className="ph-col-4"></div>
<div className="ph-col-8 empty"></div>
<div className="ph-col-6"></div>
<div className="ph-col-6 empty"></div>
<div className="ph-col-12"></div>
</div>
<div className="ph-row">
<div className="ph-col-6 big"></div>
<div className="ph-col-4 empty big"></div>
<div className="ph-col-2 big"></div>
<div className="ph-col-4"></div>
<div className="ph-col-8 empty"></div>
<div className="ph-col-6"></div>
<div className="ph-col-6 empty"></div>
<div className="ph-col-12"></div>
</div>
<div className="ph-row">
<div className="ph-col-6 big"></div>
<div className="ph-col-4 empty big"></div>
<div className="ph-col-2 big"></div>
<div className="ph-col-4"></div>
<div className="ph-col-8 empty"></div>
<div className="ph-col-6"></div>
<div className="ph-col-6 empty"></div>
<div className="ph-col-12"></div>
</div>
2023-01-24 19:10:24 +01:00
</div>
</div>
</div>
2023-01-19 19:24:01 +01:00
</>
);
}