Skip to content

Commit

Permalink
add download methods to save manager
Browse files Browse the repository at this point in the history
  • Loading branch information
argarak committed Dec 13, 2023
1 parent b8b4cfc commit 3a92500
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import * as basicPatch from "/objects/patches/basic.json";
import * as basicSynthPatch from "/objects/patches/basic-synth.json";
import localforage from "localforage";

import SaveManager from "./save-manager";

document.addEventListener("DOMContentLoaded", () => {
let btnPlay = document.getElementById("btnPlay");
btnPlay.addEventListener("click", () => {
Expand Down
20 changes: 20 additions & 0 deletions save-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ import localforage from "localforage";

class SaveManager {
constructor() {}

static downloadObject(obj, filename) {
let blob = new Blob([JSON.stringify(obj, null, 2)], {
type: "application/json",
});
this.download(blob, filename);
}

static download(blob, filename) {
const url = window.URL.createObjectURL(blob);
const anchor = document.createElement("a");
anchor.style.display = "none";
anchor.href = url;
anchor.download = filename;
document.body.appendChild(anchor);

anchor.click();
window.URL.revokeObjectURL(url);
anchor.remove();
}
}

export default SaveManager;

0 comments on commit 3a92500

Please sign in to comment.