diff --git a/plugins/tiddlywiki/browser-storage/settings.tid b/plugins/tiddlywiki/browser-storage/settings.tid index 5b8367a4f..4dfde08bd 100644 --- a/plugins/tiddlywiki/browser-storage/settings.tid +++ b/plugins/tiddlywiki/browser-storage/settings.tid @@ -1,5 +1,17 @@ title: $:/plugins/tiddlywiki/browser-storage/settings +! Disable + +You can disable the browser storage plugin: + +<$checkbox tiddler="$:/config/BrowserStorage/Enabled" field="text" checked="yes" unchecked="no" default="yes"> Use browser local storage + +! Clear + +Click this button to clear browser storage and disable its use: + +<$button message="tm-clear-browser-storage">Clear browser storage + ! Save Filter This filter determines which tiddlers will be saved to local storage. By default, it contains `[all[]]` meaning that it will attempt to save all tiddlers. Other useful values include `[prefix[$:/state/]]` to just save state tiddlers, thus preserving selected tabs, and the open/closed status of table of contents entries. diff --git a/plugins/tiddlywiki/browser-storage/startup.js b/plugins/tiddlywiki/browser-storage/startup.js index 39a9ff842..83e7affcb 100644 --- a/plugins/tiddlywiki/browser-storage/startup.js +++ b/plugins/tiddlywiki/browser-storage/startup.js @@ -18,7 +18,8 @@ exports.platforms = ["browser"]; exports.after = ["load-modules"]; exports.synchronous = true; -var SAVE_FILTER_TITLE = "$:/config/BrowserStorage/SaveFilter", +var ENABLED_TITLE = "$:/config/BrowserStorage/Enabled", + SAVE_FILTER_TITLE = "$:/config/BrowserStorage/SaveFilter", QUOTA_EXCEEDED_ALERT_TITLE = "$:/config/BrowserStorage/QuotaExceededAlert", DEFAULT_QUOTA_EXCEEDED_ALERT_PREFIX = "Quota exceeded attempting to store `", DEFAULT_QUOTA_EXCEEDED_ALERT_SUFFIX = "` in browser local storage"; @@ -37,8 +38,17 @@ exports.startup = function() { filterFn = $tw.wiki.compileFilter($tw.wiki.getTiddlerText(SAVE_FILTER_TITLE)); } compileFilter(); + // Listen for tm-clear-browser-storage messages + $tw.rootWidget.addEventListener("tm-clear-browser-storage",function(event) { + window.localStorage.clear(); + $tw.wiki.addTiddler({title: ENABLED_TITLE, text: "no"}); + }); // Track tiddler changes $tw.wiki.addEventListener("change",function(changes) { + // Bail if browser storage is disabled + if($tw.wiki.getTiddlerText(ENABLED_TITLE) === "no") { + return; + } // Recompile the filter if it has changed if(changes[SAVE_FILTER_TITLE]) { compileFilter(); @@ -50,8 +60,12 @@ exports.startup = function() { iterator(tiddler,title); }); }); -console.log("Filtered changes",filteredChanges) $tw.utils.each(filteredChanges,function(title) { + // Don't try to save changes to our enabled status + // (If it were enabled in the file but disabled in local storage then we might not realise that distributing a copy of the file would have local storage enabled for other users) + if(title === ENABLED_TITLE) { + return; + } // Get the tiddler var tiddler = $tw.wiki.getTiddler(title); if(tiddler) {