Compare commits
No commits in common. "d767f80874cd6468e2fd186091d270333c0c2f2f" and "5f049cf587d824bd836e78bce22795538f462c6d" have entirely different histories.
d767f80874
...
5f049cf587
268
main.js
268
main.js
|
@ -31,7 +31,6 @@ var workerid = null;
|
|||
var backup = "";
|
||||
var settings = null;
|
||||
var tags = null;
|
||||
var pending = {};
|
||||
|
||||
var commands = [
|
||||
{
|
||||
|
@ -59,7 +58,7 @@ var commands = [
|
|||
},
|
||||
{
|
||||
hint: "Force save",
|
||||
action: flush,
|
||||
action: serialize,
|
||||
shortcut: "ctrl+s"
|
||||
},
|
||||
{
|
||||
|
@ -285,7 +284,7 @@ function seteditorcontent(content)
|
|||
{
|
||||
md.value = content;
|
||||
applycolors();
|
||||
flush();
|
||||
serialize();
|
||||
resize();
|
||||
}
|
||||
|
||||
|
@ -352,46 +351,6 @@ 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:");
|
||||
|
@ -415,7 +374,7 @@ function createsubnote()
|
|||
var range = currentrange();
|
||||
var content = getrangecontent(range);
|
||||
guid = createnote(title);
|
||||
serialize(guid, content);
|
||||
localStorage.setItem(guid, content);
|
||||
|
||||
seteditorcontent(md.value.substring(0, range.start)
|
||||
+ "[[" + title + "]]"
|
||||
|
@ -486,7 +445,6 @@ 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(),
|
||||
|
@ -499,7 +457,7 @@ function showinfo()
|
|||
|
||||
function savesettings()
|
||||
{
|
||||
localStorage.setItem("settings", JSON.stringify(settings));
|
||||
window.localStorage.setItem("settings", JSON.stringify(settings));
|
||||
}
|
||||
|
||||
function descendants(note)
|
||||
|
@ -1086,9 +1044,9 @@ function inserttodo(text)
|
|||
}
|
||||
else
|
||||
{
|
||||
serialize(guid, content);
|
||||
localStorage.setItem(guid, content);
|
||||
metadata[guid].lastchanged = Date.now();
|
||||
flush();
|
||||
serialize();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1160,7 +1118,7 @@ function createnote(title)
|
|||
};
|
||||
metadata[guid] = item;
|
||||
serializeindex()
|
||||
serialize(guid, content);
|
||||
localStorage.setItem(guid, content);
|
||||
return guid;
|
||||
}
|
||||
|
||||
|
@ -1244,10 +1202,10 @@ function loadsettings()
|
|||
|
||||
function checksaved()
|
||||
{
|
||||
if (!unsavedmark.hidden)
|
||||
/*if (!saved)
|
||||
{
|
||||
return "not saved";
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
function initsnippets()
|
||||
|
@ -1304,8 +1262,7 @@ function migratelegacystorage()
|
|||
var legacy = localStorage.getItem("data");
|
||||
if (legacy)
|
||||
{
|
||||
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);
|
||||
alert("Old data model detected. Click ok to migrate, or close app.")
|
||||
legacy = JSON.parse(legacy);
|
||||
var index = {};
|
||||
legacy.reverse().forEach( (note, i) =>
|
||||
|
@ -1334,18 +1291,6 @@ 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)
|
||||
|
@ -1366,37 +1311,14 @@ function fetch()
|
|||
{
|
||||
if (!filecontent)
|
||||
{
|
||||
if (confirm("No remote index found. Init remote now?"))
|
||||
{
|
||||
pushall().then(resolve);
|
||||
return pushall();
|
||||
}
|
||||
else
|
||||
{
|
||||
resolve();
|
||||
// compare and fetch modified and serialize all that
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var localindex = JSON.parse(localStorage.getItem("index"));
|
||||
var remoteindex = JSON.parse(filecontent);
|
||||
|
||||
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 =>
|
||||
}).catch(err =>
|
||||
{
|
||||
if (err == "error: authent")
|
||||
{
|
||||
|
@ -1425,7 +1347,6 @@ function init()
|
|||
|
||||
window.onbeforeunload = checksaved;
|
||||
window.onclick = focuseditor;
|
||||
title.value = "";
|
||||
|
||||
initsnippets();
|
||||
|
||||
|
@ -1448,36 +1369,16 @@ 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 encryptdata(params)
|
||||
.then(encparams =>
|
||||
{
|
||||
return new Promise ( (resolve, reject) =>
|
||||
{
|
||||
encparams.password = settings.password;
|
||||
return new Promise( (apply, failed) => {
|
||||
|
||||
params.password = settings.password;
|
||||
|
||||
var paramlist = [];
|
||||
for (var i in encparams)
|
||||
for (var i in params)
|
||||
{
|
||||
paramlist.push(i + "=" + encodeURIComponent(encparams[i]));
|
||||
paramlist.push(i + "=" + encodeURIComponent(params[i]));
|
||||
}
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
|
@ -1486,14 +1387,14 @@ function queryremote(params)
|
|||
|
||||
xhr.onerror = function()
|
||||
{
|
||||
reject("XMLHttpRequest error");
|
||||
failed("XMLHttpRequest error");
|
||||
}
|
||||
|
||||
xhr.onload = function()
|
||||
{
|
||||
if (xhr.status !== 200)
|
||||
{
|
||||
reject("Http status " + xhr.status);
|
||||
failed("Http status " + xhr.status);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -1502,21 +1403,20 @@ function queryremote(params)
|
|||
{
|
||||
if (decrypted.startsWith("error: "))
|
||||
{
|
||||
reject(decrypted);
|
||||
failed(decrypted);
|
||||
}
|
||||
else
|
||||
{
|
||||
resolve(decrypted);
|
||||
apply(decrypted);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
var paramstring = paramlist.join("&");
|
||||
console.log("http '" + encparams.action + "' request length: " + formatsize(paramstring.length));
|
||||
console.log("http request length: " + formatsize(paramstring.length));
|
||||
xhr.send(paramstring);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getlinesrange()
|
||||
|
@ -1900,12 +1800,12 @@ function postpone()
|
|||
});
|
||||
}
|
||||
|
||||
function flush()
|
||||
function serialize()
|
||||
{
|
||||
clearTimeout(workerid);
|
||||
workerid = null;
|
||||
|
||||
var guid = getguid(title.value);
|
||||
var guid = getguid(window.title.value);
|
||||
if (guid)
|
||||
{
|
||||
var item = metadata[guid];
|
||||
|
@ -1914,8 +1814,8 @@ function flush()
|
|||
item.header = indexheader(md.value);
|
||||
item.lastchanged = Date.now();
|
||||
|
||||
serializeindex();
|
||||
serialize(guid, md.value);
|
||||
serializeindex()
|
||||
localStorage.setItem(guid, md.value);
|
||||
}
|
||||
else if (title.value == "settings.json")
|
||||
{
|
||||
|
@ -1927,10 +1827,96 @@ function flush()
|
|||
{
|
||||
localStorage.setItem("pgpkeys", md.value);
|
||||
}
|
||||
|
||||
unsavedmark.hidden = Object.keys(pending).length == 0;
|
||||
console.log("data serialized");
|
||||
}
|
||||
|
||||
/*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, "&")
|
||||
|
@ -2188,14 +2174,10 @@ 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"));
|
||||
|
||||
// todo: fix if current note change during postponing, the wrong one will be saved. Or prevent binding another note?
|
||||
postpone().then(flush);
|
||||
postpone().then(serialize);
|
||||
resize();
|
||||
}
|
||||
|
||||
|
@ -2209,13 +2191,13 @@ function timestamp()
|
|||
|
||||
function quicknewnote()
|
||||
{
|
||||
flush();
|
||||
serialize();
|
||||
loadnote(timestamp());
|
||||
}
|
||||
|
||||
function startnewnote()
|
||||
{
|
||||
flush();
|
||||
serialize();
|
||||
var title = prompt("Note title: ", timestamp());
|
||||
if (title)
|
||||
{
|
||||
|
@ -2403,7 +2385,7 @@ function restoredeleted()
|
|||
|
||||
function serializeindex()
|
||||
{
|
||||
serialize("index", JSON.stringify(metadata));
|
||||
localStorage.setItem("index", JSON.stringify(metadata));
|
||||
}
|
||||
|
||||
function deletenote(title)
|
||||
|
@ -2632,8 +2614,8 @@ function renameinternallinks(from, to)
|
|||
var newcontent = content.replaceAll("[[" + from + "]]", "[[" + to + "]]");
|
||||
if (content != newcontent)
|
||||
{
|
||||
serialize(guid, newcontent);
|
||||
if (item.title == title.value)
|
||||
localStorage.setItem(guid, newcontent);
|
||||
if (item.title == window.title.value)
|
||||
{
|
||||
seteditorcontent(newcontent);
|
||||
}
|
||||
|
@ -2657,7 +2639,7 @@ function ontitlechange()
|
|||
previoustitle = title.value;
|
||||
metadata[guid].title = title.value;
|
||||
setwindowtitle();
|
||||
flush();
|
||||
serialize();
|
||||
|
||||
if (!settings.titlebydefault)
|
||||
{
|
||||
|
@ -2839,12 +2821,6 @@ function togglepreviewwithsubs()
|
|||
|
||||
function bind(title, content, pos)
|
||||
{
|
||||
if (workerid)
|
||||
{
|
||||
showtemporaryinfo("Cannot open '" + title + "' because current note not yet serialized");
|
||||
return;
|
||||
}
|
||||
|
||||
previoustitle = title;
|
||||
backup = content;
|
||||
|
||||
|
|
Loading…
Reference in New Issue