fix subnotes, graph, etc

This commit is contained in:
quenousimporte 2024-02-07 21:41:03 +01:00
parent 67ce656365
commit 5857299d06
1 changed files with 76 additions and 127 deletions

187
main.js
View File

@ -211,10 +211,6 @@ var commands = [
hint: "Restore deleted note",
action: restoredeleted
},
{
hint: "Notes by size",
action: notesbysize
},
{
hint: "Replace",
shortcut: "ctrl+h",
@ -289,7 +285,7 @@ function purgetodo()
{
if (currentistodo() && confirm("Remove completed tasks?"))
{
seteditorcontent(currentnote.content.replace(/\nx .*/g, ""));
seteditorcontent(md.value.replace(/\nx .*/g, ""));
}
}
@ -373,7 +369,10 @@ function createsubnote()
setpos(md.selectionStart);
md.focus();
}
else if (getnote(title))
else
{
var guid = getguid(title);
if (guid)
{
showtemporaryinfo("'" + title + "' already exists");
setpos(md.selectionStart);
@ -383,19 +382,16 @@ function createsubnote()
{
var range = currentrange();
var content = getrangecontent(range);
var newnote =
{
title: title,
content: content
}
localdata.unshift(newnote);
guid = createnote(title);
localStorage.setItem(guid, content);
seteditorcontent(md.value.substring(0, range.start)
+ "[[" + title + "]]"
+ md.value.substring(range.end));
}
}
}
function comment()
{
seteditorcontent(md.value.substring(0, md.selectionStart)
@ -454,16 +450,16 @@ function pospercent()
function showinfo()
{
var tags = gettags(currentnote);
var tags = gettags(md.value);
showtemporaryinfo(
[
"sync: " + (settings.sync ? "en" : "dis") + "abled",
"title: " + currentnote.title,
"title: " + title.value,
"line count: " + md.value.split("\n").length,
"word count: " + getwords(),
"cursor position: " + md.selectionStart + " (" + pospercent() + "%)",
(tags ? "tags: " + tags : ""),
"notes count: " + localdata.length,
"notes count: " + sortedlist().length,
"spell check: " + (md.spellcheck ? "en" : "dis") + "abled"
].join("\n"));
}
@ -490,19 +486,20 @@ function descendants(note)
return result;
}
function children(note)
function children(guid)
{
return (note.content
var content = localStorage.getItem(guid);
return (content
.match(/\[\[([^\]]*)\]\]/g) || [])
.map(l => l.replace("[[", "").replace("]]", ""))
.filter(l => !l.includes("(deleted)"))
.map(l => getnote(l));
.map(l => getguid(l));
}
function parents(note)
function parents(guid)
{
return localdata
.filter(n => n.content.indexOf("[[" + note.title + "]]") != -1);
return Object.keys(metadata)
.filter(g => localStorage.getItem(g).indexOf("[[" + metadata[guid].title + "]]") != -1);
}
function connected(note)
@ -532,11 +529,6 @@ function toggleeditor(hidden)
}
}
function id(note)
{
return localdata.indexOf(note);
}
function shownotelinks()
{
if (settings.enablenetwork)
@ -547,17 +539,17 @@ function shownotelinks()
var nodes = [];
var edges = [];
var list = [currentnote];
var list = [getguid(title.value)];
while (list.length)
{
var current = list.shift();
if (!nodes.find(n => n.id == id(current)))
if (!nodes.find(n => n.id == current))
{
nodes.push(
{
id: id(current),
label: current.title
id: current,
label: metadata[current].title
});
var buddies = children(current).concat(parents(current));
@ -566,11 +558,11 @@ function shownotelinks()
buddies.
forEach(buddy => {
if (!edges.find(edge => (edge.to == id(current) && edge.from == id(buddy)) || (edge.to == id(buddy) && edge.from == id(current))))
if (!edges.find(edge => (edge.to == current && edge.from == buddy) || (edge.to == buddy && edge.from == current)))
{
edges.push({
from: id(current),
to: id(buddy)
from: current,
to: buddy
});
}
});
@ -609,7 +601,7 @@ function shownotelinks()
});
graph.setSelection(
{
nodes : [id(currentnote)]
nodes : [getguid(title.value)]
});
}
else
@ -964,7 +956,7 @@ function share()
navigator.share(
{
text: md.value,
title: currentnote.title
title: title.value
});
}
}
@ -974,14 +966,14 @@ function sharehtml()
if (navigator.share)
{
var file = new File(['<html><body>' + md2html(md.value) + '</body></html>'],
currentnote.title + ".html",
title.value + ".html",
{
type: "text/html",
});
navigator.share(
{
title: currentnote.title,
title: title.value,
files: [file]
});
}
@ -1107,7 +1099,7 @@ function downloadnotewithsubs()
function downloadnote()
{
download(currentnote.title + ".md", md.value);
download(title.value + ".md", md.value);
}
function remotecallfailed(error)
@ -1142,7 +1134,7 @@ function gotoline(line)
var pos = 0;
while (i < line && pos > -1)
{
pos = currentnote.content.indexOf("\n", pos + 1);
pos = md.value.indexOf("\n", pos + 1);
i++;
}
if (pos > -1)
@ -1156,6 +1148,7 @@ function createnote(title)
var guid = genguid();
var content = defaultheaders();
var item = {
lastchanged: Date.now(),
title: title,
pos: content.length,
header: indexheader(content)
@ -1194,29 +1187,10 @@ function loadstorage()
window.close();
}
// when multiple tabs or split?
/*if (currentnote)
{
currentnote = getnote(currentnote.title);
}
else if (title)
{
currentnote = getnote(title);
if (!currentnote)
{
var newcontent = params.get("description") || defaultheaders(tags);
currentnote = {title: title, content: newcontent, pos: newcontent.length};
localdata.unshift(currentnote);
}
}*/
if (window.title.value)
{
bind(window.title.value);
if (line)
{
gotoline(line);
}
// reload current
loadnote(window.title.value);
}
else
{
@ -1489,7 +1463,7 @@ function getlinesrange()
function backlinks()
{
searchinlist(localdata
.filter(n => n.content.includes("[[" + currentnote.title + "]]"))
.filter(n => n.content.includes("[[" + title.value + "]]"))
.map(n => n.title))
.then(loadnote);
}
@ -1553,11 +1527,12 @@ function md2html(content)
function loadlast()
{
loadnote(Object.values(metadata)[0].title);
loadnote(sortedlist()[0].title);
}
function loadprevious()
{
//var list = sortedlist();
var index = localdata.indexOf(currentnote);
if (index > -1 && index < localdata.length - 1)
{
@ -1574,24 +1549,31 @@ function loadnext()
}
}
function sortedlist(deleted)
{
// todo: fix deleted stuff
deleted = deleted === true;
return Object.values(metadata).filter(i => !deleted || i.deleted).sort( (i,j) => j.lastchanged - i.lastchanged);
}
function grep(needle)
{
var result = {};
localdata
.filter(note => note.title != "events.json")
.forEach(note =>
sortedlist()
.forEach(item =>
{
if (note.title.toLowerCase().includes(needle.toLowerCase()))
if (item.title.toLowerCase().includes(needle.toLowerCase()))
{
result[note.title] = {};
result[item.title] = {};
}
note.content.split("\n")
var content = localStorage.getItem(getguid(item.title));
content.split("\n")
.forEach((line, nb) => {
if (line.toLowerCase().includes(needle.toLowerCase()))
{
result[note.title] = result[note.title] || {};
result[note.title][nb] = line;
result[item.title] = result[item.title] || {};
result[item.title][nb] = line;
}
});
});
@ -1654,9 +1636,8 @@ function commandpalette()
suffix: [s.command]
};
}))
.concat(Object.values(metadata)
.filter(item => !item.deleted)
.sort( (i,j) => j.lastchanged - i.lastchanged)
.concat(
sortedlist()
.map(item =>
{
return {
@ -2305,10 +2286,7 @@ function toggletitle()
function selectnote()
{
return searchinlist(
Object
.values(metadata)
.filter(item => !item.deleted)
.sort( (i,j) => j.lastchanged - i.lastchanged)
sortedlist()
.map(item =>
{
return {
@ -2335,20 +2313,14 @@ function searchandloadnote()
});
}
function istodo(note)
{
return note.title.includes("todo") || gettags(note).includes("todo");
}
function currentistodo()
{
//return istodo(currentnote);
return title.value.includes("todo") || gettags(md.value).includes("todo");
}
function sorttodotxt(note)
function sorttodotxt(content)
{
var hat = headerandtext(note);
var hat = headerandtext(content);
var olditems = hat.text.split("\n");
var prio = [];
var std = [];
@ -2377,15 +2349,15 @@ function sorttodotxt(note)
prio = prio.sort((a,b) => a.localeCompare(b));
done = done.sort((a,b) => a.localeCompare(b));
var all = prio.concat(std).concat(done);
note.content = hat.header + all.join("\n");
return hat.header + all.join("\n");
}
function sortcurrentastodo()
{
if (currentistodo())
{
sorttodotxt(currentnote);
seteditorcontent(currentnote.content);
var content = sorttodotxt(md.value);
seteditorcontent(content);
}
}
@ -2412,32 +2384,9 @@ function searchandreplace()
seteditorcontent(md.value.replaceAll(oldvalue, newvalue));
}
function notesbysize()
{
var sortedtitles = localdata.sort( (n1,n2) => { return n2.content.length - n1.content.length})
.map(n => n.title + ": " + formatsize(n.content.length));
searchinlist(sortedtitles)
.then(titlewithsize =>
{
var title = titlewithsize.substring(0, titlewithsize.lastIndexOf(": "));
loadnote(title);
});
}
function renamereferences(newname)
{
localdata
.filter(note => note != currentnote)
.forEach(note =>
{
note.content = note.content.replaceAll("[[" + currentnote.title + "]]", "[[" + newname + "]]");
});
}
function restoredeleted()
{
searchinlist(Object.values(metadata).filter(i => i.deleted).sort( (i,j) => j.lastchanged - i.lastchanged).map(i => i.title))
searchinlist(sortedlist(true).map(i => i.title))
.then(title =>
{
if (confirm("Restore " + title + "?"))
@ -2530,7 +2479,7 @@ function esc(event)
{
removelinkdialog();
}
else if (currentnote.title == "Help" || currentnote.title == "Search result")
else if (title.value == "Help" || title.value == "Search result")
{
loadlast();
}
@ -2942,20 +2891,20 @@ function loadnote(title)
var content = localStorage.getItem(guid);
var item = metadata[guid];
/*if (gettags(content).includes("journal"))
if (item.header.tags.includes("journal"))
{
// remove empty entries
content = content.replace(/\d{4}-\d{2}-\d{2}\n*(\d{4}-\d{2}-\d{2})/g, "$1");
// create new entry for today
var hat = headerandtext(note);
var hat = headerandtext(content);
var today = timestamp().substr(0,10);
if (!hat.text.startsWith(today) && !hat.text.startsWith("\n" + today))
{
note.content = hat.header + "\n" + today + "\n\n" + hat.text;
note.pos = hat.header.length + 12;
content = hat.header + "\n" + today + "\n\n" + hat.text;
item.pos = hat.header.length + 12;
}
}
}*/
/*if (settings.autosorttodo && istodo(note))
{