mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 16:23:06 +10:00
🚧 pdfsigner, readonly field, signpage, signgn dialog
This commit is contained in:
@ -22,7 +22,7 @@ type FieldPropsType = {
|
||||
onDelete: any;
|
||||
};
|
||||
|
||||
export default function Field(props: FieldPropsType) {
|
||||
export default function EditableField(props: FieldPropsType) {
|
||||
const [field, setField]: any = useState(props.field);
|
||||
const [position, setPosition]: any = useState({
|
||||
x: props.field.positionX,
|
||||
@ -40,6 +40,7 @@ export default function Field(props: FieldPropsType) {
|
||||
|
||||
props.onPositionChanged({ x, y }, props.field.id);
|
||||
};
|
||||
|
||||
return (
|
||||
<Draggable
|
||||
nodeRef={nodeRef}
|
||||
@ -51,6 +51,7 @@ export default function PDFEditor(props: any) {
|
||||
return (
|
||||
<>
|
||||
<PDFViewer
|
||||
readonly={false}
|
||||
document={props.document}
|
||||
fields={fields}
|
||||
onPositionChanged={onPositionChangedHandler}
|
||||
|
||||
48
apps/web/components/editor/pdf-signer.tsx
Normal file
48
apps/web/components/editor/pdf-signer.tsx
Normal file
@ -0,0 +1,48 @@
|
||||
import Logo from "../logo";
|
||||
import { NEXT_PUBLIC_WEBAPP_URL } from "@documenso/lib/constants";
|
||||
import { useRouter } from "next/router";
|
||||
import dynamic from "next/dynamic";
|
||||
import SignatureDialog from "./signature-dialog";
|
||||
import { useState } from "react";
|
||||
|
||||
const PDFViewer = dynamic(() => import("./pdf-viewer"), {
|
||||
ssr: false,
|
||||
});
|
||||
|
||||
export default function PDFSigner(props: any) {
|
||||
const router = useRouter();
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
function onClick(item: any) {
|
||||
if (item.type === "SIGNATURE") {
|
||||
setOpen(true);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<SignatureDialog open={open} setOpen={setOpen}></SignatureDialog>
|
||||
<div className="bg-neon p-4">
|
||||
<div className="flex">
|
||||
<div className="flex-shrink-0">
|
||||
<Logo className="inline w-10"></Logo>
|
||||
</div>
|
||||
<div className="ml-3 flex-1 md:flex md:justify-between">
|
||||
<p className="text-lg text-slate-700 align-baseline">
|
||||
Timur Ercan (timur.ercan31@gmail.com) would like you to sign this
|
||||
document.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/* todo use public url with token auth to get document */}
|
||||
<PDFViewer
|
||||
readonly={true}
|
||||
document={props.document}
|
||||
fields={props.fields}
|
||||
pdfUrl={`${NEXT_PUBLIC_WEBAPP_URL}/api/documents/${router.query.id}?token=${router.query.token}`}
|
||||
onClick={onClick}
|
||||
></PDFViewer>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
import { Fragment, useState } from "react";
|
||||
import { Document, Page } from "react-pdf/dist/esm/entry.webpack5";
|
||||
import Field from "./field";
|
||||
import EditableField from "./editable-field";
|
||||
import ReadOnlyField from "./readonly-field";
|
||||
import short from "short-uuid";
|
||||
|
||||
export default function PDFViewer(props) {
|
||||
@ -60,15 +61,24 @@ export default function PDFViewer(props) {
|
||||
></Page>
|
||||
{props?.fields
|
||||
.filter((item) => item.page === index)
|
||||
.map((item) => (
|
||||
<Field
|
||||
key={item.id}
|
||||
field={item}
|
||||
className="absolute"
|
||||
onPositionChanged={onPositionChangedHandler}
|
||||
onDelete={onDeleteHandler}
|
||||
></Field>
|
||||
))}
|
||||
.map((item) =>
|
||||
props.readonly ? (
|
||||
<ReadOnlyField
|
||||
onClick={props.onClick}
|
||||
key={item.id}
|
||||
field={item}
|
||||
className="absolute"
|
||||
></ReadOnlyField>
|
||||
) : (
|
||||
<EditableField
|
||||
key={item.id}
|
||||
field={item}
|
||||
className="absolute"
|
||||
onPositionChanged={onPositionChangedHandler}
|
||||
onDelete={onDeleteHandler}
|
||||
></EditableField>
|
||||
)
|
||||
)}
|
||||
</div>
|
||||
</Fragment>
|
||||
))}
|
||||
|
||||
56
apps/web/components/editor/readonly-field.tsx
Normal file
56
apps/web/components/editor/readonly-field.tsx
Normal file
@ -0,0 +1,56 @@
|
||||
import { ResizableBox, ResizeCallbackData } from "react-resizable";
|
||||
import React, { SyntheticEvent, useEffect, useState } from "react";
|
||||
import Draggable from "react-draggable";
|
||||
import { CircleStackIcon, TrashIcon } from "@heroicons/react/24/solid";
|
||||
import Logo from "../logo";
|
||||
import { IconButton } from "@documenso/ui";
|
||||
import toast from "react-hot-toast";
|
||||
import { XCircleIcon } from "@heroicons/react/20/solid";
|
||||
const stc = require("string-to-color");
|
||||
|
||||
type FieldPropsType = {
|
||||
field: {
|
||||
color: string;
|
||||
type: string;
|
||||
position: any;
|
||||
positionX: number;
|
||||
positionY: number;
|
||||
id: string;
|
||||
Recipient: { name: ""; email: "" };
|
||||
};
|
||||
onClick: any;
|
||||
};
|
||||
|
||||
export default function ReadOnlyField(props: FieldPropsType) {
|
||||
const [field, setField]: any = useState(props.field);
|
||||
const [position, setPosition]: any = useState({
|
||||
x: props.field.positionX,
|
||||
y: props.field.positionY,
|
||||
});
|
||||
const nodeRef = React.createRef<HTMLDivElement>();
|
||||
|
||||
return (
|
||||
<Draggable
|
||||
nodeRef={nodeRef}
|
||||
bounds="parent"
|
||||
position={position}
|
||||
defaultPosition={{ x: 0, y: 0 }}
|
||||
cancel="div"
|
||||
>
|
||||
<div
|
||||
onClick={() => {
|
||||
props.onClick(props.field);
|
||||
}}
|
||||
ref={nodeRef}
|
||||
className="cursor-pointer opacity-80 p-2 m-auto w-auto flex-row-reverse text-lg font-bold text-center absolute top-0 left-0 select-none hover:brightness-50"
|
||||
style={{
|
||||
background: stc(props.field.Recipient.email),
|
||||
}}
|
||||
>
|
||||
<div className="m-auto w-auto flex-row-reverse font-medium text-center px-12 py-2">
|
||||
{field.type === "SIGNATURE" ? "SIGN HERE" : ""}
|
||||
</div>
|
||||
</div>
|
||||
</Draggable>
|
||||
);
|
||||
}
|
||||
132
apps/web/components/editor/signature-dialog.tsx
Normal file
132
apps/web/components/editor/signature-dialog.tsx
Normal file
@ -0,0 +1,132 @@
|
||||
import { classNames } from "@documenso/lib";
|
||||
import { Button, IconButton } from "@documenso/ui";
|
||||
import { Dialog, Transition } from "@headlessui/react";
|
||||
import {
|
||||
BuildingOfficeIcon,
|
||||
CreditCardIcon,
|
||||
LanguageIcon,
|
||||
PencilIcon,
|
||||
UserIcon,
|
||||
UsersIcon,
|
||||
XMarkIcon,
|
||||
} from "@heroicons/react/24/outline";
|
||||
import React from "react";
|
||||
import { Fragment, useState } from "react";
|
||||
|
||||
const tabs = [
|
||||
{ name: "Type", href: "#", icon: LanguageIcon, current: true },
|
||||
{ name: "Draw", href: "#", icon: PencilIcon, current: false },
|
||||
];
|
||||
|
||||
export default function SignatureDialog(props: any) {
|
||||
const [currentTab, setCurrentTab] = useState(tabs[0]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Transition.Root show={props.open} as={Fragment}>
|
||||
<Dialog as="div" className="relative z-10" onClose={props.setOpen}>
|
||||
<Transition.Child
|
||||
as={Fragment}
|
||||
enter="ease-out duration-300"
|
||||
enterFrom="opacity-0"
|
||||
enterTo="opacity-100"
|
||||
leave="ease-in duration-200"
|
||||
leaveFrom="opacity-100"
|
||||
leaveTo="opacity-0"
|
||||
>
|
||||
<div className="fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity" />
|
||||
</Transition.Child>
|
||||
|
||||
<div className="fixed inset-0 z-10 overflow-y-auto">
|
||||
<div className="flex min-h-full items-end justify-center p-4 text-center sm:items-center sm:p-0">
|
||||
<Transition.Child
|
||||
as={Fragment}
|
||||
enter="ease-out duration-300"
|
||||
enterFrom="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
|
||||
enterTo="opacity-100 translate-y-0 sm:scale-100"
|
||||
leave="ease-in duration-200"
|
||||
leaveFrom="opacity-100 translate-y-0 sm:scale-100"
|
||||
leaveTo="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
|
||||
>
|
||||
<Dialog.Panel className="relative transform overflow-hidden rounded-lg bg-white px-4 pt-5 pb-4 text-left shadow-xl transition-all sm:my-8 sm:w-full sm:max-w-sm sm:p-6">
|
||||
<div className="min-h-[50px]">
|
||||
<div className="border-b border-gray-200 mb-3">
|
||||
<nav className="-mb-px flex space-x-8" aria-label="Tabs">
|
||||
{tabs.map((tab) => (
|
||||
<Fragment>
|
||||
<a
|
||||
key={tab.name}
|
||||
onClick={() => {
|
||||
setCurrent(tab);
|
||||
}}
|
||||
className={classNames(
|
||||
tab.current
|
||||
? "border-neon text-neon"
|
||||
: "border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300",
|
||||
"group inline-flex items-center py-4 px-1 border-b-2 font-medium text-sm cursor-pointer"
|
||||
)}
|
||||
aria-current={tab.current ? "page" : undefined}
|
||||
>
|
||||
<tab.icon
|
||||
className={classNames(
|
||||
tab.current
|
||||
? "text-neon"
|
||||
: "text-gray-400 group-hover:text-gray-500",
|
||||
"-ml-0.5 mr-2 h-5 w-5"
|
||||
)}
|
||||
aria-hidden="true"
|
||||
/>
|
||||
<span>{tab.name}</span>
|
||||
</a>
|
||||
</Fragment>
|
||||
))}
|
||||
</nav>
|
||||
</div>
|
||||
{isCurrentTab("Type") ? (
|
||||
<div className="my-8">
|
||||
<input
|
||||
type="email"
|
||||
name="email"
|
||||
id="email"
|
||||
className="mt-3 pb-1 text-center block border-b w-full border-gray-300 focus:border-neon focus:ring-neon text-2xl"
|
||||
placeholder="Kindly type your name"
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
""
|
||||
)}
|
||||
{isCurrentTab("Draw") ? (
|
||||
<div className="my-8">draw</div>
|
||||
) : (
|
||||
""
|
||||
)}
|
||||
</div>
|
||||
<div className="float-right">
|
||||
<Button
|
||||
color="secondary"
|
||||
onClick={() => props.setOpen(false)}
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button className="ml-3">Sign</Button>
|
||||
</div>
|
||||
</Dialog.Panel>
|
||||
</Transition.Child>
|
||||
</div>
|
||||
</div>
|
||||
</Dialog>
|
||||
</Transition.Root>
|
||||
</>
|
||||
);
|
||||
|
||||
function isCurrentTab(tabName: string): boolean {
|
||||
return currentTab.name === tabName;
|
||||
}
|
||||
|
||||
function setCurrent(t: any) {
|
||||
tabs.forEach((tab) => {
|
||||
tab.current = tab.name === t.name;
|
||||
});
|
||||
setCurrentTab(t);
|
||||
}
|
||||
}
|
||||
@ -12,6 +12,9 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const user = await getUserFromToken(req, res);
|
||||
const { id: documentId } = req.query;
|
||||
|
||||
// TODO Check if this is a public link with token and validate the token
|
||||
const { token: recipientToken } = req.query;
|
||||
|
||||
if (!user) return;
|
||||
|
||||
if (!documentId) {
|
||||
|
||||
@ -4,28 +4,18 @@ import { useEffect } from "react";
|
||||
import { NextPageWithLayout } from "../../_app";
|
||||
import { ReadStatus } from "@prisma/client";
|
||||
import SignaturePad from "signature_pad";
|
||||
|
||||
import { InformationCircleIcon, PencilIcon } from "@heroicons/react/24/outline";
|
||||
import Logo from "../../../components/logo";
|
||||
import PDFSigner from "../../../components/editor/pdf-signer";
|
||||
import fields from "../../api/documents/[id]/fields";
|
||||
//http://localhost:3000/documents/40/sign?token=wu82JFMxLvdYVJ9sKy9jvd
|
||||
const SignPage: NextPageWithLayout = (props: any) => {
|
||||
useEffect(() => {
|
||||
const canvas: any = document.querySelector("canvas");
|
||||
const signaturePad = new SignaturePad(canvas);
|
||||
const resizeCanvas = () => {
|
||||
const ratio = Math.max(window.devicePixelRatio || 1, 1);
|
||||
canvas.width = canvas.offsetWidth * ratio;
|
||||
canvas.height = canvas.offsetHeight * ratio;
|
||||
canvas.getContext("2d").scale(ratio, ratio);
|
||||
// signaturePad.clear(); // otherwise isEmpty() might return incorrect value
|
||||
};
|
||||
window.addEventListener("resize", resizeCanvas);
|
||||
});
|
||||
return (
|
||||
<>
|
||||
<Head>
|
||||
<title>Sign | Documenso</title>
|
||||
</Head>
|
||||
Hello, please sign at the dotted line.
|
||||
<canvas className="mx-auto bg-neon"></canvas>
|
||||
<hr></hr>
|
||||
<PDFSigner document={props.document} fields={props.fields} />
|
||||
{/* todo read/ sign version of editor => flag or own component */}
|
||||
</>
|
||||
);
|
||||
@ -43,13 +33,23 @@ export async function getServerSideProps(context: any) {
|
||||
},
|
||||
});
|
||||
|
||||
const document = await prisma.recipient
|
||||
.findFirstOrThrow({
|
||||
where: {
|
||||
token: recipientToken,
|
||||
},
|
||||
})
|
||||
.Document();
|
||||
const recipient = await prisma.recipient.findFirstOrThrow({
|
||||
where: {
|
||||
token: recipientToken,
|
||||
},
|
||||
include: {
|
||||
Document: true,
|
||||
},
|
||||
});
|
||||
|
||||
const fields = await prisma.field.findMany({
|
||||
where: {
|
||||
documentId: recipient.Document.id,
|
||||
},
|
||||
include: {
|
||||
Recipient: true,
|
||||
},
|
||||
});
|
||||
|
||||
// todo get r
|
||||
|
||||
@ -57,7 +57,8 @@ export async function getServerSideProps(context: any) {
|
||||
|
||||
return {
|
||||
props: {
|
||||
document: document,
|
||||
document: recipient.Document,
|
||||
fields: fields,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user