1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-24 02:27:19 +00:00
TiddlyWiki5/core/modules/savers/twedit.js
Jermolene 0956ae10a0 Add support for downloading files
We were re-using the `tw-save-wiki` message both for saving the current
wiki and downloading a new wiki. Now we’ll use the separate
`tw-download-file` message for downloading.

Fixes #236
2013-11-27 20:51:08 +00:00

95 lines
2.3 KiB
JavaScript

/*\
title: $:/core/modules/savers/twedit.js
type: application/javascript
module-type: saver
Handles saving changes via the TWEdit iOS app
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false, netscape: false, Components: false */
"use strict";
var TWEditSaver = function(wiki) {
};
TWEditSaver.prototype.save = function(text,method,callback) {
// Bail out unless this is a save (rather than a download)
if(method !== "save") {
return false;
}
// Bail if we're not running under TWEdit
if(typeof DeviceInfo !== "object") {
return false;
}
// Get the pathname of this document
var pathname = decodeURIComponent(document.location.pathname);
// Strip any query or location part
var p = pathname.indexOf("?");
if(p !== -1) {
pathname = pathname.substr(0,p);
}
p = pathname.indexOf("#");
if(p !== -1) {
pathname = pathname.substr(0,p);
}
// Remove the leading "/Documents" from path
var prefix = "/Documents";
if(pathname.indexOf(prefix) === 0) {
pathname = pathname.substr(prefix.length);
}
// Error handler
var errorHandler = function(event) {
// Error
callback("Error saving to TWEdit: " + event.target.error.code);
};
// Get the file system
window.requestFileSystem(LocalFileSystem.PERSISTENT,0,function(fileSystem) {
// Now we've got the filesystem, get the fileEntry
fileSystem.root.getFile(pathname, {create: true}, function(fileEntry) {
// Now we've got the fileEntry, create the writer
fileEntry.createWriter(function(writer) {
writer.onerror = errorHandler;
writer.onwrite = function() {
callback(null);
};
writer.position = 0;
writer.write(text);
},errorHandler);
}, errorHandler);
}, errorHandler);
return true;
};
/*
Information about this saver
*/
TWEditSaver.prototype.info = {
name: "twedit",
priority: 1600
};
/*
Static method that returns true if this saver is capable of working
*/
exports.canSave = function(wiki) {
return true;
};
/*
Create an instance of this saver
*/
exports.create = function(wiki) {
return new TWEditSaver(wiki);
};
/////////////////////////// Hack
// HACK: This ensures that TWEdit recognises us as a TiddlyWiki document
if($tw.browser) {
window.version = {title: "TiddlyWiki"};
}
})();