File size: 671 Bytes
e3e0a75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// zipExport.js
import JSZip from "jszip";
import { loadTree } from "./fileStore";

export async function downloadProjectZip() {
  const tree = loadTree();
  const zip = new JSZip();

  function addToZip(node, folder) {
    if (node.type === "file") {
      folder.file(node.name, node.content || "");
    } else if (node.type === "folder") {
      const newFolder = folder.folder(node.name);
      node.children?.forEach((c) => addToZip(c, newFolder));
    }
  }

  addToZip(tree, zip);
  const blob = await zip.generateAsync({ type: "blob" });

  const a = document.createElement("a");
  a.href = URL.createObjectURL(blob);
  a.download = "project.zip";
  a.click();
}