Skip to content

Commit

Permalink
Merge branch 'master' into 267-switching-theme-swaps-to-draw-mode-but…
Browse files Browse the repository at this point in the history
…-says-youre-in-proof-mode
  • Loading branch information
RyanR712 authored Nov 28, 2023
2 parents 5796b2c + 6e61cab commit d2b7521
Showing 1 changed file with 57 additions and 48 deletions.
105 changes: 57 additions & 48 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,44 @@ async function saveMode() {
data = treeContext.proofHistory;
}

//Slow Download
if ("showSaveFilePicker" in window) {
const saveHandle = await window.showSaveFilePicker({
//Errors caused due to file handler or html download element should not be displayed
try {
//Slow Download
if ("showSaveFilePicker" in window) {
const saveHandle = await window.showSaveFilePicker({
excludeAcceptAllOption: true,
suggestedName: name,
startIn: "downloads",
types: [
{
description: "JSON Files",
accept: {
"text/json": [".json"],
},
},
],
});
saveFile(saveHandle, data);
} else {
//Quick Download
const f = document.createElement("a");
f.href = JSON.stringify(data, null, "\t");
f.download = name + ".json";
f.click();
}
} catch (error) {
//Catch error but do nothing
}
}

/**
* Calls the function to load the files.
*/
async function loadMode() {
try {
const [fileHandle] = await window.showOpenFilePicker({
excludeAcceptAllOption: true,
suggestedName: name,
multiple: false,
startIn: "downloads",
types: [
{
Expand All @@ -313,52 +346,28 @@ async function saveMode() {
},
],
});
saveFile(saveHandle, data);
} else {
//Quick Download
const f = document.createElement("a");
f.href = JSON.stringify(data, null, "\t");
f.download = name + ".json";
f.click();
}
}

/**
* Calls the function to load the files.
*/
async function loadMode() {
const [fileHandle] = await window.showOpenFilePicker({
excludeAcceptAllOption: true,
multiple: false,
startIn: "downloads",
types: [
{
description: "JSON Files",
accept: {
"text/json": [".json"],
},
},
],
});

const file = await fileHandle.getFile();
const reader = new FileReader();
reader.addEventListener("load", () => {
const aegData = reader.result;
if (typeof aegData === "string") {
const loadData = loadFile(treeContext.modeState, aegData);
if (treeContext.modeState === "Draw") {
treeContext.tree = loadData as AEGTree;
redrawTree(treeContext.tree);
} else if (treeContext.modeState === "Proof") {
treeContext.proofHistory = loadData as ProofNode[];
redrawProof();
const file = await fileHandle.getFile();
const reader = new FileReader();
reader.addEventListener("load", () => {
const aegData = reader.result;
if (typeof aegData === "string") {
const loadData = loadFile(treeContext.modeState, aegData);
if (treeContext.modeState === "Draw") {
treeContext.tree = loadData as AEGTree;
redrawTree(treeContext.tree);
} else if (treeContext.modeState === "Proof") {
treeContext.proofHistory = loadData as ProofNode[];
redrawProof();
}
} else {
console.log("Loading failed because reading the file was unsuccessful");
}
} else {
throw Error("Loading failed because reading the file was unsuccessful");
}
});
reader.readAsText(file);
});
reader.readAsText(file);
} catch (error) {
//Do nothing
}
}

/**
Expand Down

0 comments on commit d2b7521

Please sign in to comment.