Files
sign/apps/web/components/editor/editable-field.tsx

92 lines
2.5 KiB
TypeScript
Raw Normal View History

2023-02-21 11:19:31 +01:00
import React, { useState } from "react";
2023-02-08 15:43:14 +01:00
import Draggable from "react-draggable";
import Logo from "../logo";
2023-02-14 14:26:54 +01:00
import { IconButton } from "@documenso/ui";
2023-02-15 13:33:03 +01:00
import { XCircleIcon } from "@heroicons/react/20/solid";
2023-02-09 15:48:26 +01:00
const stc = require("string-to-color");
2023-02-08 15:43:14 +01:00
type FieldPropsType = {
2023-02-09 15:48:26 +01:00
field: {
color: string;
type: string;
position: any;
2023-02-14 12:28:33 +01:00
positionX: number;
positionY: number;
2023-02-09 15:48:26 +01:00
id: string;
Recipient: { name: ""; email: "" };
2023-02-09 15:48:26 +01:00
};
2023-02-13 18:04:44 +01:00
onPositionChanged: any;
2023-02-14 17:04:53 +01:00
onDelete: any;
2023-02-21 11:19:31 +01:00
hidden: boolean;
2023-02-08 15:43:14 +01:00
};
export default function EditableField(props: FieldPropsType) {
2023-02-09 15:48:26 +01:00
const [field, setField]: any = useState(props.field);
2023-02-14 12:28:33 +01:00
const [position, setPosition]: any = useState({
x: props.field.positionX,
y: props.field.positionY,
});
2023-02-09 15:48:26 +01:00
const nodeRef = React.createRef<HTMLDivElement>();
const onControlledDrag = (e: any, position: any) => {
const { x, y } = position;
setPosition({ x, y });
};
const onDragStop = (e: any, position: any) => {
if (!position) return;
const { x, y } = position;
2023-02-13 18:04:44 +01:00
props.onPositionChanged({ x, y }, props.field.id);
2023-02-09 15:48:26 +01:00
};
2023-02-08 15:43:14 +01:00
return (
2023-02-09 15:48:26 +01:00
<Draggable
nodeRef={nodeRef}
bounds="parent"
position={position}
onDrag={onControlledDrag}
onStop={onDragStop}
2023-02-14 12:28:33 +01:00
defaultPosition={{ x: 0, y: 0 }}
2023-02-15 13:15:41 +01:00
cancel="strong"
2023-02-22 17:23:51 +01:00
onMouseDown={(e: any) => {
e.preventDefault();
e.stopPropagation();
}}
2023-02-09 15:48:26 +01:00
>
<div
2023-02-21 11:19:31 +01:00
hidden={props.hidden}
2023-02-09 15:48:26 +01:00
ref={nodeRef}
className="cursor-move opacity-80 p-2 m-auto w-48 h-24 flex-row-reverse text-lg font-bold text-center absolute top-0 left-0 select-none"
style={{
background: stc(props.field.Recipient.email),
}}
2023-02-08 15:43:14 +01:00
>
<div className="mb-2 pointer-events-none">
<Logo className="w-16 mx-auto"></Logo>
</div>
{/* width: 192 height 96 */}
<div className="m-auto overflow-hidden flex-row-reverse text-lg font-bold text-center">
2023-02-15 13:15:41 +01:00
{field.type}
{field.type === "SIGNATURE" ? (
<div className="text-xs text-center">
{`${props.field.Recipient?.name} <${props.field.Recipient?.email}>`}
</div>
) : (
""
)}
2023-02-15 13:15:41 +01:00
</div>
<strong>
2023-02-14 14:26:54 +01:00
<IconButton
2023-02-15 13:33:03 +01:00
className="absolute top-0 right-0 -m-5"
color="secondary"
icon={XCircleIcon}
2023-02-14 17:04:53 +01:00
onClick={(event: any) => {
2023-02-15 13:33:03 +01:00
props.onDelete(props.field.id);
2023-02-14 14:26:54 +01:00
}}
></IconButton>
2023-02-15 13:15:41 +01:00
</strong>
2023-02-09 15:48:26 +01:00
</div>
2023-02-08 15:43:14 +01:00
</Draggable>
);
}