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 { saveAs } from "file-saver";
import {
diagramObjectIsValid,
jsonDiagramIsValid,
enterFullscreen,
exitFullscreen,
ddbDiagramIsValid,
} from "../utils";
import {
AreaContext,
@ -200,7 +201,7 @@ export default function ControlPanel(props) {
author: "Unnamed",
project: "Untitled",
filename: "Untitled",
date: Date().toISOString(),
date: new Date().toISOString(),
tables: tables,
relationships: relationships,
notes: notes,
@ -604,19 +605,18 @@ export default function ControlPanel(props) {
const reader = new FileReader();
reader.onload = function (event) {
if (f.type === "application/json") {
let jsonObject = null;
try {
jsonObject = JSON.parse(event.target.result);
} catch (error) {
setError({
type: ERROR.ERROR,
message: "Invalid JSON. The file contains an error.",
message: "The file contains an error.",
});
return;
}
if (!diagramObjectIsValid(jsonObject)) {
if (f.type === "application/json") {
if (!jsonDiagramIsValid(jsonObject)) {
setError({
type: ERROR.ERROR,
message:
@ -624,7 +624,16 @@ export default function ControlPanel(props) {
});
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);
if (diagramIsEmpty()) {
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.",
});
}
}
};
reader.readAsText(f);
@ -652,7 +660,7 @@ export default function ControlPanel(props) {
draggable={true}
dragMainText="Click to upload the file or drag and drop the file here"
dragSubText="Support json"
accept="application/json,.txt"
accept="application/json,.ddb"
onRemove={() =>
setError({
type: ERROR.NONE,

View File

@ -141,4 +141,15 @@ const jsonSchema = {
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 { jsonSchema } from "../schemas";
import { ddbSchema, jsonSchema } from "../schemas";
const enterFullscreen = () => {
const element = document.documentElement;
@ -26,8 +26,17 @@ const exitFullscreen = () => {
}
};
const diagramObjectIsValid = (obj) => {
const jsonDiagramIsValid = (obj) => {
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,
};