drawDB/src/components/editor_panel.jsx

170 lines
4.6 KiB
React
Raw Normal View History

2023-09-19 20:48:14 +08:00
import { React, useState, useRef } from "react";
2023-09-19 20:47:01 +08:00
import { ResizableBox } from "react-resizable";
import CodeMirror from "@uiw/react-codemirror";
import { createTheme } from "@uiw/codemirror-themes";
import { sql } from "@codemirror/lang-sql";
import { tags as t } from "@lezer/highlight";
import Shape from "./shape";
2023-09-19 20:47:04 +08:00
import { Parser } from "node-sql-parser";
2023-09-19 20:47:50 +08:00
import { Tabs } from "@douyinfe/semi-ui";
2023-09-19 20:47:01 +08:00
import "react-resizable/css/styles.css";
import TableOverview from "./table_overview";
import ReferenceOverview from "./reference_overview";
2023-09-19 20:47:54 +08:00
import { defaultTableTheme } from "../data/data";
2023-09-19 20:47:01 +08:00
const myTheme = createTheme({
dark: "light",
settings: {},
styles: [
{ tag: t.comment, color: "#8ab0ed" },
{ tag: t.string, color: "#e68e29" },
{ tag: t.number, color: "#e68e29" },
{ tag: t.keyword, color: "#295be6" },
{ tag: t.variableName, color: "#1a00db" },
{ tag: t.typeName, color: "#295be6" },
{ tag: t.tagName, color: "#008a02" },
],
});
2023-09-19 20:48:14 +08:00
const EditorPanel = (props) => {
2023-09-19 20:47:51 +08:00
const [tab, setTab] = useState("1");
2023-09-19 20:47:04 +08:00
const map = useRef(new Map());
2023-09-19 20:47:01 +08:00
2023-09-19 20:47:50 +08:00
const tabList = [
{ tab: "Tables", itemKey: "1" },
{ tab: "References", itemKey: "2" },
{ tab: "Shapes", itemKey: "3" },
{ tab: "Editor", itemKey: "4" },
2023-09-19 20:47:50 +08:00
];
const contentList = [
2023-09-19 20:47:51 +08:00
<div>
2023-09-19 20:48:14 +08:00
<TableOverview tables={props.tables} setTables={props.setTables} />
</div>,
<div>
<ReferenceOverview
relationships={props.relationships}
2023-09-19 20:48:18 +08:00
setRelationships={props.setRelationships}
tables={props.tables}
/>
2023-09-19 20:47:51 +08:00
</div>,
2023-09-19 20:47:50 +08:00
<div>
<Shape />
</div>,
<div>
<CodeMirror
value={props.code}
height="100%"
theme={myTheme}
extensions={[sql()]}
onChange={(e) => {
props.setCode(e);
}}
/>
</div>,
];
2023-09-19 20:47:01 +08:00
return (
<ResizableBox
2023-09-19 20:47:51 +08:00
className="shadow-xl"
width={window.innerWidth * 0.23}
2023-09-19 20:47:01 +08:00
height={window.innerHeight}
resizeHandles={["e"]}
2023-09-19 20:47:51 +08:00
minConstraints={[window.innerWidth * 0.23, window.innerHeight]}
2023-09-19 20:47:01 +08:00
maxConstraints={[Infinity, Infinity]}
axis="x"
>
2023-09-19 20:47:50 +08:00
<div className="overflow-auto h-full mt-2">
<Tabs
type="card"
tabList={tabList}
onChange={(key) => {
setTab(key);
2023-09-19 20:47:01 +08:00
}}
2023-09-19 20:48:29 +08:00
collapsible
2023-09-19 20:47:01 +08:00
>
2023-09-19 20:47:51 +08:00
{contentList[parseInt(tab) - 1]}
2023-09-19 20:47:50 +08:00
</Tabs>
2023-09-19 20:48:04 +08:00
<button
onClick={() => {
const newArea = {
id: props.areas.length,
name: `area_${props.areas.length}`,
x: 0,
y: 0,
width: 200,
height: 200,
color: defaultTableTheme,
};
props.setAreas((prev) => {
const updatedTables = [...prev, newArea];
return updatedTables;
});
}}
>
add area
</button>
<br />
2023-09-19 20:47:03 +08:00
<button
onClick={() => {
const blob = new Blob([props.code], {
type: "text/plain;charset=utf-8",
});
2023-09-19 20:48:25 +08:00
window.saveAs(blob, "src.txt");
2023-09-19 20:47:03 +08:00
}}
>
export src
</button>
<br />
2023-09-19 20:47:04 +08:00
<button
onClick={() => {
try {
const parser = new Parser();
const ast = parser.astify(props.code);
console.log(ast);
ast.forEach((e) => {
e.table.forEach((t) => {
if (map.current.has(t.table)) {
return;
}
map.current.set(t.table, t);
const newTable = {
2023-09-19 20:47:54 +08:00
id: props.tables.length,
name: `table_${props.tables.length}`,
2023-09-19 20:47:08 +08:00
x: 0,
y: 0,
fields: [
{
name: "id",
type: "UUID",
default: "",
2023-09-19 20:47:56 +08:00
check: "",
primary: true,
unique: true,
notNull: true,
increment: true,
2023-09-19 20:47:52 +08:00
comment: "",
},
],
2023-09-19 20:47:51 +08:00
comment: "",
indices: [],
2023-09-19 20:47:54 +08:00
color: defaultTableTheme,
2023-09-19 20:47:08 +08:00
};
2023-09-19 20:47:50 +08:00
props.setTables((prev) => [...prev, newTable]);
2023-09-19 20:47:04 +08:00
});
});
} catch (e) {
alert("parsing error");
}
}}
>
parse
</button>
2023-09-19 20:47:01 +08:00
</div>
</ResizableBox>
);
2023-09-19 20:48:14 +08:00
};
2023-09-19 20:48:13 +08:00
export default EditorPanel;