1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 10:07:19 +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

@ -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</$checkbox>
! Clear
Click this button to clear browser storage and disable its use:
<$button message="tm-clear-browser-storage">Clear browser storage</$button>
! 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.

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