mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-06-24 22:33:16 +00:00
Browser-storage: Handle quota exceeded error
This commit is contained in:
parent
e31c5563ff
commit
6f5f9ca2fb
|
@ -28,7 +28,6 @@ function hookBootTiddlersLoaded() {
|
||||||
for(var index=0; index<window.localStorage.length; index++) {
|
for(var index=0; index<window.localStorage.length; index++) {
|
||||||
var key = window.localStorage.key(index),
|
var key = window.localStorage.key(index),
|
||||||
parts = key.split("#");
|
parts = key.split("#");
|
||||||
console.log("key",key)
|
|
||||||
// If it's ours
|
// If it's ours
|
||||||
if(parts[0] === "tw5" && parts[1] === url) {
|
if(parts[0] === "tw5" && parts[1] === url) {
|
||||||
// Read it as JSON
|
// Read it as JSON
|
||||||
|
@ -45,7 +44,6 @@ console.log("key",key)
|
||||||
// Get any existing tiddler
|
// Get any existing tiddler
|
||||||
var title = incomingTiddler.fields.title,
|
var title = incomingTiddler.fields.title,
|
||||||
existingTiddler = $tw.wiki.getTiddler(title);
|
existingTiddler = $tw.wiki.getTiddler(title);
|
||||||
console.log("Existing",title,existingTiddler,existingTiddler && existingTiddler.isEqual(incomingTiddler))
|
|
||||||
if(existingTiddler && existingTiddler.isEqual(incomingTiddler)) {
|
if(existingTiddler && existingTiddler.isEqual(incomingTiddler)) {
|
||||||
// If the incoming tiddler is the same as the existing then we can delete the local storage version
|
// If the incoming tiddler is the same as the existing then we can delete the local storage version
|
||||||
window.localStorage.removeItem(key);
|
window.localStorage.removeItem(key);
|
||||||
|
|
|
@ -13,6 +13,8 @@ Browser local storage is not a panacea for TiddlyWiki:
|
||||||
|
|
||||||
Please use this plugin with caution. There are a number of unresolved issues and open questions:
|
Please use this plugin with caution. There are a number of unresolved issues and open questions:
|
||||||
|
|
||||||
* An "red screen of embarrassment" error is raised when local storage is full
|
* A somewhat user hostile error is raised when local storage is full
|
||||||
* Innerwikis read the local storage of their parent wikis
|
* Innerwikis read the local storage of their parent wikis
|
||||||
* This plugin does not interfere with the existing saver mechanism, so you'll still get warnings when refreshing the page, even if your changes are safely committed to local storage
|
* This plugin does not interfere with the existing saver mechanism, so you'll still get warnings when refreshing the page, even if your changes are safely committed to local storage
|
||||||
|
* Deleted tiddlers will be restored when the wiki is refreshed
|
||||||
|
|
||||||
|
|
|
@ -21,14 +21,32 @@ exports.synchronous = true;
|
||||||
exports.startup = function() {
|
exports.startup = function() {
|
||||||
// Compute our prefix for local storage keys
|
// Compute our prefix for local storage keys
|
||||||
var url = window.location.protocol === "file:" ? window.location.pathname : "",
|
var url = window.location.protocol === "file:" ? window.location.pathname : "",
|
||||||
prefix = "tw5#" + url + "#"
|
prefix = "tw5#" + url + "#";
|
||||||
|
// Make a logger
|
||||||
|
var logger = new $tw.utils.Logger("browser-storage",{
|
||||||
|
colour: "cyan"
|
||||||
|
});
|
||||||
// Track tiddler changes
|
// Track tiddler changes
|
||||||
$tw.wiki.addEventListener("change",function(changes) {
|
$tw.wiki.addEventListener("change",function(changes) {
|
||||||
$tw.utils.each(changes,function(change,title) {
|
$tw.utils.each(changes,function(change,title) {
|
||||||
|
// Get the tiddler
|
||||||
var tiddler = $tw.wiki.getTiddler(title);
|
var tiddler = $tw.wiki.getTiddler(title);
|
||||||
if(tiddler) {
|
if(tiddler) {
|
||||||
|
// Get the JSON of the tiddler
|
||||||
var json = JSON.stringify(tiddler.getFieldStrings());
|
var json = JSON.stringify(tiddler.getFieldStrings());
|
||||||
window.localStorage.setItem(prefix + title,json);
|
// Try to save it to local storage
|
||||||
|
try {
|
||||||
|
window.localStorage.setItem(prefix + title,json);
|
||||||
|
} catch(e) {
|
||||||
|
if(e.name === "QuotaExceededError") {
|
||||||
|
// Complain if we failed
|
||||||
|
logger.alert("Quota exceeded trying to store '" + title + "' in browser local storage");
|
||||||
|
// No point in keeping old values around for this tiddler
|
||||||
|
window.localStorage.removeItem(prefix + title);
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
console.log("browser-storage: Saving",title);
|
console.log("browser-storage: Saving",title);
|
||||||
} else {
|
} else {
|
||||||
window.localStorage.removeItem(prefix + title);
|
window.localStorage.removeItem(prefix + title);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user