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