added: autosort todo upon opening

This commit is contained in:
quenousimporte 2023-12-12 09:57:29 +01:00
parent 263af2a863
commit 45b6b9be4b
1 changed files with 52 additions and 35 deletions

87
main.js
View File

@ -13,7 +13,8 @@ var defaultsettings =
password: "", password: "",
sync: false, sync: false,
tagsinlists: true, tagsinlists: true,
uselinkpopup: true uselinkpopup: true,
autosorttodo: true
}; };
//builtin //builtin
@ -264,7 +265,7 @@ var commands = [
}, },
{ {
hint: "Sort todo.txt list", hint: "Sort todo.txt list",
action: sorttodotxt action: sortcurrentastodo
}, },
{ {
hint: "Sort text", hint: "Sort text",
@ -2400,45 +2401,56 @@ function searchandloadnote()
}); });
} }
function currentistodo() function istodo(note)
{ {
return currentnote.title.includes("todo") || gettags(currentnote).includes("todo"); return note.title.includes("todo") || gettags(note).includes("todo");
} }
function sorttodotxt() function currentistodo()
{
return istodo(currentnote);
}
function sorttodotxt(note)
{
var hat = headerandtext(note);
var olditems = hat.text.split("\n");
var prio = [];
var std = [];
var done = [];
olditems.forEach(item =>
{
if (item)
{
if (item.startsWith("("))
{
item = item.substring(4);
var priority = String.fromCharCode(65 + prio.length);
prio.push(`(${priority}) ${item}`);
}
else if (item.startsWith("x "))
{
done.push(item);
}
else
{
std.push(item);
}
}
});
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");
}
function sortcurrentastodo()
{ {
if (currentistodo()) if (currentistodo())
{ {
var hat = headerandtext(currentnote); sorttodotxt(currentnote);
var olditems = hat.text.split("\n"); seteditorcontent(currentnote.content);
var prio = [];
var std = [];
var done = [];
olditems.forEach(item =>
{
if (item)
{
if (item.startsWith("("))
{
item = item.substring(4);
var priority = String.fromCharCode(65 + prio.length);
prio.push(`(${priority}) ${item}`);
}
else if (item.startsWith("x "))
{
done.push(item);
}
else
{
std.push(item);
}
}
});
prio = prio.sort((a,b) => a.localeCompare(b));
done = done.sort((a,b) => a.localeCompare(b));
var all = prio.concat(std).concat(done);
seteditorcontent(hat.header + all.join("\n"));
} }
} }
@ -3003,6 +3015,11 @@ function loadnote(name)
} }
} }
if (settings.autosorttodo && istodo(note))
{
sorttodotxt(note);
}
bindfile(note); bindfile(note);
stat.cur.q = 0; stat.cur.q = 0;