From 1d685df9286f91db805d1888c353ca2012b60c97 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Tue, 4 Feb 2014 21:21:01 +0000 Subject: [PATCH] Add support for autosave MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Causes the wiki to be autosaved whenever clicking “done” after editing a tiddler. Only works with savers that support autosave. We should probably make autosave configurable --- core/modules/savers/andtidwiki.js | 7 ++--- core/modules/savers/download.js | 3 ++- core/modules/savers/fsosaver.js | 7 ++--- core/modules/savers/manualdownload.js | 3 ++- core/modules/savers/msdownload.js | 3 ++- core/modules/savers/tiddlyfox.js | 7 ++--- core/modules/savers/tiddlyie.js | 9 +++---- core/modules/savers/twedit.js | 7 ++--- core/modules/savers/upload.js | 7 ++--- core/modules/startup.js | 7 +++++ core/modules/widgets/navigator.js | 2 ++ core/modules/wiki.js | 37 ++++++++++++--------------- 12 files changed, 44 insertions(+), 55 deletions(-) diff --git a/core/modules/savers/andtidwiki.js b/core/modules/savers/andtidwiki.js index 8d77c7f7d..53d11d7cf 100644 --- a/core/modules/savers/andtidwiki.js +++ b/core/modules/savers/andtidwiki.js @@ -16,10 +16,6 @@ var AndTidWiki = function(wiki) { }; AndTidWiki.prototype.save = function(text,method,callback) { - // Bail out unless this is a save (rather than a download) - if(method !== "save") { - return false; - } // Get the pathname of this document var pathname = decodeURIComponent(document.location.toString()); // Strip the file:// @@ -47,7 +43,8 @@ Information about this saver */ AndTidWiki.prototype.info = { name: "andtidwiki", - priority: 1600 + priority: 1600, + capabilities: ["save", "autosave"] }; /* diff --git a/core/modules/savers/download.js b/core/modules/savers/download.js index 2f13100ad..b84347723 100644 --- a/core/modules/savers/download.js +++ b/core/modules/savers/download.js @@ -46,7 +46,8 @@ Information about this saver */ DownloadSaver.prototype.info = { name: "download", - priority: 100 + priority: 100, + capabilities: ["save", "download"] }; /* diff --git a/core/modules/savers/fsosaver.js b/core/modules/savers/fsosaver.js index bfff51681..d61235a95 100644 --- a/core/modules/savers/fsosaver.js +++ b/core/modules/savers/fsosaver.js @@ -22,10 +22,6 @@ var FSOSaver = function(wiki) { }; FSOSaver.prototype.save = function(text,method,callback) { - // Bail out unless this is a save (rather than a download) - if(method !== "save") { - return false; - } // Get the pathname of this document var pathname = unescape(document.location.pathname); // Test for a Windows path of the form /x:\blah... @@ -53,7 +49,8 @@ Information about this saver */ FSOSaver.prototype.info = { name: "FSOSaver", - priority: 120 + priority: 120, + capabilities: ["save", "autosave"] }; /* diff --git a/core/modules/savers/manualdownload.js b/core/modules/savers/manualdownload.js index 9fde5f2f6..23eb646ea 100644 --- a/core/modules/savers/manualdownload.js +++ b/core/modules/savers/manualdownload.js @@ -33,7 +33,8 @@ Information about this saver */ ManualDownloadSaver.prototype.info = { name: "manualdownload", - priority: 0 + priority: 0, + capabilities: ["save", "download"] }; /* diff --git a/core/modules/savers/msdownload.js b/core/modules/savers/msdownload.js index a3aa756c1..78fa9c140 100644 --- a/core/modules/savers/msdownload.js +++ b/core/modules/savers/msdownload.js @@ -36,7 +36,8 @@ Information about this saver */ MsDownloadSaver.prototype.info = { name: "msdownload", - priority: 110 + priority: 110, + capabilities: ["save", "download"] }; /* diff --git a/core/modules/savers/tiddlyfox.js b/core/modules/savers/tiddlyfox.js index d78f4f2cf..5a8c988e4 100644 --- a/core/modules/savers/tiddlyfox.js +++ b/core/modules/savers/tiddlyfox.js @@ -16,10 +16,6 @@ var TiddlyFoxSaver = function(wiki) { }; TiddlyFoxSaver.prototype.save = function(text,method,callback) { - // Bail out unless this is a save (rather than a download) - if(method !== "save") { - return false; - } var messageBox = document.getElementById("tiddlyfox-message-box"); if(messageBox) { // Get the pathname of this document @@ -55,7 +51,8 @@ Information about this saver */ TiddlyFoxSaver.prototype.info = { name: "tiddlyfox", - priority: 1500 + priority: 1500, + capabilities: ["save", "autosave"] }; /* diff --git a/core/modules/savers/tiddlyie.js b/core/modules/savers/tiddlyie.js index 8ba5daba0..c04f8ec94 100644 --- a/core/modules/savers/tiddlyie.js +++ b/core/modules/savers/tiddlyie.js @@ -19,11 +19,7 @@ var TiddlyIESaver = function(wiki) { }; TiddlyIESaver.prototype.save = function(text,method,callback) { - // Bail out unless this is a save (rather than a download) - if(method !== "save") { - return false; - } - // check existence of TiddlyIE BHO extension (note: only works after document is complete) + // Check existence of TiddlyIE BHO extension (note: only works after document is complete) if(typeof(window.TiddlyIE) != "undefined") { // Get the pathname of this document var pathname = unescape(document.location.pathname); @@ -53,7 +49,8 @@ Information about this saver */ TiddlyIESaver.prototype.info = { name: "tiddlyiesaver", - priority: 1500 + priority: 1500, + capabilities: ["save"] }; /* diff --git a/core/modules/savers/twedit.js b/core/modules/savers/twedit.js index f2e9b016d..48b3ffbbc 100644 --- a/core/modules/savers/twedit.js +++ b/core/modules/savers/twedit.js @@ -16,10 +16,6 @@ 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; @@ -68,7 +64,8 @@ Information about this saver */ TWEditSaver.prototype.info = { name: "twedit", - priority: 1600 + priority: 1600, + capabilities: ["save", "autosave"] }; /* diff --git a/core/modules/savers/upload.js b/core/modules/savers/upload.js index 8ae12d971..98cc5a849 100644 --- a/core/modules/savers/upload.js +++ b/core/modules/savers/upload.js @@ -22,10 +22,6 @@ var UploadSaver = function(wiki) { }; UploadSaver.prototype.save = function(text,method,callback) { - // Bail out unless this is a save (rather than a download) - if(method !== "save") { - return false; - } // Get the various parameters we need var backupDir = this.wiki.getTextReference("$:/UploadBackupDir") || ".", username = this.wiki.getTextReference("$:/UploadName"), @@ -78,7 +74,8 @@ Information about this saver */ UploadSaver.prototype.info = { name: "upload", - priority: 2000 + priority: 2000, + capabilities: ["save", "autosave"] }; /* diff --git a/core/modules/startup.js b/core/modules/startup.js index 463a8dbd4..0b39d2212 100755 --- a/core/modules/startup.js +++ b/core/modules/startup.js @@ -93,6 +93,13 @@ exports.startup = function() { downloadType: "text/plain" }); }); + $tw.rootWidget.addEventListener("tw-auto-save-wiki",function(event) { + $tw.wiki.saveWiki({ + method: "autosave", + template: event.param, + downloadType: "text/plain" + }); + }); $tw.rootWidget.addEventListener("tw-download-file",function(event) { $tw.wiki.saveWiki({ method: "download", diff --git a/core/modules/widgets/navigator.js b/core/modules/widgets/navigator.js index 91a18c2f8..81b316281 100755 --- a/core/modules/widgets/navigator.js +++ b/core/modules/widgets/navigator.js @@ -289,6 +289,8 @@ NavigatorWidget.prototype.handleSaveTiddlerEvent = function(event) { if(draftTitle !== this.storyTitle) { this.saveStoryList(storyList); } + // Send a notification event + this.dispatchEvent({type: "tw-auto-save-wiki"}); } } } diff --git a/core/modules/wiki.js b/core/modules/wiki.js index a133b2f47..35f97640b 100755 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -904,19 +904,6 @@ exports.initSavers = function(moduleType) { }); }; -/* -Invoke the highest priority saver that successfully handles a method -*/ -exports.callSaver = function(method /*, args */ ) { - for(var t=this.savers.length-1; t>=0; t--) { - var saver = this.savers[t]; - if(saver[method].apply(saver,Array.prototype.slice.call(arguments,1))) { - return true; - } - } - return false; -}; - /* Save the wiki contents. Options are: method: "save" or "download" @@ -927,15 +914,23 @@ exports.saveWiki = function(options) { options = options || {}; var method = options.method || "save", template = options.template || "$:/core/save/all", - downloadType = options.downloadType || "text/plain"; - var text = this.renderTiddler(downloadType,template); - this.callSaver("save",text,method,function(err) { - if(err) { - alert("Error while saving:\n\n" + err); - } else { - $tw.notifier.display("$:/messages/Saved"); + downloadType = options.downloadType || "text/plain", + text = this.renderTiddler(downloadType,template), + callback = function(err) { + if(err) { + alert("Error while saving:\n\n" + err); + } else { + $tw.notifier.display("$:/messages/Saved"); + } + }; + // Call the highest priority saver that supports this method + for(var t=this.savers.length-1; t>=0; t--) { + var saver = this.savers[t]; + if(saver.info.capabilities.indexOf(method) !== -1 && saver.save(text,method,callback)) { + return true; } - }); + } + return false; }; /*