From d210441840f38cd4e506f2ea6037ac8761b1c8c4 Mon Sep 17 00:00:00 2001 From: Colton Mercurio Date: Tue, 19 Apr 2016 23:07:48 -0500 Subject: [PATCH 1/2] files and directories are now downloadable --- bower.json | 3 +- src/app/require.config.js | 1 + src/components/editor/editor.html | 7 +-- src/components/editor/editor.js | 10 +--- src/components/file-browser/file-browser.js | 58 +++++++++++++++++---- 5 files changed, 54 insertions(+), 25 deletions(-) diff --git a/bower.json b/bower.json index df24b1e2..25c6fa84 100644 --- a/bower.json +++ b/bower.json @@ -28,7 +28,8 @@ "browserfs": "0.5.8", "bootstrap-contextmenu": "^0.3.4", "bootbox.js": "^4.4.0", - "github-api": "~0.10.7" + "github-api": "~0.10.7", + "Stuk/jszip": "3.0.0" }, "devDependencies": {}, "resolutions": { diff --git a/src/app/require.config.js b/src/app/require.config.js index a866dcc0..2f5051e6 100644 --- a/src/app/require.config.js +++ b/src/app/require.config.js @@ -32,6 +32,7 @@ require.config({ "bootstrap-contextmenu":"bower_modules/bootstrap-contextmenu/bootstrap-contextmenu", "bootbox": "bower_modules/bootbox.js/bootbox", "github-api": "bower_modules/github-api/github", + "jszip": "bower_modules/jszip/dist/jszip.min", // Application-specific modules "app/config": "app/config/config.dev", // overridden to 'config.dist' in build config diff --git a/src/components/editor/editor.html b/src/components/editor/editor.html index dd6a04ca..3783da8b 100644 --- a/src/components/editor/editor.html +++ b/src/components/editor/editor.html @@ -9,7 +9,7 @@ -
+
@@ -19,11 +19,6 @@ Indent code
-
- -
diff --git a/src/components/editor/editor.js b/src/components/editor/editor.js index 4f786772..d0e8a759 100644 --- a/src/components/editor/editor.js +++ b/src/components/editor/editor.js @@ -1,11 +1,9 @@ -/*global Bloodhound:false, saveAs:false*/ +/*global Bloodhound:false */ import ko from 'knockout'; import templateMarkup from 'text!./editor.html'; import ace from 'ace/ace'; import 'bloodhound'; import TokenHighlighter from 'components/editor/token-highlighter'; -import 'Blob'; -import 'FileSaver'; import * as SysGlobalObservables from 'app/sys-global-observables'; class Editor { @@ -43,12 +41,6 @@ class Editor { e.stopPropagation(); }); - $('#download-file-btn').click(() => { - var text = this.getText(); - var blob = new Blob([text]); - saveAs(blob, SysGlobalObservables.currentFileName()); - }); - $('#autoindent-code-btn').click(() => { this.autoIndentCode(); }); diff --git a/src/components/file-browser/file-browser.js b/src/components/file-browser/file-browser.js index ef772ff2..1891da32 100644 --- a/src/components/file-browser/file-browser.js +++ b/src/components/file-browser/file-browser.js @@ -7,7 +7,10 @@ import SysRuntime from 'app/sys-runtime'; import SysFileSystem from 'app/sys-filesystem'; import bootbox from 'bootbox'; import 'bootstrap-contextmenu'; +import 'Blob'; +import 'FileSaver'; import * as SysGlobalObservables from 'app/sys-global-observables'; +import JSZip from 'jszip'; // notification options var warningNotific8Options = { @@ -110,7 +113,11 @@ class Filebrowser { } menuHtml += '
  • Rename
  • '; menuHtml += '
  • Delete
  • '; + if (!self.metaData[itemId].isDirectory) { + menuHtml += '
  • Download \''+ self.metaData[itemId].name +'\'
  • '; + } if (self.metaData[itemId].isDirectory) { + menuHtml += '
  • Download \''+ self.metaData[itemId].name +'\' as .zip
  • '; menuHtml += '
  • Clone a repo into \'' + self.metaData[itemId].name + '\'...
  • '; menuHtml += '
  • Push \''+ self.metaData[itemId].name + '\' to a repo...
  • '; } @@ -139,17 +146,21 @@ class Filebrowser { }); } else if (action === 'rename') { - bootbox.prompt('Insert new name', function (result) { - if (result === null || result.trim().length === 0) { - // do nothing - } - else { - index = itemPath.indexOf(itemName); - if (index === -1) { - return; + bootbox.prompt({ + title: 'Insert a new name for \'' + itemName + '\'', + value: itemName, + callback: function (result) { + if (result === null || result.trim().length === 0) { + // do nothing } else { - self.fs.rename(itemPath, itemPath.slice(0, index) + result); + index = itemPath.indexOf(itemName); + if (index === -1) { + return; + } + else { + self.fs.rename(itemPath, itemPath.slice(0, index) + result); + } } } }); @@ -180,6 +191,35 @@ class Filebrowser { } }); } + else if (action === 'downloadFile') { + var text = self.fs.readFileSync(itemPath).toString('binary'); + var blob = new Blob([text]); + saveAs(blob, itemName); + } + else if (action === 'downloadDir') { + var zip = new JSZip(); + + var addChildren = function(zipNode, itemPath) { + var children = self.fs.getDirectoryChildren(itemPath); + + for(var i = 0; i < children.length; i++) { + if(children[i].isDirectory){ + var fol = zipNode.folder(children[i].name); + addChildren(fol, itemPath + '/' + children[i].name); + } + else{ + var fileData = self.fs.readFileSync(itemPath + '/' + children[i].name).toString('binary'); + zipNode.file(children[i].name, fileData); + } + } + } + + addChildren(zip, itemPath); + + zip.generateAsync({type:'blob'}).then(function(content) { + saveAs(content, itemName + '.zip'); + }); + } else if (action === 'clone') { bootbox.dialog({ title: 'Clone a GitHub repo into \'' + itemName + '\'...', From 9de5b155ba60d130d6e231777a5f33015885b3ab Mon Sep 17 00:00:00 2001 From: Colton Mercurio Date: Tue, 19 Apr 2016 23:15:19 -0500 Subject: [PATCH 2/2] mark saveAs as non-global --- src/components/file-browser/file-browser.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/file-browser/file-browser.js b/src/components/file-browser/file-browser.js index 1891da32..6e92c751 100644 --- a/src/components/file-browser/file-browser.js +++ b/src/components/file-browser/file-browser.js @@ -1,4 +1,4 @@ -/* global Buffer */ +/*global Buffer, saveAs:false*/ import ko from 'knockout'; import templateMarkup from 'text!./file-browser.html'; import 'knockout-projections'; @@ -212,7 +212,7 @@ class Filebrowser { zipNode.file(children[i].name, fileData); } } - } + }; addChildren(zip, itemPath);