drawDB/src/components/editor_panel.jsx

162 lines
4.4 KiB
React
Raw Normal View History

2023-09-19 20:47:04 +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:03 +08:00
import { saveAs } from "file-saver";
import html2canvas from "html2canvas";
2023-09-19 20:47:04 +08:00
import { Parser } from "node-sql-parser";
2023-09-19 20:47:01 +08:00
import "react-resizable/css/styles.css";
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" },
],
});
export default function EditorPanel(props) {
const [editor, setEditor] = useState(true);
2023-09-19 20:47:04 +08:00
const map = useRef(new Map());
2023-09-19 20:47:01 +08:00
return (
<ResizableBox
width={window.innerWidth * 0.2}
height={window.innerHeight}
resizeHandles={["e"]}
minConstraints={[window.innerWidth * 0.2, window.innerHeight]}
maxConstraints={[Infinity, Infinity]}
axis="x"
>
<div className="overflow-auto h-full">
<button
onClick={() => {
setEditor(!editor);
}}
>
change view
</button>
<br />
<button
onClick={() => {
const newTable = {
id: props.tables.length + 1,
name: `Table ${props.tables.length + 1}`,
2023-09-19 20:47:06 +08:00
x: 0,
y: 0,
fields: [
{
name: "id",
type: "UUID",
default: "",
primary: true,
unique: true,
notNull: true,
increment: true,
},
],
2023-09-19 20:47:06 +08:00
};
props.setTables(prev => {
const updatedTables = [...prev, newTable];
return updatedTables;
});
2023-09-19 20:47:10 +08:00
props.setCode((prev) =>
prev === ""
? `CREATE TABLE \`${newTable.name}\`;`
: `${prev}\n\nCREATE TABLE \`${newTable.name}\`;`
2023-09-19 20:47:10 +08:00
);
2023-09-19 20:47:01 +08:00
}}
>
add
</button>
2023-09-19 20:47:03 +08:00
<br />
<button
onClick={() => {
const blob = new Blob([props.code], {
type: "text/plain;charset=utf-8",
});
saveAs(blob, "src.txt");
}}
>
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 = {
id: props.tables.length + 1,
name: `Table ${props.tables.length + 1}`,
2023-09-19 20:47:08 +08:00
x: 0,
y: 0,
fields: [
{
name: "id",
type: "UUID",
default: "",
primary: true,
unique: true,
notNull: true,
increment: true,
},
],
2023-09-19 20:47:08 +08:00
};
props.setTables((prev)=>[...prev, newTable]);
2023-09-19 20:47:04 +08:00
});
});
} catch (e) {
alert("parsing error");
}
}}
>
parse
</button>
<br />
2023-09-19 20:47:03 +08:00
<button
onClick={() => {
html2canvas(document.getElementById("canvas")).then((canvas) => {
canvas.toBlob((blob) => {
saveAs(blob, "image.png");
});
});
}}
>
export img
</button>
2023-09-19 20:47:01 +08:00
{editor ? (
<CodeMirror
value={props.code}
height="100%"
theme={myTheme}
extensions={[sql()]}
2023-09-19 20:47:04 +08:00
onChange={(e) => {
props.setCode(e);
}}
2023-09-19 20:47:01 +08:00
/>
) : (
<Shape />
)}
</div>
</ResizableBox>
);
}