mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
initial decallbackization and promisification of frontend
This commit is contained in:
parent
ac0e5ada6e
commit
702d47e6b0
@ -1,8 +1,10 @@
|
|||||||
let globalEncryptionCallback = null;
|
let globalEncryptionDeferred = null;
|
||||||
|
|
||||||
|
function handleEncryption(requireEncryption, modal) {
|
||||||
|
const dfd = $.Deferred();
|
||||||
|
|
||||||
function handleEncryption(requireEncryption, modal, callback) {
|
|
||||||
if (requireEncryption && globalDataKey === null) {
|
if (requireEncryption && globalDataKey === null) {
|
||||||
globalEncryptionCallback = callback;
|
globalEncryptionDeferred = dfd;
|
||||||
|
|
||||||
$("#encryption-password-dialog").dialog({
|
$("#encryption-password-dialog").dialog({
|
||||||
modal: modal,
|
modal: modal,
|
||||||
@ -16,8 +18,10 @@ function handleEncryption(requireEncryption, modal, callback) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
callback();
|
dfd.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return dfd.promise();
|
||||||
}
|
}
|
||||||
|
|
||||||
let globalDataKey = null;
|
let globalDataKey = null;
|
||||||
@ -87,10 +91,10 @@ $("#encryption-password-form").submit(() => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (globalEncryptionCallback !== null) {
|
if (globalEncryptionDeferred !== null) {
|
||||||
globalEncryptionCallback();
|
globalEncryptionDeferred.resolve();
|
||||||
|
|
||||||
globalEncryptionCallback = null;
|
globalEncryptionDeferred = null;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(reason => {
|
.catch(reason => {
|
||||||
@ -225,69 +229,67 @@ function encryptNote(note) {
|
|||||||
return note;
|
return note;
|
||||||
}
|
}
|
||||||
|
|
||||||
function encryptNoteAndSendToServer() {
|
async function encryptNoteAndSendToServer() {
|
||||||
handleEncryption(true, true, () => {
|
await handleEncryption(true, true);
|
||||||
const note = globalCurrentNote;
|
|
||||||
|
|
||||||
updateNoteFromInputs(note);
|
const note = globalCurrentNote;
|
||||||
|
|
||||||
encryptNote(note);
|
updateNoteFromInputs(note);
|
||||||
|
|
||||||
saveNoteToServer(note);
|
encryptNote(note);
|
||||||
|
|
||||||
changeEncryptionOnNoteHistory(note.detail.note_id, true);
|
await saveNoteToServer(note);
|
||||||
|
|
||||||
setNoteBackgroundIfEncrypted(note);
|
await changeEncryptionOnNoteHistory(note.detail.note_id, true);
|
||||||
});
|
|
||||||
|
setNoteBackgroundIfEncrypted(note);
|
||||||
}
|
}
|
||||||
|
|
||||||
function changeEncryptionOnNoteHistory(noteId, encrypt) {
|
async function changeEncryptionOnNoteHistory(noteId, encrypt) {
|
||||||
$.ajax({
|
const result = await $.ajax({
|
||||||
url: baseApiUrl + 'notes-history/' + noteId + "?encryption=" + (encrypt ? 0 : 1),
|
url: baseApiUrl + 'notes-history/' + noteId + "?encryption=" + (encrypt ? 0 : 1),
|
||||||
type: 'GET',
|
type: 'GET',
|
||||||
success: result => {
|
|
||||||
for (const row of result) {
|
|
||||||
if (encrypt) {
|
|
||||||
row.note_title = encryptString(row.note_title);
|
|
||||||
row.note_text = encryptString(row.note_text);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
row.note_title = decryptString(row.note_title);
|
|
||||||
row.note_text = decryptString(row.note_text);
|
|
||||||
}
|
|
||||||
|
|
||||||
row.encryption = encrypt ? 1 : 0;
|
|
||||||
|
|
||||||
$.ajax({
|
|
||||||
url: baseApiUrl + 'notes-history',
|
|
||||||
type: 'PUT',
|
|
||||||
contentType: 'application/json',
|
|
||||||
data: JSON.stringify(row),
|
|
||||||
success: result => console.log('Note history ' + row.note_history_id + ' de/encrypted'),
|
|
||||||
error: () => alert("Error de/encrypting note history.")
|
|
||||||
});
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
error: () => alert("Error getting note history.")
|
error: () => alert("Error getting note history.")
|
||||||
});
|
});
|
||||||
|
|
||||||
|
for (const row of result) {
|
||||||
|
if (encrypt) {
|
||||||
|
row.note_title = encryptString(row.note_title);
|
||||||
|
row.note_text = encryptString(row.note_text);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
row.note_title = decryptString(row.note_title);
|
||||||
|
row.note_text = decryptString(row.note_text);
|
||||||
|
}
|
||||||
|
|
||||||
|
row.encryption = encrypt ? 1 : 0;
|
||||||
|
|
||||||
|
await $.ajax({
|
||||||
|
url: baseApiUrl + 'notes-history',
|
||||||
|
type: 'PUT',
|
||||||
|
contentType: 'application/json',
|
||||||
|
data: JSON.stringify(row),
|
||||||
|
error: () => alert("Error de/encrypting note history.")
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('Note history ' + row.note_history_id + ' de/encrypted');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function decryptNoteAndSendToServer() {
|
async function decryptNoteAndSendToServer() {
|
||||||
handleEncryption(true, true, () => {
|
await handleEncryption(true, true);
|
||||||
const note = globalCurrentNote;
|
|
||||||
|
|
||||||
updateNoteFromInputs(note);
|
const note = globalCurrentNote;
|
||||||
|
|
||||||
note.detail.encryption = 0;
|
updateNoteFromInputs(note);
|
||||||
|
|
||||||
saveNoteToServer(note);
|
note.detail.encryption = 0;
|
||||||
|
|
||||||
changeEncryptionOnNoteHistory(note.detail.note_id, false);
|
await saveNoteToServer(note);
|
||||||
|
|
||||||
setNoteBackgroundIfEncrypted(note);
|
await changeEncryptionOnNoteHistory(note.detail.note_id, false);
|
||||||
});
|
|
||||||
|
setNoteBackgroundIfEncrypted(note);
|
||||||
}
|
}
|
||||||
|
|
||||||
function decryptNoteIfNecessary(note) {
|
function decryptNoteIfNecessary(note) {
|
||||||
@ -301,58 +303,58 @@ function decryptNote(note) {
|
|||||||
note.detail.note_text = decryptString(note.detail.note_text);
|
note.detail.note_text = decryptString(note.detail.note_text);
|
||||||
}
|
}
|
||||||
|
|
||||||
function encryptSubTree(noteId) {
|
async function encryptSubTree(noteId) {
|
||||||
handleEncryption(true, true, () => {
|
await handleEncryption(true, true);
|
||||||
updateSubTreeRecursively(noteId, note => {
|
|
||||||
if (note.detail.encryption === null || note.detail.encryption === 0) {
|
|
||||||
encryptNote(note);
|
|
||||||
|
|
||||||
note.detail.encryption = 1;
|
updateSubTreeRecursively(noteId, note => {
|
||||||
|
if (note.detail.encryption === null || note.detail.encryption === 0) {
|
||||||
|
encryptNote(note);
|
||||||
|
|
||||||
return true;
|
note.detail.encryption = 1;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
note => {
|
||||||
|
if (note.detail.note_id === globalCurrentNote.detail.note_id) {
|
||||||
|
loadNoteToEditor(note.detail.note_id);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return false;
|
setTreeBasedOnEncryption(note);
|
||||||
}
|
}
|
||||||
},
|
});
|
||||||
note => {
|
|
||||||
if (note.detail.note_id === globalCurrentNote.detail.note_id) {
|
|
||||||
loadNoteToEditor(note.detail.note_id);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
setTreeBasedOnEncryption(note);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
alert("Encryption finished.");
|
alert("Encryption finished.");
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function decryptSubTree(noteId) {
|
async function decryptSubTree(noteId) {
|
||||||
handleEncryption(true, true, () => {
|
await handleEncryption(true, true);
|
||||||
updateSubTreeRecursively(noteId, note => {
|
|
||||||
if (note.detail.encryption === 1) {
|
|
||||||
decryptNote(note);
|
|
||||||
|
|
||||||
note.detail.encryption = 0;
|
updateSubTreeRecursively(noteId, note => {
|
||||||
|
if (note.detail.encryption === 1) {
|
||||||
|
decryptNote(note);
|
||||||
|
|
||||||
return true;
|
note.detail.encryption = 0;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
note => {
|
||||||
|
if (note.detail.note_id === globalCurrentNote.detail.note_id) {
|
||||||
|
loadNoteToEditor(note.detail.note_id);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return false;
|
setTreeBasedOnEncryption(note);
|
||||||
}
|
}
|
||||||
},
|
});
|
||||||
note => {
|
|
||||||
if (note.detail.note_id === globalCurrentNote.detail.note_id) {
|
|
||||||
loadNoteToEditor(note.detail.note_id);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
setTreeBasedOnEncryption(note);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
alert("Decryption finished.");
|
alert("Decryption finished.");
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateSubTreeRecursively(noteId, updateCallback, successCallback) {
|
function updateSubTreeRecursively(noteId, updateCallback, successCallback) {
|
||||||
|
@ -61,9 +61,7 @@ $(document).tooltip({
|
|||||||
const noteId = getNoteIdFromLink($(this).attr("href"));
|
const noteId = getNoteIdFromLink($(this).attr("href"));
|
||||||
|
|
||||||
if (noteId !== null) {
|
if (noteId !== null) {
|
||||||
loadNote(noteId, note => {
|
loadNote(noteId).then(note => callback(note.detail.note_text));
|
||||||
callback(note.detail.note_text);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
close: function(event, ui)
|
close: function(event, ui)
|
||||||
|
@ -21,12 +21,8 @@ function noteChanged() {
|
|||||||
isNoteChanged = true;
|
isNoteChanged = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveNoteIfChanged(callback) {
|
async function saveNoteIfChanged() {
|
||||||
if (!isNoteChanged) {
|
if (!isNoteChanged) {
|
||||||
if (callback) {
|
|
||||||
callback();
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,7 +32,7 @@ function saveNoteIfChanged(callback) {
|
|||||||
|
|
||||||
encryptNoteIfNecessary(note);
|
encryptNoteIfNecessary(note);
|
||||||
|
|
||||||
saveNoteToServer(note, callback);
|
await saveNoteToServer(note);
|
||||||
}
|
}
|
||||||
|
|
||||||
setInterval(saveNoteIfChanged, 5000);
|
setInterval(saveNoteIfChanged, 5000);
|
||||||
@ -91,25 +87,20 @@ function updateNoteFromInputs(note) {
|
|||||||
note.detail.note_title = title;
|
note.detail.note_title = title;
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveNoteToServer(note, callback) {
|
async function saveNoteToServer(note) {
|
||||||
$.ajax({
|
await $.ajax({
|
||||||
url: baseApiUrl + 'notes/' + note.detail.note_id,
|
url: baseApiUrl + 'notes/' + note.detail.note_id,
|
||||||
type: 'PUT',
|
type: 'PUT',
|
||||||
data: JSON.stringify(note),
|
data: JSON.stringify(note),
|
||||||
contentType: "application/json",
|
contentType: "application/json",
|
||||||
success: () => {
|
|
||||||
isNoteChanged = false;
|
|
||||||
|
|
||||||
message("Saved!");
|
|
||||||
|
|
||||||
if (callback) {
|
|
||||||
callback();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
error: () => {
|
error: () => {
|
||||||
error("Error saving the note!");
|
error("Error saving the note!");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
isNoteChanged = false;
|
||||||
|
|
||||||
|
message("Saved!");
|
||||||
}
|
}
|
||||||
|
|
||||||
let globalCurrentNote;
|
let globalCurrentNote;
|
||||||
@ -123,7 +114,7 @@ function createNewTopLevelNote() {
|
|||||||
|
|
||||||
let newNoteCreated = false;
|
let newNoteCreated = false;
|
||||||
|
|
||||||
function createNote(node, parentKey, target, encryption) {
|
async function createNote(node, parentKey, target, encryption) {
|
||||||
// if encryption isn't available (user didn't enter password yet), then note is created as unencrypted
|
// if encryption isn't available (user didn't enter password yet), then note is created as unencrypted
|
||||||
// but this is quite weird since user doesn't see where the note is being created so it shouldn't occur often
|
// but this is quite weird since user doesn't see where the note is being created so it shouldn't occur often
|
||||||
if (!encryption || !isEncryptionAvailable()) {
|
if (!encryption || !isEncryptionAvailable()) {
|
||||||
@ -133,7 +124,7 @@ function createNote(node, parentKey, target, encryption) {
|
|||||||
const newNoteName = "new note";
|
const newNoteName = "new note";
|
||||||
const newNoteNameEncryptedIfNecessary = encryption > 0 ? encryptString(newNoteName) : newNoteName;
|
const newNoteNameEncryptedIfNecessary = encryption > 0 ? encryptString(newNoteName) : newNoteName;
|
||||||
|
|
||||||
$.ajax({
|
const result = await $.ajax({
|
||||||
url: baseApiUrl + 'notes/' + parentKey + '/children' ,
|
url: baseApiUrl + 'notes/' + parentKey + '/children' ,
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
data: JSON.stringify({
|
data: JSON.stringify({
|
||||||
@ -142,33 +133,32 @@ function createNote(node, parentKey, target, encryption) {
|
|||||||
target_note_id: node.key,
|
target_note_id: node.key,
|
||||||
encryption: encryption
|
encryption: encryption
|
||||||
}),
|
}),
|
||||||
contentType: "application/json",
|
contentType: "application/json"
|
||||||
success: result => {
|
|
||||||
const newNode = {
|
|
||||||
title: newNoteName,
|
|
||||||
key: result.note_id,
|
|
||||||
note_id: result.note_id,
|
|
||||||
encryption: encryption,
|
|
||||||
extraClasses: encryption ? "encrypted" : ""
|
|
||||||
};
|
|
||||||
|
|
||||||
globalAllNoteIds.push(result.note_id);
|
|
||||||
|
|
||||||
newNoteCreated = true;
|
|
||||||
|
|
||||||
if (target === 'after') {
|
|
||||||
node.appendSibling(newNode).setActive(true);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
node.addChildren(newNode).setActive(true);
|
|
||||||
|
|
||||||
node.folder = true;
|
|
||||||
node.renderTitle();
|
|
||||||
}
|
|
||||||
|
|
||||||
message("Created!");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const newNode = {
|
||||||
|
title: newNoteName,
|
||||||
|
key: result.note_id,
|
||||||
|
note_id: result.note_id,
|
||||||
|
encryption: encryption,
|
||||||
|
extraClasses: encryption ? "encrypted" : ""
|
||||||
|
};
|
||||||
|
|
||||||
|
globalAllNoteIds.push(result.note_id);
|
||||||
|
|
||||||
|
newNoteCreated = true;
|
||||||
|
|
||||||
|
if (target === 'after') {
|
||||||
|
node.appendSibling(newNode).setActive(true);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
node.addChildren(newNode).setActive(true);
|
||||||
|
|
||||||
|
node.folder = true;
|
||||||
|
node.renderTitle();
|
||||||
|
}
|
||||||
|
|
||||||
|
message("Created!");
|
||||||
}
|
}
|
||||||
|
|
||||||
function setTreeBasedOnEncryption(note) {
|
function setTreeBasedOnEncryption(note) {
|
||||||
@ -191,58 +181,57 @@ function setNoteBackgroundIfEncrypted(note) {
|
|||||||
setTreeBasedOnEncryption(note);
|
setTreeBasedOnEncryption(note);
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadNoteToEditor(noteId) {
|
async function loadNoteToEditor(noteId) {
|
||||||
$.get(baseApiUrl + 'notes/' + noteId).then(note => {
|
const note = await $.get(baseApiUrl + 'notes/' + noteId);
|
||||||
globalCurrentNote = note;
|
globalCurrentNote = note;
|
||||||
globalCurrentNoteLoadTime = Math.floor(new Date().getTime() / 1000);
|
globalCurrentNoteLoadTime = Math.floor(new Date().getTime() / 1000);
|
||||||
|
|
||||||
if (newNoteCreated) {
|
if (newNoteCreated) {
|
||||||
newNoteCreated = false;
|
newNoteCreated = false;
|
||||||
|
|
||||||
$("#note-title").focus().select();
|
$("#note-title").focus().select();
|
||||||
}
|
}
|
||||||
|
|
||||||
handleEncryption(note.detail.encryption > 0, false, () => {
|
await handleEncryption(note.detail.encryption > 0, false);
|
||||||
$("#note-detail-wrapper").show();
|
|
||||||
|
|
||||||
// this may fal if the dialog has not been previously opened
|
$("#note-detail-wrapper").show();
|
||||||
try {
|
|
||||||
$("#encryption-password-dialog").dialog('close');
|
|
||||||
}
|
|
||||||
catch(e) {}
|
|
||||||
|
|
||||||
$("#encryption-password").val('');
|
// this may fal if the dialog has not been previously opened
|
||||||
|
try {
|
||||||
|
$("#encryption-password-dialog").dialog('close');
|
||||||
|
}
|
||||||
|
catch(e) {}
|
||||||
|
|
||||||
decryptNoteIfNecessary(note);
|
$("#encryption-password").val('');
|
||||||
|
|
||||||
$("#note-title").val(note.detail.note_title);
|
decryptNoteIfNecessary(note);
|
||||||
|
|
||||||
noteChangeDisabled = true;
|
$("#note-title").val(note.detail.note_title);
|
||||||
|
|
||||||
// Clear contents and remove all stored history. This is to prevent undo from going across notes
|
noteChangeDisabled = true;
|
||||||
$('#note-detail').summernote('reset');
|
|
||||||
|
|
||||||
$('#note-detail').summernote('code', note.detail.note_text);
|
// Clear contents and remove all stored history. This is to prevent undo from going across notes
|
||||||
|
$('#note-detail').summernote('reset');
|
||||||
|
|
||||||
document.location.hash = noteId;
|
$('#note-detail').summernote('code', note.detail.note_text);
|
||||||
|
|
||||||
addRecentNote(noteId, note.detail.note_id);
|
document.location.hash = noteId;
|
||||||
|
|
||||||
noteChangeDisabled = false;
|
addRecentNote(noteId, note.detail.note_id);
|
||||||
|
|
||||||
setNoteBackgroundIfEncrypted(note);
|
noteChangeDisabled = false;
|
||||||
});
|
|
||||||
});
|
setNoteBackgroundIfEncrypted(note);
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadNote(noteId, callback) {
|
async function loadNote(noteId) {
|
||||||
$.get(baseApiUrl + 'notes/' + noteId).then(note => {
|
const note = await $.get(baseApiUrl + 'notes/' + noteId);
|
||||||
if (note.detail.encryption > 0 && !isEncryptionAvailable()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
decryptNoteIfNecessary(note);
|
if (note.detail.encryption > 0 && !isEncryptionAvailable()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
callback(note);
|
decryptNoteIfNecessary(note);
|
||||||
});
|
|
||||||
|
return note;
|
||||||
}
|
}
|
@ -97,7 +97,7 @@ function initFancyTree(notes, startNoteId) {
|
|||||||
activate: (event, data) => {
|
activate: (event, data) => {
|
||||||
const node = data.node.data;
|
const node = data.node.data;
|
||||||
|
|
||||||
saveNoteIfChanged(() => loadNoteToEditor(node.note_id));
|
saveNoteIfChanged().then(() => loadNoteToEditor(node.note_id));
|
||||||
},
|
},
|
||||||
expand: (event, data) => {
|
expand: (event, data) => {
|
||||||
setExpandedToServer(data.node.key, true);
|
setExpandedToServer(data.node.key, true);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user