From d6e248aab82347953bca0fcceb0879219228946d Mon Sep 17 00:00:00 2001 From: Karishma Chadha Date: Mon, 5 Mar 2018 17:20:36 -0500 Subject: [PATCH] VM should do the heavy lifting of saving project to a zip file, and return a promise of the zip to gui. --- src/virtual-machine.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/virtual-machine.js b/src/virtual-machine.js index 6d97dfe46..07aa159b4 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -1,5 +1,6 @@ const TextEncoder = require('text-encoding').TextEncoder; const EventEmitter = require('events'); +const JSZip = require('jszip'); const centralDispatch = require('./dispatch/central-dispatch'); const ExtensionManager = require('./extension-support/extension-manager'); @@ -227,15 +228,25 @@ class VirtualMachine extends EventEmitter { * @returns {string} Project in a Scratch 3.0 JSON representation. */ saveProjectSb3 () { - // @todo: Handle other formats, e.g., Scratch 1.4, Scratch 2.0. const soundDescs = serializeSounds(this.runtime); const costumeDescs = serializeCostumes(this.runtime); + const projectJson = this.toJSON(); - return { - projectJson: this.toJSON(), - sounds: soundDescs, - costumes: costumeDescs - }; + const zip = new JSZip(); + + // Put everything in a zip file + // TODO compression? + zip.file('project.json', projectJson); + for (let i = 0; i < soundDescs.length; i++) { + const currSound = soundDescs[i]; + zip.file(currSound.fileName, currSound.fileContent); + } + for (let i = 0; i < costumeDescs.length; i++) { + const currCostume = costumeDescs[i]; + zip.file(currCostume.fileName, currCostume.fileContent); + } + + return zip.generateAsync({type: 'blob'}); } /**