Import ddb files

This commit is contained in:
1ilit 2023-09-19 15:49:31 +03:00
parent a4a170d97a
commit 6eaad6e720
3 changed files with 57 additions and 29 deletions

View File

@ -29,9 +29,10 @@ import {
import { toPng, toJpeg, toSvg } from "html-to-image"; import { toPng, toJpeg, toSvg } from "html-to-image";
import { saveAs } from "file-saver"; import { saveAs } from "file-saver";
import { import {
diagramObjectIsValid, jsonDiagramIsValid,
enterFullscreen, enterFullscreen,
exitFullscreen, exitFullscreen,
ddbDiagramIsValid,
} from "../utils"; } from "../utils";
import { import {
AreaContext, AreaContext,
@ -200,7 +201,7 @@ export default function ControlPanel(props) {
author: "Unnamed", author: "Unnamed",
project: "Untitled", project: "Untitled",
filename: "Untitled", filename: "Untitled",
date: Date().toISOString(), date: new Date().toISOString(),
tables: tables, tables: tables,
relationships: relationships, relationships: relationships,
notes: notes, notes: notes,
@ -604,19 +605,18 @@ export default function ControlPanel(props) {
const reader = new FileReader(); const reader = new FileReader();
reader.onload = function (event) { reader.onload = function (event) {
if (f.type === "application/json") {
let jsonObject = null; let jsonObject = null;
try { try {
jsonObject = JSON.parse(event.target.result); jsonObject = JSON.parse(event.target.result);
} catch (error) { } catch (error) {
setError({ setError({
type: ERROR.ERROR, type: ERROR.ERROR,
message: "Invalid JSON. The file contains an error.", message: "The file contains an error.",
}); });
return; return;
} }
if (f.type === "application/json") {
if (!diagramObjectIsValid(jsonObject)) { if (!jsonDiagramIsValid(jsonObject)) {
setError({ setError({
type: ERROR.ERROR, type: ERROR.ERROR,
message: message:
@ -624,7 +624,16 @@ export default function ControlPanel(props) {
}); });
return; return;
} }
} else if (f.name.split(".").pop() === "ddb") {
if (!ddbDiagramIsValid(jsonObject)) {
setError({
type: ERROR.ERROR,
message:
"The file is missing necessary properties for a diagram.",
});
return;
}
}
setData(jsonObject); setData(jsonObject);
if (diagramIsEmpty()) { if (diagramIsEmpty()) {
setError({ setError({
@ -638,7 +647,6 @@ export default function ControlPanel(props) {
"The current diagram is not empty. Importing a new diagram will overwrite the current changes.", "The current diagram is not empty. Importing a new diagram will overwrite the current changes.",
}); });
} }
}
}; };
reader.readAsText(f); reader.readAsText(f);
@ -652,7 +660,7 @@ export default function ControlPanel(props) {
draggable={true} draggable={true}
dragMainText="Click to upload the file or drag and drop the file here" dragMainText="Click to upload the file or drag and drop the file here"
dragSubText="Support json" dragSubText="Support json"
accept="application/json,.txt" accept="application/json,.ddb"
onRemove={() => onRemove={() =>
setError({ setError({
type: ERROR.NONE, type: ERROR.NONE,

View File

@ -141,4 +141,15 @@ const jsonSchema = {
required: ["tables", "relationships", "notes", "subjectAreas"], required: ["tables", "relationships", "notes", "subjectAreas"],
}; };
export { jsonSchema }; const ddbSchema = {
type: "object",
properties: {
author: { type: "string" },
project: { type: "string" },
filename: { type: "string" },
date: { type: "string" },
...jsonSchema.properties,
},
};
export { jsonSchema, ddbSchema };

View File

@ -1,5 +1,5 @@
import { Validator } from "jsonschema"; import { Validator } from "jsonschema";
import { jsonSchema } from "../schemas"; import { ddbSchema, jsonSchema } from "../schemas";
const enterFullscreen = () => { const enterFullscreen = () => {
const element = document.documentElement; const element = document.documentElement;
@ -26,8 +26,17 @@ const exitFullscreen = () => {
} }
}; };
const diagramObjectIsValid = (obj) => { const jsonDiagramIsValid = (obj) => {
return new Validator().validate(obj, jsonSchema).valid; return new Validator().validate(obj, jsonSchema).valid;
}; };
export { enterFullscreen, exitFullscreen, diagramObjectIsValid }; const ddbDiagramIsValid = (obj) => {
return new Validator().validate(obj, ddbSchema).valid;
};
export {
enterFullscreen,
exitFullscreen,
jsonDiagramIsValid,
ddbDiagramIsValid,
};