1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-01 07:36:18 +00:00
TiddlyWiki5/core/modules/savers/download.js
Jermolene b520efdeb8 Update savers to specify variables, including filename
Now the `tm-download-file` and `tm-save-file` messages use the hashmap
to specify variables to be applied when rendering the tiddler.

We also add a convention that the variable “filename” is used to
specify a filename for the download.
2014-11-10 19:54:19 +00:00

75 lines
1.6 KiB
JavaScript

/*\
title: $:/core/modules/savers/download.js
type: application/javascript
module-type: saver
Handles saving changes via HTML5's download APIs
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Select the appropriate saver module and set it up
*/
var DownloadSaver = function(wiki) {
};
DownloadSaver.prototype.save = function(text,method,callback,options) {
options = options || {};
// Get the current filename
var filename = options.variables.filename;
if(!filename) {
var p = document.location.pathname.lastIndexOf("/");
if(p !== -1) {
filename = document.location.pathname.substr(p+1);
} else {
filename = "tiddlywiki.html";
}
}
// Set up the link
var link = document.createElement("a");
link.setAttribute("target","_blank");
if(Blob !== undefined) {
var blob = new Blob([text], {type: "text/html"});
link.setAttribute("href", URL.createObjectURL(blob));
} else {
link.setAttribute("href","data:text/html," + encodeURIComponent(text));
}
link.setAttribute("download",filename);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
// Callback that we succeeded
callback(null);
return true;
};
/*
Information about this saver
*/
DownloadSaver.prototype.info = {
name: "download",
priority: 100,
capabilities: ["save", "download"]
};
/*
Static method that returns true if this saver is capable of working
*/
exports.canSave = function(wiki) {
return document.createElement("a").download !== undefined;
};
/*
Create an instance of this saver
*/
exports.create = function(wiki) {
return new DownloadSaver(wiki);
};
})();