Skip to content

Commit

Permalink
feat: add save diagram to localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenmate committed Jun 25, 2024
1 parent d79064b commit 28e0a77
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/components/DiagramEditor/DiagramEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
Select,
} from "@mui/material";
import { default as MxGraph } from "mxgraph";
import { mxConstants, mxPoint } from "mxgraph-js";
import { mxCodec, mxConstants, mxPoint, mxUtils } from "mxgraph-js";
import toast, { Toaster } from "react-hot-toast";
import { generateSQL } from "../../utils/sql";
import { POSSIBLE_CARDINALITIES, validateGraph } from "../../utils/validation";
Expand Down Expand Up @@ -86,6 +86,17 @@ export default function App(props) {
return graph.model.cells[idMx];
}

const saveToLocalStorage = () => {
const diagramData = JSON.stringify(diagramRef.current);
localStorage.setItem("diagramData", diagramData);

const encoder = new mxCodec();
const node = encoder.encode(graph.getModel());
const xmlString = mxUtils.getXml(node); // fetch XML (string or document/node)
localStorage.setItem("graphData", xmlString);

console.log(xmlString);
};
React.useEffect(() => {
if (!graph) {
mxEvent.disableContextMenu(containerRef.current);
Expand Down Expand Up @@ -147,6 +158,7 @@ export default function App(props) {
entity.position.y = cellData.geometry.y;

updateEntityAttributes(entity);
saveToLocalStorage();
}
});

Expand Down

0 comments on commit 28e0a77

Please sign in to comment.