fix create new note
This commit is contained in:
quenousimporte 2024-02-04 21:13:22 +01:00
parent 3b145cf219
commit d633cf719d
1 changed files with 57 additions and 35 deletions

82
main.js
View File

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