1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 18:17:20 +00:00
TiddlyWiki5/core/modules/savers/twedit.js
2013-05-14 20:20:48 +01:00

91 lines
2.2 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,callback) {
// 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"};
}
})();