import { React, useContext, useState } from "react"; import { IconCaretdown, IconChevronRight, IconShareStroked, IconChevronUp, IconChevronDown, IconCheckboxTick, IconSaveStroked, IconUndo, IconRedo, } from "@douyinfe/semi-icons"; import { Link } from "react-router-dom"; import icon from "../assets/icon_dark_64.png"; import { Avatar, AvatarGroup, Button, Divider, Dropdown, InputNumber, Image, Modal, Spin, Input, Upload, Banner, Toast, } from "@douyinfe/semi-ui"; import { toPng, toJpeg, toSvg } from "html-to-image"; import { saveAs } from "file-saver"; import { jsonDiagramIsValid, enterFullscreen, exitFullscreen, ddbDiagramIsValid, dataURItoBlob, } from "../utils"; import { AreaContext, LayoutContext, NoteContext, SettingsContext, TableContext, } from "../pages/editor"; import { AddTable, AddArea, AddNote } from "./custom_icons"; import { defaultTableTheme, defaultNoteTheme } from "../data/data"; import CodeMirror from "@uiw/react-codemirror"; import { json } from "@codemirror/lang-json"; import jsPDF from "jspdf"; export default function ControlPanel(props) { const MODAL = { NONE: 0, IMG: 1, CODE: 2, IMPORT: 3, }; const ERROR = { NONE: 0, WARNING: 1, ERROR: 2, OK: 3, }; const [visible, setVisible] = useState(MODAL.NONE); const [exportData, setExportData] = useState({ data: "", filename: `diagram_${new Date().toISOString()}`, extension: "", }); const [error, setError] = useState({ type: ERROR.NONE, message: "", }); const [data, setData] = useState(null); const { layout, setLayout } = useContext(LayoutContext); const { settings, setSettings } = useContext(SettingsContext); const { relationships, tables, setTables, setRelationships } = useContext(TableContext); const { notes, setNotes } = useContext(NoteContext); const { areas, setAreas } = useContext(AreaContext); const menu = { File: { New: { children: [], function: () => console.log("New"), }, "New window": { children: [], function: () => {}, }, Save: { children: [], function: () => {}, }, "Save as": { children: [], function: () => {}, }, Share: { children: [], function: () => {}, }, Rename: { children: [], function: () => {}, }, Import: { children: [], function: () => { setVisible(MODAL.IMPORT); }, }, "Export as": { children: [ { PNG: () => { toPng(document.getElementById("canvas")).then(function (dataUrl) { setExportData((prev) => ({ ...prev, data: dataUrl, extension: "png", })); }); setVisible(MODAL.IMG); }, }, { JPEG: () => { toJpeg(document.getElementById("canvas"), { quality: 0.95 }).then( function (dataUrl) { setExportData((prev) => ({ ...prev, data: dataUrl, extension: "jpeg", })); } ); setVisible(MODAL.IMG); }, }, { JSON: () => { setVisible(MODAL.CODE); const result = JSON.stringify( { tables: tables, relationships: relationships, notes: notes, subjectAreas: areas, }, null, 2 ); setExportData((prev) => ({ ...prev, data: result, extension: "json", })); }, }, { SVG: () => { const filter = (node) => node.tagName !== "i"; toSvg(document.getElementById("canvas"), { filter: filter }).then( function (dataUrl) { setExportData((prev) => ({ ...prev, data: dataUrl, extension: "svg", })); } ); setVisible(MODAL.IMG); }, }, { PDF: () => { const canvas = document.getElementById("canvas"); toJpeg(canvas).then(function (dataUrl) { const doc = new jsPDF("l", "px", [ canvas.offsetWidth, canvas.offsetHeight, ]); doc.addImage( dataUrl, "jpeg", 0, 0, canvas.offsetWidth, canvas.offsetHeight ); doc.save(`${exportData.filename}.pdf`); }); }, }, { DRAWDB: () => { const result = JSON.stringify( { author: "Unnamed", project: "Untitled", filename: "Untitled", date: new Date().toISOString(), tables: tables, relationships: relationships, notes: notes, subjectAreas: areas, }, null, 2 ); const blob = new Blob([result], { type: "text/plain;charset=utf-8", }); saveAs(blob, `${exportData.filename}.ddb`); }, }, ], function: () => {}, }, "Export source": { children: [ { MySQL: () => {} }, { PostgreSQL: () => {} }, { DBML: () => {} }, ], function: () => {}, }, Properties: { children: [], function: () => {}, }, Close: { children: [], function: () => {}, }, }, Edit: { Undo: { children: [], function: () => {}, }, Redo: { children: [], function: () => {}, }, Clear: { children: [], function: () => { setTables([]); setRelationships([]); setAreas([]); setNotes([]); }, }, Cut: { children: [], function: () => {}, }, Copy: { 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: () => {}, }, "Edit table": { children: [], function: () => {}, }, }, View: { Toolbar: { children: [], function: () => {}, }, Grid: { children: [], function: () => setSettings((prev) => ({ ...prev, showGrid: !prev.showGrid })), }, Sidebar: { children: [], function: () => {}, }, Editor: { children: [], function: () => {}, }, "Strict mode": { children: [], function: () => { setSettings((prev) => ({ ...prev, strictMode: !prev.strictMode })); Toast.success(`Stict mode is ${settings.strictMode ? "on" : "off"}.`); }, }, "Field summary": { children: [], function: () => { setSettings((prev) => ({ ...prev, showFieldSummary: !prev.showFieldSummary, })); Toast.success( `Field summary is ${settings.showFieldSummary ? "off" : "on"}.` ); }, }, "Reset view": { children: [], function: () => {}, }, "View schema": { children: [], function: () => {}, }, Theme: { children: [{ Light: () => {} }, { Dark: () => {} }], function: () => {}, }, "Zoom in": { children: [], function: () => setSettings((prev) => ({ ...prev, zoom: prev.zoom * 1.2 })), }, "Zoom out": { children: [], function: () => setSettings((prev) => ({ ...prev, zoom: prev.zoom / 1.2 })), }, Fullscreen: { children: [], function: enterFullscreen, }, }, Logs: { "Open logs": { children: [], function: () => {}, }, "Commit changes": { children: [], function: () => {}, }, "Revert changes": { children: [], function: () => {}, }, "View commits": { children: [], function: () => {}, }, }, Help: { Shortcuts: { children: [], function: () => {}, }, "Ask us on discord": { children: [], function: () => {}, }, "Tweet us": { children: [], function: () => {}, }, "Found a bug": { children: [], function: () => {}, }, }, }; const invertLayout = (component) => setLayout((prev) => ({ ...prev, [component]: !prev[component] })); const diagramIsEmpty = () => { return ( tables.length === 0 && relationships.length === 0 && notes.length === 0 && areas.length === 0 ); }; const overwriteDiagram = () => { setTables(data.tables); setRelationships(data.relationships); setAreas(data.subjectAreas); setNotes(data.notes); }; return (