From 18bc292879113e7a196cb94de99a5f505460ddcf Mon Sep 17 00:00:00 2001
From: Shahriar <31452340+ShahriarKh@users.noreply.github.com>
Date: Sat, 20 Jan 2024 01:06:55 +0330
Subject: [PATCH] export as image
---
admin/src/components/ExportModal.js | 129 ++++++++++++++++++++++++++++
admin/src/pages/HomePage/index.js | 21 +++--
admin/src/store/index.js | 6 ++
package-lock.json | 6 ++
package.json | 1 +
5 files changed, 158 insertions(+), 5 deletions(-)
create mode 100644 admin/src/components/ExportModal.js
diff --git a/admin/src/components/ExportModal.js b/admin/src/components/ExportModal.js
new file mode 100644
index 0000000..8543bbc
--- /dev/null
+++ b/admin/src/components/ExportModal.js
@@ -0,0 +1,129 @@
+import {
+ ModalLayout,
+ ModalHeader,
+ ModalFooter,
+ ModalBody,
+ Button,
+ Typography,
+ SingleSelect,
+ SingleSelectOption,
+ NumberInput,
+} from "@strapi/design-system";
+import { toJpeg, toPng, toSvg } from "html-to-image";
+import { useCallback, useState } from "react";
+import { useDigramStore } from "../store";
+import { useTheme } from "styled-components";
+
+export function ExportModal({ imageRef }) {
+ const theme = useTheme();
+
+ const { setShowModal } = useDigramStore();
+
+ const [format, setFormat] = useState("png");
+ const [quality, setQuality] = useState(0.95);
+
+ function downloadImage(dataUrl, fileExtension) {
+ const link = document.createElement("a");
+ link.download = `strapi-diagram-${new Date()
+ .toISOString()
+ .replace(/[-:T.]/g, "")
+ .slice(0, -5)}.${fileExtension}`;
+ link.href = dataUrl;
+ link.click();
+ }
+
+ const exportDiagram = useCallback(() => {
+ if (imageRef.current === null) {
+ return;
+ }
+
+ const filter = (node) => {
+ const exclusionClasses = ["cte-plugin-controls"];
+ return !exclusionClasses.some((classname) =>
+ node.classList?.contains(classname)
+ );
+ };
+
+ if (format == "png") {
+ toPng(imageRef.current, {
+ cacheBust: true,
+ filter: filter,
+ style: {
+ background: theme.colors.neutral100,
+ },
+ })
+ .then((dataUrl) => {
+ downloadImage(dataUrl, "png");
+ })
+ .catch((err) => {
+ console.log(err);
+ });
+ } else if (format == "svg") {
+ toSvg(imageRef.current, {
+ cacheBust: true,
+ filter: filter,
+ style: {
+ background: theme.colors.neutral100,
+ },
+ })
+ .then((dataUrl) => {
+ downloadImage(dataUrl, "svg");
+ })
+ .catch((err) => {
+ console.log(err);
+ });
+ } else if (format == "jpeg") {
+ toJpeg(imageRef.current, {
+ cacheBust: true,
+ filter: filter,
+ quality: quality,
+ style: {
+ background: theme.colors.neutral100,
+ },
+ })
+ .then((dataUrl) => {
+ downloadImage(dataUrl, "jpeg");
+ })
+ .catch((err) => {
+ console.log(err);
+ });
+ }
+ }, [imageRef, quality, format]);
+
+ return (
+