From 67ce656365616c0f7de29a6f5dee44d7f9acd55b Mon Sep 17 00:00:00 2001 From: quenousimporte Date: Tue, 6 Feb 2024 13:29:27 +0100 Subject: [PATCH] sort notes by last change fix delete and restore --- main.js | 56 +++++++++++++++++++++++++------------------------------- 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/main.js b/main.js index 3ffb3f6..84611c7 100644 --- a/main.js +++ b/main.js @@ -1654,7 +1654,10 @@ function commandpalette() suffix: [s.command] }; })) - .concat(Object.values(metadata).map(item => + .concat(Object.values(metadata) + .filter(item => !item.deleted) + .sort( (i,j) => j.lastchanged - i.lastchanged) + .map(item => { return { prefix: "note ", @@ -2301,9 +2304,12 @@ function toggletitle() function selectnote() { - // todo: sort by lastchanged return searchinlist( - Object.values(metadata).map(item => + Object + .values(metadata) + .filter(item => !item.deleted) + .sort( (i,j) => j.lastchanged - i.lastchanged) + .map(item => { return { text: item.title, @@ -2431,42 +2437,26 @@ function renamereferences(newname) function restoredeleted() { - // todo: parse local storage starting with "deleted" and reindex with a new guid - var trash = window.localStorage.getItem("trash"); - if (trash) + searchinlist(Object.values(metadata).filter(i => i.deleted).sort( (i,j) => j.lastchanged - i.lastchanged).map(i => i.title)) + .then(title => { - trash = JSON.parse(window.localStorage.getItem("trash")); - searchinlist(trash.map(note => note.title + " - deleted on " + note.deletiondate)) - .then(item => + if (confirm("Restore " + title + "?")) { - if (confirm("Restore " + item + "?")) - { - var title = item.split(" - deleted on ").shift(); - var stamp = item.split(" - deleted on ").pop(); - var index = trash.findIndex(n => n.title == title && (!n.deletiondate || n.deletiondate == stamp)); - if (index > -1) - { - var notetorestore = trash.splice(index, 1).pop(); - notetorestore.title += " - restored on " + timestamp(); - delete notetorestore.deletiondate; - localdata.unshift(notetorestore); - loadnote(notetorestore.title); - datachanged(); - window.localStorage.setItem("trash", JSON.stringify(trash)); - } - } - }); - } + var guid = getguid(title); + metadata[guid].deleted = false; + metadata[guid].lastchanged = Date.now(); + renameinternallinks(title + " (deleted)", title); + localStorage.setItem("index", JSON.stringify(metadata)); + } + }); } function deletenote(title) { var guid = getguid(title); - delete metadata[guid]; + metadata[guid].deleted = true; + metadata[guid].lastchanged = Date.now(); renameinternallinks(title, title + " (deleted)"); - var content = localStorage.getItem(guid); - localStorage.removeItem(guid); - localStorage.setItem("deleted_" + title, content); localStorage.setItem("index", JSON.stringify(metadata)); } @@ -2692,6 +2682,10 @@ function renameinternallinks(from, to) if (content != newcontent) { localStorage.setItem(guid, newcontent); + if (item.title == window.title.value) + { + seteditorcontent(newcontent); + } } }); }