fixed command palette

This commit is contained in:
quenousimporte 2024-02-02 11:42:35 +01:00
parent 47f0907e77
commit 7e75006d85
1 changed files with 50 additions and 16 deletions

66
main.js
View File

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