diff --git a/main.js b/main.js index e61ff2d..c0f06dc 100644 --- a/main.js +++ b/main.js @@ -1081,9 +1081,8 @@ function downloadhtmlnotes() }); } -function headerandtext(note) +function headerandtext(content) { - var content = note.content; var result = { header: "", @@ -1189,7 +1188,8 @@ function loadstorage() var content = defaultheaders(); var item = { title: timestamp(), - pos: content.length + pos: content.length, + header: indexheader(content) }; notesindex[guid] = item; localStorage.setItem("index", JSON.stringify(notesindex)); @@ -1302,6 +1302,31 @@ function initsnippets() }); } +function indexheader(content) +{ + var indexedheader = {}; + var hat = headerandtext(content); + var header = hat.header; + if (header) + { + header.split("\n").forEach(line => + { + if (line && line != "---") + { + var t = line.split(":"); + var val = t.pop(); + var key = t.pop(); + if (key == "tags") + { + val = val.split(",").map(t => t.trim()); + } + indexedheader[key] = val; + } + }); + } + return indexedheader; +} + function migratelegacystorage() { var legacy = localStorage.getItem("data"); @@ -1313,7 +1338,7 @@ function migratelegacystorage() { var guid = genguid(); localStorage.setItem(guid, note.content); - note.tags = gettags(note.content); + note.header = indexheader(note.content); delete note.content; index[guid] = note; }); @@ -1654,12 +1679,12 @@ function commandpalette() suffix: [s.command] }; })) - .concat(localdata.map(n => + .concat(Object.values(notesindex).map(item => { return { prefix: "note ", - text: n.title, - suffix: gettags(n).map(t => tagmark + t) + text: item.title, + suffix: item.header.tags.map(t => tagmark + t) }; })) .concat(Object.keys(settings).map(s => @@ -1857,7 +1882,9 @@ function serialize() var item = notesindex[currentguid]; item.title = title.value; item.pos = md.selectionStart; - item.tags = gettags(md.value); + item.header = indexheader(md.value); + + // is it the right place? putontop(); localStorage.setItem("index", JSON.stringify(notesindex)); @@ -2591,6 +2618,14 @@ function boldify() md.setSelectionRange(pos.start + offset, pos.end + offset); } +function foreachitemindex(callback) +{ + Object.keys(notesindex).forEach(guid => + { + callback(guid, notesindex[guid]); + }); +} + function snippetautocomplete() { var tocursor = md.value.substr(0, md.selectionStart) @@ -2653,21 +2688,20 @@ function mainkeydownhandler() else if (event.ctrlKey || event.altKey) { // notes shortcuts - var note = localdata.find(n => + var found = false; + foreachitemindex((guid, item) => { - var shortcut = n.content.match(/shortcut: (.*)/); - if (shortcut && shortcutmatches(event, shortcut[1])) + if (item.header.shortcut && shortcutmatches(event, item.header.shortcut)) { - console.log("Loading note '" + n.title + "' from header shortcut " + shortcut[1]); + console.log("Loading note '" + item.title + "' from header shortcut " + item.header.shortcut); event.preventDefault(); - loadnote(n.title); - return true; + loadnote(guid); + found = true; } - return false; }); // commands shortcuts - if (!note) + if (!found) { commands.filter(c => c.shortcut) .every(command =>