From dec6362fa9f5758a025c9cbd1d6e0a6dd82eef99 Mon Sep 17 00:00:00 2001 From: 1ilit Date: Tue, 19 Dec 2023 04:36:10 +0200 Subject: [PATCH] Add `Save as template` --- src/components/ControlPanel.jsx | 41 +++++++++++++++++-------- src/data/db.js | 2 +- src/pages/Templates.jsx | 53 +++++++++++++++++++++++++-------- src/templates/template1.js | 1 + src/templates/template2.js | 1 + src/templates/template3.js | 1 + src/templates/template4.js | 1 + src/templates/template5.js | 1 + src/templates/template6.js | 1 + src/templates/template7.js | 1 + src/templates/template8.js | 1 + 11 files changed, 78 insertions(+), 26 deletions(-) diff --git a/src/components/ControlPanel.jsx b/src/components/ControlPanel.jsx index 6c30026..7c1e658 100644 --- a/src/components/ControlPanel.jsx +++ b/src/components/ControlPanel.jsx @@ -729,6 +729,23 @@ export default function ControlPanel({ function: saveDiagramAs, shortcut: "Ctrl+Shift+S", }, + "Save as template": { + function: () => { + db.templates + .add({ + title: title, + tables: tables, + relationships: relationships, + types: types, + notes: notes, + subjectAreas: areas, + custom: 1, + }) + .then(() => { + Toast.success("Template saved!"); + }); + }, + }, Share: { function: () => {}, }, @@ -756,18 +773,6 @@ export default function ControlPanel({ .catch(() => Toast.error("Oops! Something went wrong.")); }, }, - "Flush storage": { - function: async () => { - db.delete() - .then(() => { - Toast.success("Storage flushed"); - window.location.reload(false); - }) - .catch(() => { - Toast.error("Oops! Something went wrong."); - }); - }, - }, Import: { function: fileImport, shortcut: "Ctrl+I", @@ -935,6 +940,18 @@ export default function ControlPanel({ return { ...prev, panning: !prev.panning }; }), }, + { + "Flush storage": async () => { + db.delete() + .then(() => { + Toast.success("Storage flushed"); + window.location.reload(false); + }) + .catch(() => { + Toast.error("Oops! Something went wrong."); + }); + }, + }, ], }, Exit: { diff --git a/src/data/db.js b/src/data/db.js index 62bac55..db1ba0a 100644 --- a/src/data/db.js +++ b/src/data/db.js @@ -5,7 +5,7 @@ const db = new Dexie("drawDB"); db.version(2).stores({ diagrams: "++id, lastModified", - templates: "++id", + templates: "++id, custom", }); db.on("populate", (transaction) => { diff --git a/src/pages/Templates.jsx b/src/pages/Templates.jsx index 4ade8ae..c112540 100644 --- a/src/pages/Templates.jsx +++ b/src/pages/Templates.jsx @@ -1,19 +1,18 @@ -import { useEffect } from "react"; +import { useEffect, useState } from "react"; import logo_light from "../assets/logo_light_46.png"; import { Link } from "react-router-dom"; import { Tabs, TabPane } from "@douyinfe/semi-ui"; -import { useLiveQuery } from "dexie-react-hooks"; import { db } from "../data/db"; import { calcPath } from "../utils"; -function Thumbnail({ diagram }) { +function Thumbnail({ diagram, i }) { const zoom = 0.3; return ( -
+
- + {diagram.subjectAreas?.map((a) => ( db.templates.toArray()); + const [defaultTemplates, setDefaultTemplates] = useState([]); + const [customTemplates, setCustomTemplates] = useState([]); useEffect(() => { document.title = "Templates | drawDB"; + const loadTemplates = async () => { + const def = await db.templates.where({ custom: 0 }).toArray(); + setDefaultTemplates(def); + const custom = await db.templates.where({ custom: 1 }).toArray(); + setCustomTemplates(custom); + }; + + loadTemplates(); }, []); return ( @@ -198,13 +206,13 @@ export default function Templates() { tab={Default templates} itemKey="1" > -
- {templates?.map((t, i) => ( +
+ {defaultTemplates?.map((t, i) => (
- +
@@ -224,7 +232,26 @@ export default function Templates() { tab={Your templates} itemKey="2" > -
Your templates
+
+ {customTemplates?.map((c, i) => ( +
+ +
+
+
+ {c.title} +
+ +
+
+
+ ))} +
diff --git a/src/templates/template1.js b/src/templates/template1.js index 7d8aac7..4904b1c 100644 --- a/src/templates/template1.js +++ b/src/templates/template1.js @@ -151,4 +151,5 @@ export const template1 = { title: "Template 1", description: "Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam nulla illo pariatur nostrum.", + custom: 0, }; diff --git a/src/templates/template2.js b/src/templates/template2.js index ad588c6..0960a2d 100644 --- a/src/templates/template2.js +++ b/src/templates/template2.js @@ -160,4 +160,5 @@ export const template2 = { title: "Template 2", description: "Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam nulla illo pariatur nostrum.", + custom: 0, }; diff --git a/src/templates/template3.js b/src/templates/template3.js index 3b1dacb..75f6d34 100644 --- a/src/templates/template3.js +++ b/src/templates/template3.js @@ -227,4 +227,5 @@ export const template3 = { title: "Template 3", description: "Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam nulla illo pariatur nostrum.", + custom: 0, }; diff --git a/src/templates/template4.js b/src/templates/template4.js index 178880a..d2780eb 100644 --- a/src/templates/template4.js +++ b/src/templates/template4.js @@ -307,4 +307,5 @@ export const template4 = { title: "Template 4", description: "Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam nulla illo pariatur nostrum.", + custom: 0, }; diff --git a/src/templates/template5.js b/src/templates/template5.js index ba5cb2a..e7275f9 100644 --- a/src/templates/template5.js +++ b/src/templates/template5.js @@ -265,4 +265,5 @@ export const template5 = { title: "Template 5", description: "Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam nulla illo pariatur nostrum.", + custom: 0, }; diff --git a/src/templates/template6.js b/src/templates/template6.js index ba094cc..d4426a1 100644 --- a/src/templates/template6.js +++ b/src/templates/template6.js @@ -301,4 +301,5 @@ export const template6 = { title: "Template 6", description: "Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam nulla illo pariatur nostrum.", + custom: 0, }; diff --git a/src/templates/template7.js b/src/templates/template7.js index 1e1e012..bcff0aa 100644 --- a/src/templates/template7.js +++ b/src/templates/template7.js @@ -311,4 +311,5 @@ export const template7 = { title: "Template 7", description: "Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam nulla illo pariatur nostrum.", + custom: 0, }; diff --git a/src/templates/template8.js b/src/templates/template8.js index cb8baaa..b4fb939 100644 --- a/src/templates/template8.js +++ b/src/templates/template8.js @@ -366,4 +366,5 @@ export const template8 = { title: "Template 8", description: "Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam nulla illo pariatur nostrum.", + custom: 0, };