import React, { useState } from "react";
import styles from "./Diagram.module.css";
import Xarrow, { Xwrapper, useXarrow } from "react-xarrows";
import { TransformWrapper, TransformComponent } from "react-zoom-pan-pinch";
import CameraAltIcon from '@mui/icons-material/CameraAlt';
import html2canvas from 'html2canvas';
export const Diagram = () => {
const [diagram, setDiagram] = useState({});
const updateXarrow = useXarrow();
const onDrag = () => {
setIsMoveable(true);
updateXarrow();
};
const onStop = () => {
setIsMoveable(false);
updateXarrow();
};
const takeScreenShot = () => {
const linkElement = document.getElementById("my-diag-link");
const originalLeft = linkElement.style.left;
console.log(linkElement);
linkElement.style.left = parseInt(linkElement.style.left) - 60 + "px";
const element = document.getElementById("my-diagram-area");
var date = new Date();
if (!element) {
return;
}
html2canvas(element, {
backgroundColor: "#FAFDFD",
height: 1500,
width: 2500,
foreignObjectRendering: true
}).then((canvas) => {
updateXarrow();
let image = canvas.toDataURL("image/jpeg", 1.0);
const a = document.createElement("a");
a.href = image;
a.download = `${diagram.name} ${date}.jpeg`;
a.click();
linkElement.style.left = originalLeft;
}).catch((err) => {
console.err("Нет изображения")
//linkElement.style.left = originalLeft;
})
};
const getTables = () => {
let components = [];
diagram.areas?.map((area) => {
let color = findColorByAreaName(area.name);
area.color = color ? color : area.color;
area.tables?.map((table) => {
components.push(
<Table
key={table.name}
diagram={diagram}
table={table}
areaName={area.name}
color={area.color}
onDrag={onDrag}
onStop={onStop}
/>
);
});
});
const getArrows = () => {
let arrows = [];
diagram.areas?.map((area) => {
area.foreignKeys?.map((link, key) => {
diagram.areas?.map((area) => {
area.tables?.map((table) => {
arrows.push(
<Xarrow
start={link.sourceTableName}
end={link.targetTableName}
path={"straight"}
/>
);
});
});
return arrows;
};
return (
<div className={styles.MainContainer}>
<TransformWrapper
initialScale={scale}
disabled={isMoveable}
minScale={0.4}
maxScale={1.1}
limitToBounds={false}
onPanning={updateXarrow}
pinch={{ step: 5 }}
doubleClick={{ disabled: "true" }}
// initialPositionX={200}
// initialPositionY={200}
zoomIn={{ step: 50 }}
zoomOut={{ step: 50 }}
onZoomStop={(e) => {
setScale(e.state.scale);
}}
onZoom={(e) => {
setScale(e.state.scale);
updateXarrow();
}}
>
{({ zoomIn, zoomOut }) => (
<div data-html2canvas-ignore="true">
<CameraAltIcon
onClick={takeScreenShot}
sx={{ fontSize: "large" }}
/>
</div>
<div
id="my-diagram-area"
className={styles.AreaContainer}
>
<Xwrapper>
<TransformComponent
contentClass="main"
wrapperStyle={{
height: "200vh",
width: "200vw"
}}
>
<div id="my-diag-tables" className={styles.TableContainer}>
{getTables()}
</div>
</TransformComponent>
<div id="my-diag-link" className={styles.ArrowContainer}>
{getArrows()}
</div>
</Xwrapper>
</div>
</>
)}
</TransformWrapper>
</div>
);
};
const linkElement = document.getElementById("my-diag-link");
const originalLeft = linkElement.style.left;
console.log(linkElement);
linkElement.style.left = parseInt(linkElement.style.left) - 60 + "px";