From 4596864e2f638e373f7427b8b3fd5a82b37b87ed Mon Sep 17 00:00:00 2001 From: 1ilit Date: Thu, 26 Oct 2023 16:34:50 +0300 Subject: [PATCH] Save diagram to IndexedDb --- src/components/ControlPanel.jsx | 20 ++++++++++++++++++-- src/data/db.js | 9 +++++++++ src/pages/LandingPage.jsx | 18 ++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 src/data/db.js diff --git a/src/components/ControlPanel.jsx b/src/components/ControlPanel.jsx index 43e95b9..6f63124 100644 --- a/src/components/ControlPanel.jsx +++ b/src/components/ControlPanel.jsx @@ -59,6 +59,7 @@ import { useHotkeys } from "react-hotkeys-hook"; import { Validator } from "jsonschema"; import { areaSchema, noteSchema, tableSchema } from "../data/schemas"; import { Editor } from "@monaco-editor/react"; +import { db } from "../data/db"; export default function ControlPanel(props) { const MODAL = { @@ -658,6 +659,17 @@ export default function ControlPanel(props) { copy(); del(); }; + const save = () => { + console.log("saving"); + db.diagrams.add({ + name: title, + tables: tables, + references: relationships, + types: types, + notes: notes, + areas: areas, + }); + }; const menu = { File: { @@ -671,7 +683,8 @@ export default function ControlPanel(props) { function: () => {}, }, Save: { - function: () => {}, + function: save, + shortcut: "Ctrl+S", }, "Save as": { function: () => {}, @@ -1401,7 +1414,10 @@ export default function ControlPanel(props) { - diff --git a/src/data/db.js b/src/data/db.js new file mode 100644 index 0000000..881e163 --- /dev/null +++ b/src/data/db.js @@ -0,0 +1,9 @@ +import Dexie from "dexie"; + +const db = new Dexie("diagrams"); + +db.version(1).stores({ + diagrams: "++id", +}); + +export { db }; diff --git a/src/pages/LandingPage.jsx b/src/pages/LandingPage.jsx index d4bb25c..b551104 100644 --- a/src/pages/LandingPage.jsx +++ b/src/pages/LandingPage.jsx @@ -2,10 +2,27 @@ import React, { useState, useEffect } from "react"; import { Link } from "react-router-dom"; import { IconCrossStroked } from "@douyinfe/semi-icons"; import Navbar from "../components/Navbar"; +import { useLiveQuery } from "dexie-react-hooks"; +import { db } from "../data/db"; export default function LandingPage() { const [showSurvey, setShowSurvey] = useState(true); + const clearDatabase = () => { + db.delete() + .then(() => { + console.log("Database cleared."); + }) + .catch((error) => { + console.error("Failed to clear the database:", error); + }); + }; + + const diagrams = useLiveQuery(() => db.diagrams.toArray()); + useEffect(() => { + console.log(diagrams); + }, [diagrams]); + useEffect(() => { document.body.setAttribute("theme-mode", "light"); document.title = @@ -27,6 +44,7 @@ export default function LandingPage() { )} + ); }