From 3a09e1b50a0c3d70f843484fb22e6a64ff853f70 Mon Sep 17 00:00:00 2001 From: quenousimporte Date: Fri, 29 Sep 2023 17:57:22 +0200 Subject: [PATCH] added: feature to restore a deleted note (from local trash) --- main.js | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/main.js b/main.js index ac758e2..275e34c 100644 --- a/main.js +++ b/main.js @@ -214,7 +214,7 @@ var commands = [ action: deletecurrentnote }, { - hint: "Restore note", + hint: "Restore current note", action: restore }, { @@ -366,6 +366,10 @@ var commands = [ hint: "Show upcoming events", action: showupcomingevents, remoteonly: true +}, +{ + hint: "Restore deleted note", + action: restoredeleted }]; var snippets = [ @@ -2331,9 +2335,40 @@ function rename(newname) return ""; } +function restoredeleted() +{ + var trash = window.localStorage.getItem("trash"); + if (trash) + { + trash = JSON.parse(window.localStorage.getItem("trash")); + searchinlist(trash.map(note => note.title + " - deleted on " + note.deletiondate)) + .then(item => + { + 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 == stamp); + if (index > -1) + { + var notetorestore = trash.splice(index, 1).pop(); + window.localStorage.setItem("trash", JSON.stringify(trash)); + + notetorestore.title += " - restored on " + timestamp(); + delete notetorestore.deletiondate; + localdata.unshift(notetorestore); + loadnote(notetorestore.title); + datachanged(); + } + } + }); + } +} + function deletenote(note) { var trash = JSON.parse(window.localStorage.getItem("trash") || "[]"); + note.deletiondate = timestamp(); trash.push(note); window.localStorage.setItem("trash", JSON.stringify(trash));