Added new save functions for when running in Node-Webkit.

This commit is contained in:
Lee Grey 2015-03-15 01:40:00 +13:00
parent 1c66282b01
commit fa6f2e5db6
3 changed files with 90 additions and 3 deletions

View file

@ -25,8 +25,26 @@
this.isPublicCheckbox = $('input[name=save-public-checkbox]');
this.isPublicCheckbox.prop('checked', descriptor.isPublic);
this.saveFileButton = $('#save-file-button');
this.saveFileButton.click(this.saveFile_.bind(this));
// prevent default behaviour of ctrl-s is there a way to do this
// with the pre-existing system? Should I use alt-s instead?
$(document).bind('keydown', 'ctrl+s', function(e) {
e.preventDefault();
return false;
});
//Environment dependent configuration:
if (pskl.utils.Environment.detectNodeWebkit()) {
// running in Node-Webkit...
pskl.app.shortcutService.addShortcut('ctrl+s', this.saveFileDesktop_.bind(this));
this.saveFileButton = $('#save-file-button');
this.saveFileButton.click(this.saveFileDesktop_.bind(this));
} else {
// running in browser...
pskl.app.shortcutService.addShortcut('ctrl+s', this.saveFileBrowser_.bind(this));
this.saveFileButton = $('#save-file-button');
this.saveFileButton.click(this.saveFileBrowser_.bind(this));
}
this.saveLocalButton = $('#save-browser-button');
this.saveLocalButton.click(this.saveLocal_.bind(this));
@ -122,7 +140,20 @@
}
};
/**
* @deprecated - renamed "saveFileBrowser_"
* @private
*/
ns.SaveController.prototype.saveFile_ = function () {
// detect if this is running in NodeWebkit
if (pskl.utils.Environment.detectNodeWebkit()) {
this.saveFileDesktop_();
} else {
this.saveFileBrowser_();
}
};
ns.SaveController.prototype.saveFileBrowser_ = function () {
this.beforeSaving_();
pskl.utils.BlobUtils.stringToBlob(pskl.app.piskelController.serialize(), function(blob) {
pskl.utils.FileUtils.downloadAsFile(blob, this.getLocalFilename_());
@ -131,6 +162,20 @@
}.bind(this), "application/piskel+json");
};
ns.SaveController.prototype.saveFileDesktop_ = function () {
this.beforeSaving_();
var serialized = pskl.app.piskelController.serialize();
// TODO: hold on to the full path to the file:
var fullSavePath = null;
// TODO: if we already have a filename (and we are sure this is the same document!!!)
// then save over the old file without opening a save dialog (using nodejs 'fs' api)
pskl.utils.FileUtils.desktopSaveAs(serialized, null, function (filename) {
this.onSaveSuccess_();
this.afterSaving_();
fullSavePath = filename;
}.bind(this));
}
ns.SaveController.prototype.getName = function () {
return this.nameInput.val();
};

View file

@ -37,6 +37,48 @@
downloadLink.removeEventListener('click', stopPropagation);
document.body.removeChild(downloadLink);
}
},
/**
*
* @param content
* @param defaultFileName
* @param callback
*/
desktopSaveAs: function (content, defaultFileName, callback) {
// NodeWebkit has no js api for opening the save dialog.
// Instead, it adds two new attributes to the anchor tag: nwdirectory and nwsaveas
// (see: https://github.com/nwjs/nw.js/wiki/File-dialogs )
defaultFileName = defaultFileName || "";
var tagString = '<input type="file" nwsaveas="'+ defaultFileName +'" nwworkingdir=""/>';
var $chooser = $(tagString);
$chooser.change(function(e) {
var filename = $(this).val();
pskl.utils.FileUtils.desktopSaveToFile(content, filename, function(){
callback(filename);
});
});
$chooser.trigger('click');
},
/**
* Save data directly to disk, without showing a save dialog
* Requires Node-Webkit environment for file system access
* @param content - data to be saved
* @param {string} filename - fill path to the file
* @callback callback
*/
desktopSaveToFile: function(content, filename, callback) {
var fs = require('fs');
//var arrayBuffer = ns.FileUtils.toArrayBuffer(content);
fs.writeFile(filename, content, function(err){
if (err) {
//throw err;
console.log('destopSavetoFile() - error saving file', filename, 'Error:', err);
}
callback();
});
}
};
})();

View file

@ -18,11 +18,11 @@
"js/utils/DateUtils.js",
"js/utils/Dom.js",
"js/utils/Event.js",
"js/utils/Environment.js",
"js/utils/Math.js",
"js/utils/FileUtils.js",
"js/utils/FrameTransform.js",
"js/utils/FrameUtils.js",
"js/utils/Environment.js",
"js/utils/LayerUtils.js",
"js/utils/ImageResizer.js",
"js/utils/PixelUtils.js",