45 lines
1.3 KiB
JavaScript
45 lines
1.3 KiB
JavaScript
import { awaitEvent, emitEvent } from "./events.js";
|
|
import { clearPlanner } from "./planner.js";
|
|
|
|
export function showOpenFileDialog() {
|
|
const input = document.createElement("input");
|
|
input.type = "file";
|
|
input.accept = ".ncpf.json";
|
|
input.addEventListener("change", async () => {
|
|
const data = await new Promise(resolve => {
|
|
const reader = new FileReader();
|
|
reader.addEventListener("load", () => {
|
|
resolve(JSON.parse(reader.result));
|
|
});
|
|
reader.readAsText(input.files[0]);
|
|
});
|
|
void loadFile(data);
|
|
});
|
|
input.click();
|
|
}
|
|
|
|
export async function loadExampleFile(filename) {
|
|
const data = await fetch(`./examples/${filename}.ncpf.json`)
|
|
.then(data => data.json());
|
|
return loadFile(data);
|
|
}
|
|
|
|
export async function loadFile(data) {
|
|
if (!data.designs) throw new Error("invalid file");
|
|
if (data.designs.length === 0) throw new Error("no designs");
|
|
|
|
let design = data.designs[0];
|
|
if (data.designs.length > 1) {
|
|
const prepChoice = awaitEvent("fileLoad:chooseDesign:choice");
|
|
emitEvent("fileLoad:chooseDesign", data);
|
|
const res = await prepChoice;
|
|
design = data.designs[res.detail];
|
|
}
|
|
|
|
if (!design) throw new Error("no design");
|
|
|
|
clearPlanner(design.dimensions, design.type);
|
|
console.log(data);
|
|
console.log(design);
|
|
}
|