VM should do the heavy lifting of saving project to a zip file, and return a promise of the zip to gui.

This commit is contained in:
Karishma Chadha 2018-03-05 17:20:36 -05:00
parent c0afcd2ab3
commit d6e248aab8

View file

@ -1,5 +1,6 @@
const TextEncoder = require('text-encoding').TextEncoder; const TextEncoder = require('text-encoding').TextEncoder;
const EventEmitter = require('events'); const EventEmitter = require('events');
const JSZip = require('jszip');
const centralDispatch = require('./dispatch/central-dispatch'); const centralDispatch = require('./dispatch/central-dispatch');
const ExtensionManager = require('./extension-support/extension-manager'); 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. * @returns {string} Project in a Scratch 3.0 JSON representation.
*/ */
saveProjectSb3 () { saveProjectSb3 () {
// @todo: Handle other formats, e.g., Scratch 1.4, Scratch 2.0.
const soundDescs = serializeSounds(this.runtime); const soundDescs = serializeSounds(this.runtime);
const costumeDescs = serializeCostumes(this.runtime); const costumeDescs = serializeCostumes(this.runtime);
const projectJson = this.toJSON();
return { const zip = new JSZip();
projectJson: this.toJSON(),
sounds: soundDescs, // Put everything in a zip file
costumes: costumeDescs // 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'});
} }
/** /**