Skip to content
Snippets Groups Projects
Commit 8641ae2a authored by Karishma Chadha's avatar Karishma Chadha
Browse files

Update save button to download zip of project json + serialized assets (costumes and sounds).

parent 3b01b354
No related merge requests found
...@@ -2,6 +2,7 @@ import bindAll from 'lodash.bindall'; ...@@ -2,6 +2,7 @@ import bindAll from 'lodash.bindall';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import React from 'react'; import React from 'react';
import {connect} from 'react-redux'; import {connect} from 'react-redux';
import JSZip from 'jszip';
import ButtonComponent from '../components/button/button.jsx'; import ButtonComponent from '../components/button/button.jsx';
import {ComingSoonTooltip} from '../components/coming-soon/coming-soon.jsx'; import {ComingSoonTooltip} from '../components/coming-soon/coming-soon.jsx';
...@@ -15,24 +16,44 @@ class SaveButton extends React.Component { ...@@ -15,24 +16,44 @@ class SaveButton extends React.Component {
]); ]);
} }
handleClick () { handleClick () {
const json = this.props.saveProjectSb3(); const zip = new JSZip();
// Get the serialized project and assets from vm
const projectInfo = this.props.saveProjectSb3();
const json = projectInfo.projectJson;
const sounds = projectInfo.sounds;
const costumes = projectInfo.costumes;
// Put everything in a zip file
zip.file('project.json', json);
for (let i = 0; i < sounds.length; i++) {
const currSound = sounds[i];
zip.file(currSound.fileName, currSound.fileContent);
}
for (let i = 0; i < costumes.length; i++) {
const currCostume = costumes[i];
zip.file(currCostume.fileName, currCostume.fileContent);
}
// Download project data into a file - create link element, // Download project data into a file - create link element,
// simulate click on it, and then remove it. // simulate click on it, and then remove it.
const saveLink = document.createElement('a'); const saveLink = document.createElement('a');
document.body.appendChild(saveLink); document.body.appendChild(saveLink);
const data = new Blob([json], {type: 'text'}); zip.generateAsync({type: 'blob'}).then(content => {
const url = window.URL.createObjectURL(data); const url = window.URL.createObjectURL(content);
saveLink.href = url;
saveLink.href = url;
// File name: project-DATE-TIME // TODO Project name/location should be chosen by user
const date = new Date(); // File name: project-DATE-TIME
const timestamp = `${date.toLocaleDateString()}-${date.toLocaleTimeString()}`; const date = new Date();
saveLink.download = `project-${timestamp}.json`; const timestamp = `${date.toLocaleDateString()}-${date.toLocaleTimeString()}`;
saveLink.click(); saveLink.download = `project-${timestamp}.zip`;
window.URL.revokeObjectURL(url); saveLink.click();
document.body.removeChild(saveLink); window.URL.revokeObjectURL(url);
document.body.removeChild(saveLink);
});
} }
render () { render () {
const { const {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment