From a1c0033e26ba44f734e5d9440ee8de2f4bdcae16 Mon Sep 17 00:00:00 2001 From: quenousimporte Date: Thu, 15 Feb 2024 22:11:56 +0100 Subject: [PATCH] fix save to server --- main.js | 182 +++++++++++++++++++++++--------------------------------- 1 file changed, 75 insertions(+), 107 deletions(-) diff --git a/main.js b/main.js index d576e91..809fc16 100644 --- a/main.js +++ b/main.js @@ -31,6 +31,7 @@ var workerid = null; var backup = ""; var settings = null; var tags = null; +var pending = {}; var commands = [ { @@ -58,7 +59,7 @@ var commands = [ }, { hint: "Force save", - action: serialize, + action: flush, shortcut: "ctrl+s" }, { @@ -284,7 +285,7 @@ function seteditorcontent(content) { md.value = content; applycolors(); - serialize(); + flush(); resize(); } @@ -351,6 +352,47 @@ function currentrange() }; } +function pushitem(key, value) +{ + unsavedmark.hidden = false; + pending[key] = true; + + encryptstring(value) + .then(encrypted => + { + return queryremote({action: "push", name: key, data: encrypted}); + }) + .then( () => + { + console.log(key + " pushed to server"); + delete pending[key]; + }) + .catch( err => + { + var msg = "error while pushing '" + (metadata[key] ? metadata[key].title : key) + "': " + err; + console.error(msg); + showtemporaryinfo(msg); + setTimeout( () => + { + serialize(key, value); + }, settings.savedelay); + }) + .finally( () => + { + unsavedmark.hidden = Object.keys(pending) == 0; + }); +} + +function serialize(key, value) +{ + localStorage.setItem(key, value); + + if (settings.sync) + { + pushitem(key, value); + } +} + function createsubnote() { var title = prompt("Subnote tite:"); @@ -374,7 +416,7 @@ function createsubnote() var range = currentrange(); var content = getrangecontent(range); guid = createnote(title); - localStorage.setItem(guid, content); + serialize(guid, content); seteditorcontent(md.value.substring(0, range.start) + "[[" + title + "]]" @@ -445,6 +487,7 @@ function showinfo() showtemporaryinfo( [ "sync: " + (settings.sync ? "en" : "dis") + "abled", + "pending: " + Object.keys(pending).join(), "title: " + title.value, "line count: " + md.value.split("\n").length, "word count: " + getwords(), @@ -457,7 +500,7 @@ function showinfo() function savesettings() { - window.localStorage.setItem("settings", JSON.stringify(settings)); + window.serialize("settings", JSON.stringify(settings)); } function descendants(note) @@ -1044,9 +1087,9 @@ function inserttodo(text) } else { - localStorage.setItem(guid, content); + serialize(guid, content); metadata[guid].lastchanged = Date.now(); - serialize(); + flush(); } } @@ -1118,7 +1161,7 @@ function createnote(title) }; metadata[guid] = item; serializeindex() - localStorage.setItem(guid, content); + serialize(guid, content); return guid; } @@ -1202,10 +1245,10 @@ function loadsettings() function checksaved() { - /*if (!saved) + if (!unsavedmark.hidden) { return "not saved"; - }*/ + } } function initsnippets() @@ -1825,7 +1868,7 @@ function postpone() }); } -function serialize() +function flush() { clearTimeout(workerid); workerid = null; @@ -1839,8 +1882,8 @@ function serialize() item.header = indexheader(md.value); item.lastchanged = Date.now(); - serializeindex() - localStorage.setItem(guid, md.value); + serializeindex(); + serialize(guid, md.value); } else if (title.value == "settings.json") { @@ -1850,98 +1893,13 @@ function serialize() } else if (title.value == "pgpkeys") { - localStorage.setItem("pgpkeys", md.value); + serialize("pgpkeys", md.value); } - console.log("data serialized"); + + unsavedmark.hidden = Object.keys(pending).length == 0; + console.log("data serialized locally"); } -/*function save() -{ - return new Promise(function(resolve, reject) - { - clearTimeout(workerid); - - if (title.value == "settings.json") - { - settings = JSON.parse(md.value); - savesettings(); - loadsettings(); - setsaved(); - resolve(); - } - else if (title.value == "pgpkeys") - { - localStorage.setItem("pgpkeys", md.value); - setsaved(); - resolve(); - } - else if (pending) - { - console.log("pending query: save cancelled"); - reject(); - } - else if (saved) - { - console.log("nothing to save"); - reject(); - } - else - { - var content = md.value; - if ((content == "" && backup != "") || content == "null" || content == "undefined") - { - showtemporaryinfo("Invalid content '" + content + "', file '" + title.value + "' not saved"); - reject(); - } - else - { - serialize(); - - if (settings.sync) - { - var datatosend = JSON.stringify(localdata); - return encryptstring(datatosend) - .then(encrypted => - { - console.log("sending data to php server..."); - pending = true; - return queryremote({action: "push", data: encrypted}) - }) - .then(() => - { - console.log("...data saved on server"); - setsaved(); - }) - .catch(remotecallfailed) - .finally(() => - { - pending = false; - if (content != md.value) - { - console.log("but content changed: will save again"); - return datachanged(); - } - else if (!saved) - { - console.log("save failed. Data unsaved on server. Will retry."); - return datachanged(); - } - else - { - resolve(); - } - }); - } - else - { - setsaved(); - resolve(); - } - } - } - }); -}*/ - function escapeHtml(unsafe) { return unsafe .replace(/&/g, "&") @@ -2199,10 +2157,14 @@ function applycolors(currentonly) function editorinput() { + unsavedmark.hidden = false; + // criteria to improve. Or redraw only after? var multiline = md.value.substring(md.selectionStart, md.selectionEnd).includes("\n"); applycolors(!multiline && event.data && (event.inputType == "insertText" || event.inputType == "deleteContentBackward" || event.inputType == "deleteContentForward")); - postpone().then(serialize); + + // todo: fix if current note change during postponing, the wrong one will be saved. Or prevent binding another note? + postpone().then(flush); resize(); } @@ -2216,13 +2178,13 @@ function timestamp() function quicknewnote() { - serialize(); + flush(); loadnote(timestamp()); } function startnewnote() { - serialize(); + flush(); var title = prompt("Note title: ", timestamp()); if (title) { @@ -2410,7 +2372,7 @@ function restoredeleted() function serializeindex() { - localStorage.setItem("index", JSON.stringify(metadata)); + serialize("index", JSON.stringify(metadata)); } function deletenote(title) @@ -2639,7 +2601,7 @@ function renameinternallinks(from, to) var newcontent = content.replaceAll("[[" + from + "]]", "[[" + to + "]]"); if (content != newcontent) { - localStorage.setItem(guid, newcontent); + serialize(guid, newcontent); if (item.title == window.title.value) { seteditorcontent(newcontent); @@ -2664,7 +2626,7 @@ function ontitlechange() previoustitle = title.value; metadata[guid].title = title.value; setwindowtitle(); - serialize(); + flush(); if (!settings.titlebydefault) { @@ -2846,6 +2808,12 @@ function togglepreviewwithsubs() function bind(title, content, pos) { + if (workerid) + { + showtemporaryinfo("current note not yet serialized"); + return; + } + previoustitle = title; backup = content;