1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-10-31 15:42:59 +00:00

Browser-storage: Add enable/disable and clear button

This commit is contained in:
Jermolene
2019-03-01 22:06:14 +00:00
parent 655fc31cee
commit a6875df7ef
2 changed files with 28 additions and 2 deletions

View File

@@ -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) {