Compare commits

...

10 Commits

Author SHA1 Message Date
quenousimporte d767f80874 refactor remove useless window
change logs
2024-02-16 15:04:36 +01:00
quenousimporte c425725423 refactor remove useless return statements 2024-02-16 11:25:37 +01:00
quenousimporte 77ddc4907f fix encryption 2024-02-16 11:03:08 +01:00
quenousimporte 717b2a1bce change keep legacy storage just in case 2024-02-16 09:46:15 +01:00
quenousimporte e8c5d38b3a change messages 2024-02-16 09:39:19 +01:00
quenousimporte 682a18132d fix exception at startup sync 2024-02-16 09:31:54 +01:00
quenousimporte e896275724 fix reload 2024-02-15 22:39:50 +01:00
quenousimporte e0a365b612 add logs
fix reload of current note
2024-02-15 22:32:17 +01:00
quenousimporte a1c0033e26 fix save to server 2024-02-15 22:11:56 +01:00
quenousimporte 4f33d65317 fix fetch modified notes from server 2024-02-15 16:39:31 +01:00
1 changed files with 184 additions and 160 deletions

268
main.js
View File

@ -31,6 +31,7 @@ var workerid = null;
var backup = "";
var settings = null;
var tags = null;
var pending = {};
var commands = [
{
@ -58,7 +59,7 @@ var commands = [
},
{
hint: "Force save",
action: serialize,
action: flush,
shortcut: "ctrl+s"
},
{
@ -284,7 +285,7 @@ function seteditorcontent(content)
{
md.value = content;
applycolors();
serialize();
flush();
resize();
}
@ -351,6 +352,46 @@ function currentrange()
};
}
function pushitem(key, value)
{
unsavedmark.hidden = false;
pending[key] = true;
var name = metadata && metadata[key] ? metadata[key].title : key;
queryremote({action: "push", name: key, data: value})
.then( () =>
{
console.log("'" + name + "' pushed to server");
delete pending[key];
})
.catch( err =>
{
console.error("error while pushing '" + name + "': " + err);
showtemporaryinfo(msg);
setTimeout( () =>
{
serialize(key, value);
}, settings.savedelay);
})
.finally( () =>
{
unsavedmark.hidden = Object.keys(pending) == 0;
});
}
function serialize(key, value)
{
localStorage.setItem(key, value);
var name = metadata && metadata[key] ? metadata[key].title : key;
console.log("'" + name + "' serialized locally");
if (settings.sync)
{
pushitem(key, value);
}
}
function createsubnote()
{
var title = prompt("Subnote tite:");
@ -374,7 +415,7 @@ function createsubnote()
var range = currentrange();
var content = getrangecontent(range);
guid = createnote(title);
localStorage.setItem(guid, content);
serialize(guid, content);
seteditorcontent(md.value.substring(0, range.start)
+ "[[" + title + "]]"
@ -445,6 +486,7 @@ function showinfo()
showtemporaryinfo(
[
"sync: " + (settings.sync ? "en" : "dis") + "abled",
"pending: " + Object.keys(pending).join(),
"title: " + title.value,
"line count: " + md.value.split("\n").length,
"word count: " + getwords(),
@ -457,7 +499,7 @@ function showinfo()
function savesettings()
{
window.localStorage.setItem("settings", JSON.stringify(settings));
localStorage.setItem("settings", JSON.stringify(settings));
}
function descendants(note)
@ -1044,9 +1086,9 @@ function inserttodo(text)
}
else
{
localStorage.setItem(guid, content);
serialize(guid, content);
metadata[guid].lastchanged = Date.now();
serialize();
flush();
}
}
@ -1118,7 +1160,7 @@ function createnote(title)
};
metadata[guid] = item;
serializeindex()
localStorage.setItem(guid, content);
serialize(guid, content);
return guid;
}
@ -1202,10 +1244,10 @@ function loadsettings()
function checksaved()
{
/*if (!saved)
if (!unsavedmark.hidden)
{
return "not saved";
}*/
}
}
function initsnippets()
@ -1262,7 +1304,8 @@ function migratelegacystorage()
var legacy = localStorage.getItem("data");
if (legacy)
{
alert("Old data model detected. Click ok to migrate, or close app.")
alert("Hey! I am about to migrate your notes to the brand new data model. No worries, I will keep a backup somewhere in case things go wrong. Take a deep breath, and click ok when you're ready.");
localStorage.setItem("legacy", legacy);
legacy = JSON.parse(legacy);
var index = {};
legacy.reverse().forEach( (note, i) =>
@ -1291,6 +1334,18 @@ function pushall()
return Promise.all(list);
}
function fetchandserialize(guid)
{
var name = (metadata && metadata[guid]) ? metadata[guid] : guid;
console.log("fetching '" + name + "'");
return queryremote({action: "fetch", name: guid})
.then(content =>
{
localStorage.setItem(guid, content);
console.log("'" + name + "' fetched and serialized");
});
}
function fetch()
{
return new Promise(function(resolve)
@ -1311,14 +1366,37 @@ function fetch()
{
if (!filecontent)
{
return pushall();
if (confirm("No remote index found. Init remote now?"))
{
pushall().then(resolve);
}
else
{
// compare and fetch modified and serialize all that
resolve();
}
}
else
{
var localindex = JSON.parse(localStorage.getItem("index"));
var remoteindex = JSON.parse(filecontent);
}).catch(err =>
var list = [];
Object.keys(remoteindex).forEach(guid =>
{
if (!localindex[guid] || localindex[guid].lastchanged < remoteindex[guid].lastchanged)
{
list.push(fetchandserialize(guid));
}
});
Promise.all(list).then( () =>
{
localStorage.setItem("index", JSON.stringify(remoteindex));
resolve();
});
}
})
.catch(err =>
{
if (err == "error: authent")
{
@ -1347,6 +1425,7 @@ function init()
window.onbeforeunload = checksaved;
window.onclick = focuseditor;
title.value = "";
initsnippets();
@ -1369,16 +1448,36 @@ function init()
});
}
function encryptdata(params)
{
if (params.data)
{
return encryptstring(params.data)
.then(encrypted =>
{
params.data = encrypted;
return Promise.resolve(params);
});
}
else
{
return Promise.resolve(params);
}
}
function queryremote(params)
{
return new Promise( (apply, failed) => {
params.password = settings.password;
return encryptdata(params)
.then(encparams =>
{
return new Promise ( (resolve, reject) =>
{
encparams.password = settings.password;
var paramlist = [];
for (var i in params)
for (var i in encparams)
{
paramlist.push(i + "=" + encodeURIComponent(params[i]));
paramlist.push(i + "=" + encodeURIComponent(encparams[i]));
}
var xhr = new XMLHttpRequest();
@ -1387,14 +1486,14 @@ function queryremote(params)
xhr.onerror = function()
{
failed("XMLHttpRequest error");
reject("XMLHttpRequest error");
}
xhr.onload = function()
{
if (xhr.status !== 200)
{
failed("Http status " + xhr.status);
reject("Http status " + xhr.status);
}
else
{
@ -1403,20 +1502,21 @@ function queryremote(params)
{
if (decrypted.startsWith("error: "))
{
failed(decrypted);
reject(decrypted);
}
else
{
apply(decrypted);
resolve(decrypted);
}
});
}
}
var paramstring = paramlist.join("&");
console.log("http request length: " + formatsize(paramstring.length));
console.log("http '" + encparams.action + "' request length: " + formatsize(paramstring.length));
xhr.send(paramstring);
});
});
}
function getlinesrange()
@ -1800,12 +1900,12 @@ function postpone()
});
}
function serialize()
function flush()
{
clearTimeout(workerid);
workerid = null;
var guid = getguid(window.title.value);
var guid = getguid(title.value);
if (guid)
{
var item = metadata[guid];
@ -1814,8 +1914,8 @@ function serialize()
item.header = indexheader(md.value);
item.lastchanged = Date.now();
serializeindex()
localStorage.setItem(guid, md.value);
serializeindex();
serialize(guid, md.value);
}
else if (title.value == "settings.json")
{
@ -1827,96 +1927,10 @@ function serialize()
{
localStorage.setItem("pgpkeys", md.value);
}
console.log("data serialized");
unsavedmark.hidden = Object.keys(pending).length == 0;
}
/*function save()
{
return new Promise(function(resolve, reject)
{
clearTimeout(workerid);
if (title.value == "settings.json")
{
settings = JSON.parse(md.value);
savesettings();
loadsettings();
setsaved();
resolve();
}
else if (title.value == "pgpkeys")
{
localStorage.setItem("pgpkeys", md.value);
setsaved();
resolve();
}
else if (pending)
{
console.log("pending query: save cancelled");
reject();
}
else if (saved)
{
console.log("nothing to save");
reject();
}
else
{
var content = md.value;
if ((content == "" && backup != "") || content == "null" || content == "undefined")
{
showtemporaryinfo("Invalid content '" + content + "', file '" + title.value + "' not saved");
reject();
}
else
{
serialize();
if (settings.sync)
{
var datatosend = JSON.stringify(localdata);
return encryptstring(datatosend)
.then(encrypted =>
{
console.log("sending data to php server...");
pending = true;
return queryremote({action: "push", data: encrypted})
})
.then(() =>
{
console.log("...data saved on server");
setsaved();
})
.catch(remotecallfailed)
.finally(() =>
{
pending = false;
if (content != md.value)
{
console.log("but content changed: will save again");
return datachanged();
}
else if (!saved)
{
console.log("save failed. Data unsaved on server. Will retry.");
return datachanged();
}
else
{
resolve();
}
});
}
else
{
setsaved();
resolve();
}
}
}
});
}*/
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&amp;")
@ -2174,10 +2188,14 @@ function applycolors(currentonly)
function editorinput()
{
unsavedmark.hidden = false;
// criteria to improve. Or redraw only after?
var multiline = md.value.substring(md.selectionStart, md.selectionEnd).includes("\n");
applycolors(!multiline && event.data && (event.inputType == "insertText" || event.inputType == "deleteContentBackward" || event.inputType == "deleteContentForward"));
postpone().then(serialize);
// todo: fix if current note change during postponing, the wrong one will be saved. Or prevent binding another note?
postpone().then(flush);
resize();
}
@ -2191,13 +2209,13 @@ function timestamp()
function quicknewnote()
{
serialize();
flush();
loadnote(timestamp());
}
function startnewnote()
{
serialize();
flush();
var title = prompt("Note title: ", timestamp());
if (title)
{
@ -2385,7 +2403,7 @@ function restoredeleted()
function serializeindex()
{
localStorage.setItem("index", JSON.stringify(metadata));
serialize("index", JSON.stringify(metadata));
}
function deletenote(title)
@ -2614,8 +2632,8 @@ function renameinternallinks(from, to)
var newcontent = content.replaceAll("[[" + from + "]]", "[[" + to + "]]");
if (content != newcontent)
{
localStorage.setItem(guid, newcontent);
if (item.title == window.title.value)
serialize(guid, newcontent);
if (item.title == title.value)
{
seteditorcontent(newcontent);
}
@ -2639,7 +2657,7 @@ function ontitlechange()
previoustitle = title.value;
metadata[guid].title = title.value;
setwindowtitle();
serialize();
flush();
if (!settings.titlebydefault)
{
@ -2821,6 +2839,12 @@ function togglepreviewwithsubs()
function bind(title, content, pos)
{
if (workerid)
{
showtemporaryinfo("Cannot open '" + title + "' because current note not yet serialized");
return;
}
previoustitle = title;
backup = content;