added: light mode for old device

changed: shortcuts
refactor: serialization and push to server
changed: add current tag filter in window title
This commit is contained in:
quenousimporte 2023-01-21 21:05:39 +01:00
parent af55cd2d1d
commit cbd45a13f8
1 changed files with 88 additions and 69 deletions

155
main.js
View File

@ -9,7 +9,8 @@ var defaultsettings =
foldmarkstart: 22232, foldmarkstart: 22232,
defaultpreviewinsplit: false, defaultpreviewinsplit: false,
enablefolding: false, enablefolding: false,
tagautocomplete: false tagautocomplete: false,
light: false
}; };
//builtin //builtin
@ -76,7 +77,7 @@ var commands = [
action: loadquicknote action: loadquicknote
}, },
{ {
shortcut: "ctrl+shift+F", shortcut: "ctrl+g",
hint: "Find in notes", hint: "Find in notes",
savedonly: true, savedonly: true,
action: showgrep action: showgrep
@ -150,7 +151,7 @@ var commands = [
}*/, }*/,
{ {
hint: "Force push", hint: "Force push",
action: push, action: pushtoremote,
shortcut: "ctrl+s" shortcut: "ctrl+s"
}, },
{ {
@ -214,7 +215,9 @@ var commands = [
}, },
{ {
hint: "Add tag filter", hint: "Add tag filter",
action: addtagfilter action: addtagfilter,
shortcut: "ctrl+shift+F",
savedonly: true
}]; }];
var snippets = [ var snippets = [
@ -260,12 +263,14 @@ function addtagfilter()
{ {
currenttag = t; currenttag = t;
command.hint = "Remove tag filter '" + currenttag + "'"; command.hint = "Remove tag filter '" + currenttag + "'";
setwindowtitle();
}); });
} }
else else
{ {
currenttag = ""; currenttag = "";
command.hint = "Add tag filter"; command.hint = "Add tag filter";
setwindowtitle();
} }
} }
@ -403,12 +408,6 @@ function editsettings()
function showtemporaryinfo(str) function showtemporaryinfo(str)
{ {
/*var prev = mark.textContent;
mark.textContent = str;
setTimeout(function()
{
mark.textContent = prev;
}, 5000);*/
alert(str); alert(str);
} }
@ -550,26 +549,44 @@ function downloadnote()
download(currentnote.title + ".md", getnotecontent()); download(currentnote.title + ".md", getnotecontent());
} }
function serialize() function delay()
{ {
putontop(); return new Promise(function(apply)
window.localStorage.setItem(currentvault, JSON.stringify(localdata));
if (currentnote.title == "settings.json")
{
window.localStorage.setItem("settings", getnotecontent());
}
if (isremote())
{ {
clearTimeout(timeoutid); clearTimeout(timeoutid);
timeoutid = setTimeout(push, settings.savedelay); timeoutid = setTimeout(apply, settings.savedelay);
});
}
function save()
{
var content = getnotecontent();
if ((content == "" && backup != "") || content == "null" || content == "undefined")
{
console.warn("Invalid content '" + content + "', file '" + currentnote.title + "' not saved");
} }
else else
{ {
marksaved(); currentnote.content = content;
window.localStorage.setItem(currentvault, JSON.stringify(localdata));
if (currentnote.title == "settings.json")
{
window.localStorage.setItem("settings", content);
}
console.log("data serialized in local storage")
if (isremote())
{
pushtoremote();
}
else
{
marksaved();
}
} }
} }
function remotecallfailed(error) function remotecallfailed(error)
@ -693,7 +710,6 @@ function init()
if (isremote()) if (isremote())
{ {
markunsaved("*");
queryremote({action: "fetch"}) queryremote({action: "fetch"})
.then(data => .then(data =>
{ {
@ -731,13 +747,14 @@ function togglepassword()
password.focus(); password.focus();
} }
function push() function pushtoremote()
{ {
if (!isremote()) if (!isremote())
{ {
console.log("local vault, no push"); console.log("local vault, no push");
return; return;
} }
console.log("sending data to php server");
if (localdata) if (localdata)
{ {
@ -745,15 +762,15 @@ function push()
var content = getnotecontent(); var content = getnotecontent();
queryremote({action: "push", data: JSON.stringify(localdata)}) queryremote({action: "push", data: JSON.stringify(localdata)})
.then(() => { .then(() => {
console.log("Data sent to server"); console.log("data saved on server");
if (getnotecontent() == content) if (getnotecontent() == content)
{ {
marksaved(); marksaved();
} }
else else
{ {
console.warn("Content changed in the meantime, mark as not saved"); console.warn("Content changed in the meantime, keep as unsaved");
timeoutid = setTimeout(push, settings.savedelay); save();
} }
}) })
.catch(remotecallfailed); .catch(remotecallfailed);
@ -1193,7 +1210,6 @@ function searchinlist(list)
filteredlist.innerHTML = ""; filteredlist.innerHTML = "";
filter.value = ""; filter.value = "";
filter.focus();
list.forEach(item => list.forEach(item =>
{ {
@ -1220,6 +1236,8 @@ function searchinlist(list)
selectitem(selected ? selected.textContent : filter.value); selectitem(selected ? selected.textContent : filter.value);
} }
} }
filter.focus();
}); });
} }
@ -1282,50 +1300,42 @@ function putontop()
function notecontentchanged() function notecontentchanged()
{ {
markunsaved("*"); if (!settings.light)
// check snippets and autocomplete
// should we move this on key down? to cancel when backspace?
if (before(2) == "[[")
{ {
searchautocomplete(); // check snippets and autocomplete
} // should we move this on key down? to cancel when backspace?
else if (settings.tagautocomplete && md.value.substring(0, getpos()).split("\n").pop().startsWith("tags: ")) if (before(2) == "[[")
{
// search in tags list
if (before(2) == ", ")
{ {
console.log(event.key); searchautocomplete();
tagslist() }
.then(tag => else if (settings.tagautocomplete && md.value.substring(0, getpos()).split("\n").pop().startsWith("tags: "))
{
// search in tags list
if (before(2) == ", ")
{ {
insert(tag); console.log(event.key);
notecontentchanged(); tagslist()
md.focus(); .then(tag =>
}) {
insert(tag);
notecontentchanged();
md.focus();
})
}
} }
} else
else
{
var snippet = snippets.find(s => before(s.command.length) == s.command);
if (snippet)
{ {
insert(snippet.insert, snippet.cursor, snippet.command.length); var snippet = snippets.find(s => before(s.command.length) == s.command);
if (snippet)
{
insert(snippet.insert, snippet.cursor, snippet.command.length);
}
} }
} }
resize();
// save resize();
var content = getnotecontent(); markunsaved("*");
if ((content == "" && backup != "") || content == "null" || content == "undefined") delay().then(save);
{
console.warn("Invalid content '" + content + "', file '" + currentnote.title + "' not saved");
}
else
{
currentnote.content = content;
serialize();
}
} }
function loadtodo() function loadtodo()
@ -1434,7 +1444,7 @@ function rename(newname)
currentnote.title = newname; currentnote.title = newname;
markunsaved("*"); markunsaved("*");
serialize(); save();
return ""; return "";
} }
@ -1533,6 +1543,15 @@ function mainkeydownhandler()
}); });
} }
function setwindowtitle()
{
document.title = title.value;
if (currenttag)
{
document.title = "tag:" + currenttag + " - " + document.title;
}
}
function ontitlechange() function ontitlechange()
{ {
var oldname = currentnote.title; var oldname = currentnote.title;
@ -1541,7 +1560,7 @@ function ontitlechange()
if (!error) if (!error)
{ {
console.log("'" + oldname + "' renamed to '" + currentnote.title + "'"); console.log("'" + oldname + "' renamed to '" + currentnote.title + "'");
document.title = title.value; setwindowtitle();
} }
else else
{ {
@ -1640,7 +1659,7 @@ function bindfile(note)
backup = note.content; backup = note.content;
currentnote = note; currentnote = note;
title.value = note.title; title.value = note.title;
document.title = note.title; setwindowtitle();
setnotecontent(note.content || ""); setnotecontent(note.content || "");
preview.innerHTML = md2html(getnotecontent()); preview.innerHTML = md2html(getnotecontent());