From 2affe457d2466bfc4363d7a141378e9e8eef9366 Mon Sep 17 00:00:00 2001 From: catarak Date: Wed, 20 Jul 2016 15:33:37 -0400 Subject: [PATCH] when exporting to zip, also add images --- client/modules/IDE/actions/files.js | 3 ++- client/modules/IDE/actions/project.js | 24 ++++++++++++++++++------ client/modules/IDE/reducers/files.js | 2 +- package.json | 2 ++ 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/client/modules/IDE/actions/files.js b/client/modules/IDE/actions/files.js index fa9ceef4..a918e7a6 100644 --- a/client/modules/IDE/actions/files.js +++ b/client/modules/IDE/actions/files.js @@ -64,7 +64,8 @@ export function createFile(formProps) { dispatch({ type: ActionTypes.CREATE_FILE, name: formProps.name, - id: `${maxFileId + 1}` + id: `${maxFileId + 1}`, + url: formProps.url }); dispatch({ type: ActionTypes.HIDE_MODAL diff --git a/client/modules/IDE/actions/project.js b/client/modules/IDE/actions/project.js index 1ea6cc33..d44cf12a 100644 --- a/client/modules/IDE/actions/project.js +++ b/client/modules/IDE/actions/project.js @@ -2,8 +2,10 @@ import * as ActionTypes from '../../../constants'; import { browserHistory } from 'react-router'; import axios from 'axios'; import JSZip from 'jszip'; +import JSZipUtils from 'jszip-utils'; import { saveAs } from 'file-saver'; import { getBlobUrl } from './files'; +import async from 'async'; const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api'; @@ -117,12 +119,22 @@ export function exportProjectAsZip() { console.log('exporting project!'); const state = getState(); const zip = new JSZip(); - state.files.forEach(file => { - zip.file(file.name, file.content); - }); - - zip.generateAsync({ type: 'blob' }).then((content) => { - saveAs(content, `${state.project.name}.zip`); + async.each(state.files, (file, cb) => { + console.log(file); + if (file.url) { + JSZipUtils.getBinaryContent(file.url, (err, data) => { + zip.file(file.name, data, { binary: true }); + cb(); + }); + } else { + zip.file(file.name, file.content); + cb(); + } + }, err => { + if (err) console.log(err); + zip.generateAsync({ type: 'blob' }).then((content) => { + saveAs(content, `${state.project.name}.zip`); + }); }); }; } diff --git a/client/modules/IDE/reducers/files.js b/client/modules/IDE/reducers/files.js index 4849df88..c8568e8e 100644 --- a/client/modules/IDE/reducers/files.js +++ b/client/modules/IDE/reducers/files.js @@ -72,7 +72,7 @@ const files = (state = initialState, action) => { case ActionTypes.SET_PROJECT: return [...action.files]; case ActionTypes.CREATE_FILE: - return [...state, { name: action.name, id: action.id, content: '' }]; + return [...state, { name: action.name, id: action.id, content: '', url: action.url }]; default: return state; } diff --git a/package.json b/package.json index b0582a2f..211ea9ed 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "node": ">=4" }, "dependencies": { + "async": "^2.0.0", "axios": "^0.12.0", "babel-core": "^6.8.0", "bcrypt-nodejs": "0.0.3", @@ -77,6 +78,7 @@ "htmlhint": "^0.9.13", "jshint": "^2.9.2", "jszip": "^3.0.0", + "jszip-utils": "0.0.2", "moment": "^2.14.1", "mongoose": "^4.4.16", "node-uuid": "^1.4.7",