2020-10-19 17:14:28 +03:00
|
|
|
import clsx from "clsx";
|
2020-12-27 18:26:30 +02:00
|
|
|
import oc from "open-color";
|
2021-10-14 22:56:51 +05:30
|
|
|
import { useEffect, useRef, useState } from "react";
|
2020-12-27 18:26:30 +02:00
|
|
|
import { MIME_TYPES } from "../constants";
|
2021-04-08 19:54:50 +02:00
|
|
|
import { useIsMobile } from "../components/App";
|
2020-12-27 18:26:30 +02:00
|
|
|
import { exportToSvg } from "../scene/export";
|
2021-10-21 22:05:48 +02:00
|
|
|
import { BinaryFiles, LibraryItem } from "../types";
|
2020-12-27 18:26:30 +02:00
|
|
|
import "./LibraryUnit.scss";
|
2021-11-17 23:53:43 +05:30
|
|
|
import { CheckboxItem } from "./CheckboxItem";
|
2020-07-10 02:20:23 -07:00
|
|
|
|
|
|
|
// fa-plus
|
|
|
|
const PLUS_ICON = (
|
|
|
|
<svg viewBox="0 0 1792 1792">
|
2020-08-13 04:35:31 -07:00
|
|
|
<path
|
|
|
|
fill="currentColor"
|
|
|
|
d="M1600 736v192q0 40-28 68t-68 28h-416v416q0 40-28 68t-68 28h-192q-40 0-68-28t-28-68v-416h-416q-40 0-68-28t-28-68v-192q0-40 28-68t68-28h416v-416q0-40 28-68t68-28h192q40 0 68 28t28 68v416h416q40 0 68 28t28 68z"
|
|
|
|
/>
|
2020-07-10 02:20:23 -07:00
|
|
|
</svg>
|
|
|
|
);
|
|
|
|
|
|
|
|
export const LibraryUnit = ({
|
2021-11-17 23:53:43 +05:30
|
|
|
id,
|
2020-07-10 02:20:23 -07:00
|
|
|
elements,
|
2021-10-21 22:05:48 +02:00
|
|
|
files,
|
2021-11-17 23:53:43 +05:30
|
|
|
isPending,
|
2020-07-10 02:20:23 -07:00
|
|
|
onClick,
|
2021-11-17 23:53:43 +05:30
|
|
|
selected,
|
|
|
|
onToggle,
|
2020-07-10 02:20:23 -07:00
|
|
|
}: {
|
2021-11-17 23:53:43 +05:30
|
|
|
id: LibraryItem["id"] | /** for pending item */ null;
|
|
|
|
elements?: LibraryItem["elements"];
|
2021-10-21 22:05:48 +02:00
|
|
|
files: BinaryFiles;
|
2021-11-17 23:53:43 +05:30
|
|
|
isPending?: boolean;
|
2020-07-10 02:20:23 -07:00
|
|
|
onClick: () => void;
|
2021-11-17 23:53:43 +05:30
|
|
|
selected: boolean;
|
|
|
|
onToggle: (id: string) => void;
|
2020-07-10 02:20:23 -07:00
|
|
|
}) => {
|
|
|
|
const ref = useRef<HTMLDivElement | null>(null);
|
|
|
|
useEffect(() => {
|
2021-10-22 22:07:20 +02:00
|
|
|
const node = ref.current;
|
|
|
|
if (!node) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-03 02:07:01 +05:30
|
|
|
(async () => {
|
2021-11-17 23:53:43 +05:30
|
|
|
if (!elements) {
|
2021-10-21 22:05:48 +02:00
|
|
|
return;
|
2020-07-10 02:20:23 -07:00
|
|
|
}
|
2021-10-21 22:05:48 +02:00
|
|
|
const svg = await exportToSvg(
|
2021-11-17 23:53:43 +05:30
|
|
|
elements,
|
2021-10-21 22:05:48 +02:00
|
|
|
{
|
|
|
|
exportBackground: false,
|
|
|
|
viewBackgroundColor: oc.white,
|
|
|
|
},
|
|
|
|
files,
|
|
|
|
);
|
2021-10-22 22:07:20 +02:00
|
|
|
node.innerHTML = svg.outerHTML;
|
2021-10-21 22:05:48 +02:00
|
|
|
})();
|
2021-10-22 22:07:20 +02:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
node.innerHTML = "";
|
|
|
|
};
|
2021-11-17 23:53:43 +05:30
|
|
|
}, [elements, files]);
|
2020-07-10 02:20:23 -07:00
|
|
|
|
|
|
|
const [isHovered, setIsHovered] = useState(false);
|
2020-07-20 00:12:56 +03:00
|
|
|
const isMobile = useIsMobile();
|
2021-11-17 23:53:43 +05:30
|
|
|
const adder = isPending && (
|
2020-07-10 02:20:23 -07:00
|
|
|
<div className="library-unit__adder">{PLUS_ICON}</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2020-10-19 17:14:28 +03:00
|
|
|
className={clsx("library-unit", {
|
2021-11-17 23:53:43 +05:30
|
|
|
"library-unit__active": elements,
|
|
|
|
"library-unit--hover": elements && isHovered,
|
|
|
|
"library-unit--selected": selected,
|
2020-10-19 17:14:28 +03:00
|
|
|
})}
|
2020-07-10 02:20:23 -07:00
|
|
|
onMouseEnter={() => setIsHovered(true)}
|
|
|
|
onMouseLeave={() => setIsHovered(false)}
|
|
|
|
>
|
|
|
|
<div
|
2020-10-19 17:14:28 +03:00
|
|
|
className={clsx("library-unit__dragger", {
|
2021-11-17 23:53:43 +05:30
|
|
|
"library-unit__pulse": !!isPending,
|
2020-10-19 17:14:28 +03:00
|
|
|
})}
|
2020-07-10 02:20:23 -07:00
|
|
|
ref={ref}
|
|
|
|
draggable={!!elements}
|
2021-11-17 23:53:43 +05:30
|
|
|
onClick={!!elements || !!isPending ? onClick : undefined}
|
2020-07-10 02:20:23 -07:00
|
|
|
onDragStart={(event) => {
|
|
|
|
setIsHovered(false);
|
|
|
|
event.dataTransfer.setData(
|
2020-10-13 14:47:07 +02:00
|
|
|
MIME_TYPES.excalidrawlib,
|
2020-07-10 02:20:23 -07:00
|
|
|
JSON.stringify(elements),
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{adder}
|
2021-11-17 23:53:43 +05:30
|
|
|
{id && elements && (isHovered || isMobile || selected) && (
|
|
|
|
<CheckboxItem
|
|
|
|
checked={selected}
|
|
|
|
onChange={() => onToggle(id)}
|
|
|
|
className="library-unit__checkbox"
|
|
|
|
/>
|
2020-07-10 02:20:23 -07:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|