Skip to content

Commit

Permalink
Merge pull request #163 from 2xAA/next
Browse files Browse the repository at this point in the history
feat(editor-state): adds load and save for editor state (#162)
  • Loading branch information
2xAA authored Apr 16, 2023
2 parents 278867f + ae3a397 commit 8db5c83
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 120 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"main": "background.js",
"dependencies": {
"compress-json": "^2.1.2",
"genmdm-parser": "1.0.2",
"vue": "^2.6.11",
"vue-flexible-link": "^1.0.2",
Expand Down
12 changes: 11 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@
Reset State
</button>
</c>
<c>
<StateUpload />
</c>
<c>
<StateDownload />
</c>
</grid>
</c>
<c span="10">
Expand Down Expand Up @@ -319,6 +325,8 @@ import Dialog from "./components/Dialog";
import Y12FileUpload from "./components/Y12FileUpload.vue";
import Y12FileDownload from "./components/Y12FileDownload.vue";
import ResetStateDialog from "./components/ResetStateDialog.vue";
import StateUpload from "./components/StateUpload.vue";
import StateDownload from "./components/StateDownload.vue";
export default {
name: "App",
Expand All @@ -344,7 +352,9 @@ export default {
Dialog,
Y12FileUpload,
Y12FileDownload,
ResetStateDialog
ResetStateDialog,
StateUpload,
StateDownload
},
data() {
Expand Down
30 changes: 2 additions & 28 deletions src/components/DMPFileDownload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<script>
import { GenMDMParser } from "genmdm-parser/dist/main.js";
import { saveFile } from "../utils/save-file";
export default {
methods: {
Expand All @@ -21,34 +22,7 @@ export default {
const instrument = parser.parseGenMDM(map);
const dmpData = instrument.toDMP();
var downloadBlob, downloadURL;
downloadBlob = function(data, fileName, mimeType) {
var blob, url;
blob = new Blob([data], {
type: mimeType
});
url = window.URL.createObjectURL(blob);
downloadURL(url, fileName);
setTimeout(function() {
return window.URL.revokeObjectURL(url);
}, 1000);
};
downloadURL = function(data, fileName) {
var a;
a = document.createElement("a");
a.href = data;
a.download = fileName;
document.body.appendChild(a);
a.style = "display: none";
a.click();
a.remove();
};
downloadBlob(dmpData, "genmdm-patch.dmp", "application/octet-stream");
console.log(dmpData);
saveFile("genmdm-patch.dmp", dmpData);
}
}
};
Expand Down
30 changes: 2 additions & 28 deletions src/components/GENMFileDownload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<script>
import { GenMDMParser } from "genmdm-parser/dist/main.js";
import { saveFile } from "../utils/save-file";
export default {
methods: {
Expand Down Expand Up @@ -37,34 +38,7 @@ export default {
return;
}
var downloadBlob, downloadURL;
downloadBlob = function(data, fileName, mimeType) {
var blob, url;
blob = new Blob([data], {
type: mimeType
});
url = window.URL.createObjectURL(blob);
downloadURL(url, fileName);
setTimeout(function() {
return window.URL.revokeObjectURL(url);
}, 1000);
};
downloadURL = function(data, fileName) {
var a;
a = document.createElement("a");
a.href = data;
a.download = fileName;
document.body.appendChild(a);
a.style = "display: none";
a.click();
a.remove();
};
downloadBlob(genmString, "genmdm-bank.genm", "application/octet-stream");
console.log(genmString);
saveFile("genmdm-bank.genm", genmString);
}
}
};
Expand Down
14 changes: 7 additions & 7 deletions src/components/ResetStateDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<grid columns="3" class="reset-state-dialog-text">
<c span="3"><h2>Reset State</h2></c>
<c span="3">
<grid columns="2" class="control-group">
<c class="control-group__label">Channel</c>
<c class="control-group__control">
<grid columns="8" class="control-group">
<c span="6" class="control-group__label">Channel</c>
<c span="2" class="control-group__control">
<DraggableSelect
:values="[0, 1, 2, 3, 4, 5, 6, 7]"
:emitArrayValue="true"
Expand All @@ -19,8 +19,8 @@
v-model="channel"
/>
</c>
<c class="control-group__label">Reset Editor Settings</c>
<c class="control-group__control">
<c span="6" class="control-group__label">Reset Editor Settings</c>
<c span="2" class="control-group__control">
<DraggableSelect
:values="[0, 1]"
:emitArrayValue="true"
Expand All @@ -29,8 +29,8 @@
v-model="resetEditor"
/>
</c>
<c class="control-group__label">Reset Patches</c>
<c class="control-group__control">
<c span="6" class="control-group__label">Reset Patches</c>
<c span="2" class="control-group__control">
<DraggableSelect
:values="[0, 1]"
:emitArrayValue="true"
Expand Down
17 changes: 17 additions & 0 deletions src/components/StateDownload.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<template>
<button class="button" @click="saveState">Save State</button>
</template>

<script>
import { compress } from "compress-json";
import { saveFile } from "../utils/save-file";
export default {
methods: {
saveState() {
const compressed = compress(this.$store.state);
saveFile("state.ged", JSON.stringify(compressed));
}
}
};
</script>
42 changes: 42 additions & 0 deletions src/components/StateUpload.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<template>
<label class="button">
<input type="file" id="input" accept=".ged" @change="fileAdded" />
</label>
</template>

<script>
import { decompress } from "compress-json";
export default {
methods: {
fileAdded(e) {
const {
files: { 0: file }
} = e.target;
const reader = new FileReader();
reader.onload = () => {
const newState = decompress(JSON.parse(reader.result));
this.$store.commit("SET_STATE", newState);
e.target.value = "";
};
try {
reader.readAsText(file);
} catch (e) {
console.log("Can't read file", e);
}
}
}
};
</script>

<style scoped>
input {
display: none;
}
label::before {
content: "Load State";
}
</style>
30 changes: 2 additions & 28 deletions src/components/TFIFileDownload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<script>
import { GenMDMParser } from "genmdm-parser/dist/main.js";
import { saveFile } from "../utils/save-file";
export default {
methods: {
Expand All @@ -21,34 +22,7 @@ export default {
const instrument = parser.parseGenMDM(map);
const tfiData = instrument.toTFI();
var downloadBlob, downloadURL;
downloadBlob = function(data, fileName, mimeType) {
var blob, url;
blob = new Blob([data], {
type: mimeType
});
url = window.URL.createObjectURL(blob);
downloadURL(url, fileName);
setTimeout(function() {
return window.URL.revokeObjectURL(url);
}, 1000);
};
downloadURL = function(data, fileName) {
var a;
a = document.createElement("a");
a.href = data;
a.download = fileName;
document.body.appendChild(a);
a.style = "display: none";
a.click();
a.remove();
};
downloadBlob(tfiData, "genmdm-patch.tfi", "application/octet-stream");
console.log(tfiData);
saveFile("genmdm-patch.tfi", tfiData);
}
}
};
Expand Down
30 changes: 2 additions & 28 deletions src/components/Y12FileDownload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<script>
import { GenMDMParser } from "genmdm-parser/dist/main.js";
import { saveFile } from "../utils/save-file";
export default {
methods: {
Expand All @@ -22,34 +23,7 @@ export default {
instrument.instrumentName = "genmdm-patch";
const y12data = instrument.toY12();
var downloadBlob, downloadURL;
downloadBlob = function(data, fileName, mimeType) {
var blob, url;
blob = new Blob([data], {
type: mimeType
});
url = window.URL.createObjectURL(blob);
downloadURL(url, fileName);
setTimeout(function() {
return window.URL.revokeObjectURL(url);
}, 1000);
};
downloadURL = function(data, fileName) {
var a;
a = document.createElement("a");
a.href = data;
a.download = fileName;
document.body.appendChild(a);
a.style = "display: none";
a.click();
a.remove();
};
downloadBlob(y12data, "genmdm-patch.y12", "application/octet-stream");
console.log(y12data);
saveFile("genmdm-patch.y12", y12data);
}
}
};
Expand Down
25 changes: 25 additions & 0 deletions src/utils/save-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const downloadURL = function(data, fileName) {
const a = document.createElement("a");
a.href = data;
a.download = fileName;
document.body.appendChild(a);
a.style = "display: none";
a.click();
a.remove();
};

export function saveFile(name, contents = "") {
if (!name) {
throw new Error("Name can't be blank");
}

const blob = new Blob([contents], {
type: "application/octet-stream"
});

const url = window.URL.createObjectURL(blob);
downloadURL(url, name);
setTimeout(function() {
return window.URL.revokeObjectURL(url);
}, 1000);
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2763,6 +2763,11 @@ component-emitter@^1.2.1:
resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.3.0.tgz#16e4070fba8ae29b679f2215853ee181ab2eabc0"
integrity sha512-Rd3se6QB+sO1TwqZjscQrurpEPIfO0/yYnSin6Q/rD3mOutHvUrCAhJub3r90uNb+SESBuE0QYoB90YdfatsRg==

compress-json@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/compress-json/-/compress-json-2.1.2.tgz#37e0e7c7480c572fad9ad387fca5a2f36fee6f83"
integrity sha512-91247RD8bKQXzRmXUS4zGT250mhw86+J9X8w2L2SGtRE7g0CvzjOETFaFmsDdaXPWv8T7L9iiM7kdcnnH3BH7w==

compressible@~2.0.16:
version "2.0.18"
resolved "https://registry.yarnpkg.com/compressible/-/compressible-2.0.18.tgz#af53cca6b070d4c3c0750fbd77286a6d7cc46fba"
Expand Down

0 comments on commit 8db5c83

Please sign in to comment.