1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-29 21:09:56 +00:00

Update tw.utils.Logger using tw.utils.Logger

This commit is contained in:
pmario 2024-06-21 09:02:45 +02:00
parent eb38b0238a
commit 08577b7fdb
2 changed files with 30 additions and 29 deletions

View File

@ -25,24 +25,21 @@ var ENABLED_TITLE = "$:/config/BrowserStorage/Enabled",
var BrowserStorageUtil = require("$:/plugins/tiddlywiki/browser-storage/util.js").BrowserStorageUtil;
exports.startup = function() {
var self = this;
// If not exists, add ENABLED tiddler with default value "yes"
if(!$tw.wiki.getTiddler(ENABLED_TITLE)) {
$tw.wiki.addTiddler({title: ENABLED_TITLE, text: "yes"});
}
// If not exists, add ENABLED tiddler with default value "yes"
if(!$tw.wiki.getTiddler(ENABLED_TITLE)) {
$tw.wiki.addTiddler({title: ENABLED_TITLE, text: "yes"});
}
// Compute our prefix for local storage keys
var prefix = "tw5#" + window.location.pathname + "#";
// Make a logger
var logger = new $tw.utils.Logger("browser-storage",{
colour: "cyan"
});
// Add browserStorage object to $tw
$tw.browserStorage = new BrowserStorageUtil($tw.wiki,{
enabledTitle: ENABLED_TITLE,
prefix: prefix,
logger: logger
prefix: prefix
});
// Add a logger
$tw.browserStorage.logger = new $tw.utils.Logger("browser-storage",{
colour: "green"
});
// Function to compile the filter
var filterFn,
compileFilter = function() {
@ -60,19 +57,26 @@ exports.startup = function() {
},
requestPersistence = function() {
setPersistedState("requested");
$tw.browserStorage.logger.info("Show user request for browser PERSISTED storage");
navigator.storage.persist().then(function(persisted) {
console.log("Request for persisted storage " + (persisted ? "granted" : "denied"));
setPersistedState(persisted ? "granted" : "denied");
if(persisted) {
$tw.browserStorage.logger.info("User GRANTED access to browser PERSISTED storage");
setPersistedState("granted");
}
else {
$tw.browserStorage.logger.info("User DENIED access to persisted storage. Storage MAY BE CLEARED by the UA under storage pressure.");
setPersistedState("denied");
}
});
},
persistPermissionRequested = false,
requestPersistenceOnFirstSave = function() {
$tw.hooks.addHook("th-saving-tiddler", function(tiddler) {
if (!persistPermissionRequested) {
if(!persistPermissionRequested) {
var filteredChanges = filterFn.call($tw.wiki, function(iterator) {
iterator(tiddler,tiddler.getFieldString("title"));
});
if (filteredChanges.length > 0) {
if(filteredChanges.length > 0) {
// The tiddler will be saved to local storage, so request persistence
requestPersistence();
persistPermissionRequested = true;
@ -84,9 +88,9 @@ exports.startup = function() {
// Request the browser to never evict the localstorage. Some browsers such as firefox
// will prompt the user. To make the decision easier for the user only prompt them
// when they click the save button on a tiddler which will be stored to localstorage.
if (navigator.storage && navigator.storage.persist) {
if(navigator.storage && navigator.storage.persist) {
navigator.storage.persisted().then(function(isPersisted) {
if (!isPersisted) {
if(!isPersisted) {
setPersistedState("not requested yet");
requestPersistenceOnFirstSave();
} else {

View File

@ -45,7 +45,7 @@ BrowserStorageUtil.prototype.addCachedTiddlers = function() {
};
BrowserStorageUtil.prototype.removeTiddlerFromLocalStorage = function(title) {
console.log("browser-storage: Removing", title);
this.logger.log("Removing", title);
window.localStorage.removeItem(this.options.prefix + title);
};
@ -53,9 +53,9 @@ BrowserStorageUtil.prototype.saveTiddlerToLocalStorage = function(title) {
// Get the tiddler
var tiddler = $tw.wiki.getTiddler(title);
if(tiddler) {
if (this.wiki.tiddlerExists(title)) {
if(this.wiki.tiddlerExists(title)) {
// This is not a shadow tiddler
console.log("browser-storage: Saving",title);
this.logger.log("Saving",title);
// Get the JSON of the tiddler
var json = JSON.stringify(tiddler.getFieldStrings());
// Try to save it to local storage
@ -65,13 +65,11 @@ BrowserStorageUtil.prototype.saveTiddlerToLocalStorage = function(title) {
if(e.name === "QuotaExceededError") {
// Complain if we failed
var msg = $tw.wiki.getTiddlerText(this.QUOTA_EXCEEDED_ALERT_TITLE,this.DEFAULT_QUOTA_EXCEEDED_ALERT_PREFIX + title + this.DEFAULT_QUOTA_EXCEEDED_ALERT_SUFFIX);
if(this.options.logger) {
this.options.logger.alert(msg);
}
this.logger.alert(msg);
// No point in keeping old values around for this tiddler
window.localStorage.removeItem(this.options.prefix + title);
} else {
console.log("Browser-storage error:",e);
this.logger.log("Error:",e);
}
}
} else {
@ -83,18 +81,17 @@ BrowserStorageUtil.prototype.saveTiddlerToLocalStorage = function(title) {
// In local storage, use the special value of empty string to mark the tiddler as deleted
// On future page loads, if the tiddler is already gone from startup then the blank entry
// will be removed from localstorage. Otherwise, the tiddler will be deleted.
console.log("browser-storage: Blanking",title);
this.logger.log("Blanking",title);
try {
window.localStorage.setItem(this.options.prefix + title, "");
} catch(e) {
console.log("Browser-storage error:",e);
this.logger.error("Error:",e);
}
}
};
BrowserStorageUtil.prototype.clearLocalStorage = function() {
var url = window.location.pathname,
log = [];
var url = window.location.pathname;
// Step through each browser storage item
if(window.localStorage) {
for(var index=window.localStorage.length - 1; index>=0; index--) {