From b41942c794a06cc0e393e47ad3ac253e4df9f4dd Mon Sep 17 00:00:00 2001 From: 1ilit Date: Sat, 21 Oct 2023 19:45:13 +0300 Subject: [PATCH] Rename diagram --- public/manifest.json | 14 +- src/App.js | 2 +- src/components/ControlPanel.jsx | 403 +++++++++++++++++++------------- 3 files changed, 245 insertions(+), 174 deletions(-) diff --git a/public/manifest.json b/public/manifest.json index 080d6c7..89658a2 100644 --- a/public/manifest.json +++ b/public/manifest.json @@ -1,21 +1,11 @@ { - "short_name": "React App", - "name": "Create React App Sample", + "short_name": "drawDB", + "name": "drawDB", "icons": [ { "src": "favicon.ico", "sizes": "64x64 32x32 24x24 16x16", "type": "image/x-icon" - }, - { - "src": "logo192.png", - "type": "image/png", - "sizes": "192x192" - }, - { - "src": "logo512.png", - "type": "image/png", - "sizes": "512x512" } ], "start_url": ".", diff --git a/src/App.js b/src/App.js index a0c7924..e0ac6fe 100644 --- a/src/App.js +++ b/src/App.js @@ -12,7 +12,7 @@ function App() { } /> - } /> + } /> } /> } /> } /> diff --git a/src/components/ControlPanel.jsx b/src/components/ControlPanel.jsx index 9f5185a..93e2a5e 100644 --- a/src/components/ControlPanel.jsx +++ b/src/components/ControlPanel.jsx @@ -10,6 +10,7 @@ import { IconUndo, IconRedo, IconRowsStroked, + IconEdit, } from "@douyinfe/semi-icons"; import { Link } from "react-router-dom"; import icon from "../assets/icon_dark_64.png"; @@ -65,6 +66,7 @@ export default function ControlPanel(props) { IMG: 1, CODE: 2, IMPORT: 3, + RENAME: 4, }; const STATUS = { NONE: 0, @@ -73,6 +75,9 @@ export default function ControlPanel(props) { OK: 3, }; const [visible, setVisible] = useState(MODAL.NONE); + const [title, setTitle] = useState("Untitled Diagram"); + const [prevTitle, setPrevTitle] = useState(title); + const [showEditName, setShowEditName] = useState(false); const [exportData, setExportData] = useState({ data: null, filename: `diagram_${new Date().toISOString()}`, @@ -672,7 +677,10 @@ export default function ControlPanel(props) { function: () => {}, }, Rename: { - function: () => {}, + function: () => { + setVisible(MODAL.RENAME); + setPrevTitle(title); + }, }, Import: { function: fileImport, @@ -1009,35 +1017,228 @@ export default function ControlPanel(props) { }); useHotkeys("ctrl+alt+w, meta+alt+w", fitWindow, { preventDefault: true }); + const getModalTitle = () => { + switch (visible) { + case MODAL.IMPORT: + return "Import diagram"; + case MODAL.CODE: + return "Export diagram"; + case MODAL.IMG: + return "Export image"; + case MODAL.RENAME: + return "Rename diagram"; + default: + return ""; + } + }; + + const getOkText = () => { + switch (visible) { + case MODAL.IMPORT: + return "Import"; + case MODAL.CODE: + case MODAL.IMG: + return "Export"; + case MODAL.RENAME: + return "Rename"; + default: + return ""; + } + }; + + const getModalOnOk = () => { + switch (visible) { + case MODAL.IMG: + saveAs( + exportData.data, + `${exportData.filename}.${exportData.extension}` + ); + return; + case MODAL.CODE: + const blob = new Blob([exportData.data], { + type: "application/json", + }); + saveAs(blob, `${exportData.filename}.${exportData.extension}`); + return; + case MODAL.IMPORT: + if (error.type !== STATUS.ERROR) { + setSettings((prev) => ({ ...prev, pan: { x: 0, y: 0 } })); + overwriteDiagram(); + setData(null); + setVisible(MODAL.NONE); + setUndoStack([]); + setRedoStack([]); + } + return; + default: + setVisible(MODAL.NONE); + return; + } + }; + + const importModalBody = () => { + return ( + <> + { + const f = fileList[0].fileInstance; + if (!f) { + return; + } + const reader = new FileReader(); + reader.onload = function (event) { + let jsonObject = null; + try { + jsonObject = JSON.parse(event.target.result); + } catch (error) { + setError({ + type: STATUS.ERROR, + message: "The file contains an error.", + }); + return; + } + if (f.type === "application/json") { + if (!jsonDiagramIsValid(jsonObject)) { + setError({ + type: STATUS.ERROR, + message: + "The file is missing necessary properties for a diagram.", + }); + return; + } + } else if (f.name.split(".").pop() === "ddb") { + if (!ddbDiagramIsValid(jsonObject)) { + setError({ + type: STATUS.ERROR, + message: + "The file is missing necessary properties for a diagram.", + }); + return; + } + } + setData(jsonObject); + if (diagramIsEmpty()) { + setError({ + type: STATUS.OK, + message: "Everything looks good. You can now import.", + }); + } else { + setError({ + type: STATUS.WARNING, + message: + "The current diagram is not empty. Importing a new diagram will overwrite the current changes.", + }); + } + }; + reader.readAsText(f); + + return { + autoRemove: false, + fileInstance: file.fileInstance, + status: "success", + shouldUpload: false, + }; + }} + draggable={true} + dragMainText="Drag and drop the file here or click to upload." + dragSubText="Support json and ddb" + accept="application/json,.ddb" + onRemove={() => + setError({ + type: STATUS.NONE, + message: "", + }) + } + onFileChange={() => + setError({ + type: STATUS.NONE, + message: "", + }) + } + limit={1} + > + {error.type === STATUS.ERROR ? ( + {error.message}} + /> + ) : error.type === STATUS.OK ? ( + {error.message}} + /> + ) : ( + error.type === STATUS.WARNING && ( + {error.message}} + /> + ) + )} + + ); + }; + + const getModalBody = () => { + if (visible === MODAL.IMPORT) { + return importModalBody(); + } + if (visible === MODAL.RENAME) { + return ( + setTitle(v)} + /> + ); + } + if (exportData.data !== "" || exportData.data) { + return ( + <> + {visible === MODAL.IMG ? ( + Diagram + ) : ( + + )} +
Filename:
+ {`.${exportData.extension}`}} + onChange={(value) => + setExportData((prev) => ({ ...prev, filename: value })) + } + field="filename" + /> + + ); + } else { + return ( +
+ +
+ ); + } + }; + return ( <> {layout.header && header()} {toolbar()} { - if (visible === MODAL.IMG) { - saveAs( - exportData.data, - `${exportData.filename}.${exportData.extension}` - ); - } else if (visible === MODAL.CODE) { - const blob = new Blob([exportData.data], { - type: "application/json", - }); - saveAs(blob, `${exportData.filename}.${exportData.extension}`); - } else if (visible === MODAL.IMPORT) { - if (error.type !== STATUS.ERROR) { - setSettings((prev) => ({ ...prev, pan: { x: 0, y: 0 } })); - overwriteDiagram(); - setData(null); - setVisible(MODAL.NONE); - setUndoStack([]); - setRedoStack([]); - } - } - }} + onOk={getModalOnOk} afterClose={() => { setExportData((prev) => ({ data: "", @@ -1050,10 +1251,13 @@ export default function ControlPanel(props) { }); setData(null); }} - onCancel={() => setVisible(MODAL.NONE)} + onCancel={() => { + if (visible === MODAL.RENAME) setTitle(prevTitle); + setVisible(MODAL.NONE); + }} centered closeOnEsc={true} - okText={`${visible === MODAL.IMPORT ? "Import" : "Export"}`} + okText={getOkText()} okButtonProps={{ disabled: (visible === MODAL.IMPORT && @@ -1064,140 +1268,7 @@ export default function ControlPanel(props) { cancelText="Cancel" width={600} > - {visible === MODAL.IMPORT ? ( -
- { - const f = fileList[0].fileInstance; - if (!f) { - return; - } - const reader = new FileReader(); - reader.onload = function (event) { - let jsonObject = null; - try { - jsonObject = JSON.parse(event.target.result); - } catch (error) { - setError({ - type: STATUS.ERROR, - message: "The file contains an error.", - }); - return; - } - if (f.type === "application/json") { - if (!jsonDiagramIsValid(jsonObject)) { - setError({ - type: STATUS.ERROR, - message: - "The file is missing necessary properties for a diagram.", - }); - return; - } - } else if (f.name.split(".").pop() === "ddb") { - if (!ddbDiagramIsValid(jsonObject)) { - setError({ - type: STATUS.ERROR, - message: - "The file is missing necessary properties for a diagram.", - }); - return; - } - } - setData(jsonObject); - if (diagramIsEmpty()) { - setError({ - type: STATUS.OK, - message: "Everything looks good. You can now import.", - }); - } else { - setError({ - type: STATUS.WARNING, - message: - "The current diagram is not empty. Importing a new diagram will overwrite the current changes.", - }); - } - }; - reader.readAsText(f); - - return { - autoRemove: false, - fileInstance: file.fileInstance, - status: "success", - shouldUpload: false, - }; - }} - draggable={true} - dragMainText="Click to upload the file or drag and drop the file here" - dragSubText="Support json" - accept="application/json,.ddb" - onRemove={() => - setError({ - type: STATUS.NONE, - message: "", - }) - } - onFileChange={() => - setError({ - type: STATUS.NONE, - message: "", - }) - } - limit={1} - > - {error.type === STATUS.ERROR ? ( - {error.message}
- } - /> - ) : error.type === STATUS.OK ? ( - {error.message}} - /> - ) : ( - error.type === STATUS.WARNING && ( - {error.message}} - /> - ) - )} - - ) : exportData.data !== "" || exportData.data ? ( - <> - {visible === MODAL.IMG ? ( - Diagram - ) : ( - - )} -
Filename:
- {`.${exportData.extension}`}} - onChange={(value) => - setExportData((prev) => ({ ...prev, filename: value })) - } - field="filename" - /> - - ) : ( -
- -
- )} + {getModalBody()}
); @@ -1360,7 +1431,17 @@ export default function ControlPanel(props) { />
-
Project1 / Untitled
+
+
setShowEditName(true)} + onMouseLeave={() => setShowEditName(false)} + onClick={() => setVisible(MODAL.RENAME)} + > + {title} +
+ {(showEditName || visible === MODAL.RENAME) && } +
{Object.keys(menu).map((category) => (