diff --git a/main.js b/main.js index 5f6a1e4..0193d5d 100644 --- a/main.js +++ b/main.js @@ -24,8 +24,7 @@ var codelanguages = ["xml", "js", "sql"]; var tagmark = "+"; // globals -var currentguid = null; -var notesindex = null; +var metadata = null; var fileindex = 0; var workerid = null; var backup = ""; @@ -1162,6 +1161,19 @@ function remotecallfailed(error) } } +function getguid(title) +{ + var guid = null; + foreachmetadata( (g,i) => + { + if (i.title == title) + { + guid = g; + } + }); + return guid; +} + function gotoline(line) { var i = 0; @@ -1177,23 +1189,28 @@ function gotoline(line) } } +function createnote(title) +{ + var guid = genguid(); + var content = defaultheaders(); + var item = { + title: title, + pos: content.length, + header: indexheader(content) + }; + metadata[guid] = item; + localStorage.setItem("index", JSON.stringify(metadata)); + localStorage.setItem(guid, content); + return guid; +} + function loadstorage() { - notesindex = JSON.parse(localStorage.getItem("index")); - if (!notesindex) + metadata = JSON.parse(localStorage.getItem("index")); + if (!metadata) { - notesindex = {}; - // todo: refactor in "add new note" - var guid = genguid(); - var content = defaultheaders(); - var item = { - title: timestamp(), - pos: content.length, - header: indexheader(content) - }; - notesindex[guid] = item; - localStorage.setItem("index", JSON.stringify(notesindex)); - localStorage.setItem(guid, content); + metadata = {}; + createnote(timestamp()); } var params = new URLSearchParams(window.location.search); @@ -1230,9 +1247,10 @@ function loadstorage() } }*/ - if (currentguid) + if (window.title.value) { - bind(currentguid); + var guid = getguid(window.title.value); + bind(guid); if (line) { gotoline(line); @@ -1336,6 +1354,7 @@ function migratelegacystorage() var legacy = localStorage.getItem("data"); if (legacy) { + // todo: use title as key and guid as property. or not. var legacy = JSON.parse(legacy); var index = {}; legacy.forEach(note => @@ -1578,7 +1597,7 @@ function md2html(content) function loadlast() { - loadnote(Object.keys(notesindex)[0]); + loadnote(Object.values(metadata)[0].title); } function loadprevious() @@ -1683,7 +1702,7 @@ function commandpalette() suffix: [s.command] }; })) - .concat(Object.values(notesindex).map(item => + .concat(Object.values(metadata).map(item => { return { prefix: "note ", @@ -1883,7 +1902,8 @@ function setsaved() function serialize() { // serialize all gui stuff to local storage - var item = notesindex[currentguid]; + var guid = getguid(window.title.value); + var item = metadata[guid]; item.title = title.value; item.pos = md.selectionStart; item.header = indexheader(md.value); @@ -1891,8 +1911,8 @@ function serialize() // is it the right place? putontop(); - localStorage.setItem("index", JSON.stringify(notesindex)); - localStorage.setItem(currentguid, md.value); + localStorage.setItem("index", JSON.stringify(metadata)); + localStorage.setItem(guid, md.value); } @@ -2343,7 +2363,7 @@ function toggletitle() function selectnote() { return searchinlist( - Object.values(notesindex).map(item => + Object.values(metadata).map(item => { return { text: item.title, @@ -2365,9 +2385,7 @@ function searchandloadnote() selectnote().then(selected => { var title = selected.text || selected; - var guid = Object.values(notesindex).find(i => i.title == title); - // todo: create a new one if not found - loadnote(guid); + loadnote(title); }); } @@ -2625,11 +2643,11 @@ function boldify() md.setSelectionRange(pos.start + offset, pos.end + offset); } -function foreachitemindex(callback) +function foreachmetadata(callback) { - Object.keys(notesindex).forEach(guid => + Object.keys(metadata).forEach(guid => { - callback(guid, notesindex[guid]); + callback(guid, metadata[guid]); }); } @@ -2696,13 +2714,13 @@ function mainkeydownhandler() { // notes shortcuts var found = false; - foreachitemindex((guid, item) => + foreachmetadata((guid, item) => { if (item.header.shortcut && shortcutmatches(event, item.header.shortcut)) { console.log("Loading note '" + item.title + "' from header shortcut " + item.header.shortcut); event.preventDefault(); - loadnote(guid); + loadnote(item.title); found = true; } }); @@ -2977,11 +2995,15 @@ function defaultheaders(tags = "") "",""].join("\n"); } -function loadnote(guid) +function loadnote(title) { - currentguid = guid; + var guid = getguid(title); + if (!guid) + { + guid = createnote(title); + } var content = localStorage.getItem(guid); - var item = notesindex[guid]; + var item = metadata[guid]; /*if (gettags(content).includes("journal")) {