From d2515a0bcb4f3a5bb3c80eeff8c8b1fcc6dcaa49 Mon Sep 17 00:00:00 2001 From: 1ilit Date: Tue, 19 Sep 2023 15:49:39 +0300 Subject: [PATCH] Clear diagram, copy as image --- src/components/control_panel.jsx | 30 ++++++++++++++++++++++++++---- src/components/editor_panel.jsx | 2 +- src/pages/editor.jsx | 6 +++--- src/utils/index.js | 14 ++++++++++++++ 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/components/control_panel.jsx b/src/components/control_panel.jsx index 52434c4..88c0f8b 100644 --- a/src/components/control_panel.jsx +++ b/src/components/control_panel.jsx @@ -34,6 +34,7 @@ import { enterFullscreen, exitFullscreen, ddbDiagramIsValid, + dataURItoBlob, } from "../utils"; import { AreaContext, @@ -246,6 +247,15 @@ export default function ControlPanel(props) { children: [], function: () => {}, }, + Clear: { + children: [], + function: () => { + setTables([]); + setRelationships([]); + setAreas([]); + setNotes([]); + }, + }, Cut: { children: [], function: () => {}, @@ -254,14 +264,26 @@ export default function ControlPanel(props) { children: [], function: () => {}, }, - "Copy as image": { - children: [], - function: () => {}, - }, Paste: { children: [], function: () => {}, }, + "Copy as image": { + children: [], + function: () => { + toPng(document.getElementById("canvas")).then(function (dataUrl) { + const blob = dataURItoBlob(dataUrl); + navigator.clipboard + .write([new ClipboardItem({ "image/png": blob })]) + .then(() => { + Toast.success("Copied to clipboard."); + }) + .catch((error) => { + Toast.error("Could not copy to clipboard."); + }); + }); + }, + }, Delete: { children: [], function: () => {}, diff --git a/src/components/editor_panel.jsx b/src/components/editor_panel.jsx index 4bc1b84..3a358ad 100644 --- a/src/components/editor_panel.jsx +++ b/src/components/editor_panel.jsx @@ -60,7 +60,7 @@ const EditorPanel = (props) => { return (
diff --git a/src/pages/editor.jsx b/src/pages/editor.jsx index c106c27..7b54922 100644 --- a/src/pages/editor.jsx +++ b/src/pages/editor.jsx @@ -21,7 +21,7 @@ export default function Editor(props) { const [areas, setAreas] = useState([]); const [notes, setNotes] = useState([]); const [resize, setResize] = useState(false); - const [width, setWidth] = useState(320); + const [width, setWidth] = useState(340); const [selectedTable, setSelectedTable] = useState(""); const [tab, setTab] = useState(Tab.tables); const [layout, setLayout] = useState({ @@ -46,7 +46,7 @@ export default function Editor(props) { const dragHandler = (e) => { if (!resize) return; const w = e.clientX; - if (w > 320) setWidth(w); + if (w > 340) setWidth(w); }; useEffect(() => { @@ -61,7 +61,7 @@ export default function Editor(props) { - +
{ return new Validator().validate(obj, ddbSchema).valid; }; +function dataURItoBlob(dataUrl) { + const byteString = atob(dataUrl.split(",")[1]); + const mimeString = dataUrl.split(",")[0].split(":")[1].split(";")[0]; + const arrayBuffer = new ArrayBuffer(byteString.length); + const intArray = new Uint8Array(arrayBuffer); + + for (let i = 0; i < byteString.length; i++) { + intArray[i] = byteString.charCodeAt(i); + } + + return new Blob([intArray], { type: mimeString }); +} + export { enterFullscreen, exitFullscreen, jsonDiagramIsValid, ddbDiagramIsValid, + dataURItoBlob, };