Skip to content

Build your personal knowledge base with Trilium Notes

fixed update of encrypted note

azivner53d327e43565
diff --git a/src/templates/app.html b/src/templates/app.html
index e383e1f364..313c772b35 100644
--- a/src/templates/app.html
+++ b/src/templates/app.html
@@ -42,7 +42,7 @@
<div style="float: left; margin: 0 5px 5px 5px;" class="hide-toggle">
<input autocomplete="off" value="Welcome to Notecase web app!" id="noteTitle" style="font-size: x-large; border: 0; width: 600px;" tabindex="1">
- <button class="btn btn-sm" onclick="encryptNote();">Encrypt</button>
+ <button class="btn btn-sm" onclick="encryptNoteAndSendToServer();">Encrypt</button>
</div>
<div style="clear: both; height: 0"></div>
diff --git a/static/js/note.js b/static/js/note.js
index 5953c93b30..55abc5db72 100644
--- a/static/js/note.js
+++ b/static/js/note.js
@@ -67,7 +67,9 @@ function saveNoteIfChanged(callback) {
updateNoteFromInputs(note);
- saveNoteToServer(note, callback);
+ encryptNoteIfNecessary(note).then(note => {
+ saveNoteToServer(note, callback);
+ });
}
setInterval(saveNoteIfChanged, 5000);
@@ -160,16 +162,7 @@ function loadNote(noteId) {
$("#noteTitle").focus().select();
}
- let decryptPromise;
-
- if (note.detail.encryption === 1) {
- decryptPromise = decryptNote(note.detail.note_text);
- }
- else {
- decryptPromise = Promise.resolve(note.detail.note_text);
- }
-
- decryptPromise.then(decrypted => {
+ decryptNoteIfNecessary(note).then(decrypted => {
note.detail.note_text = decrypted;
let noteText = notecase2html(note);
@@ -262,12 +255,17 @@ function getAes() {
});
}
-function encryptNote() {
- getAes().then(aes => {
- const note = globalNote;
-
- updateNoteFromInputs(note);
+function encryptNoteIfNecessary(note) {
+ if (note.detail.encryption === 0) {
+ return Promise.resolve(note);
+ }
+ else {
+ return encryptNote(note);
+ }
+}
+function encryptNote(note) {
+ return getAes().then(aes => {
const noteJson = note.detail.note_text;
const noteBytes = aesjs.utils.utf8.toBytes(noteJson);
@@ -275,9 +273,16 @@ function encryptNote() {
const encryptedBytes = aes.encrypt(noteBytes);
// To print or store the binary data, you may convert it to hex
- const encryptedBase64 = uint8ToBase64(encryptedBytes);
+ note.detail.note_text = uint8ToBase64(encryptedBytes);
- note.detail.note_text = encryptedBase64;
+ return note;
+ });
+}
+
+function encryptNoteAndSendToServer() {
+ updateNoteFromInputs(globalNote);
+
+ encryptNote(globalNote).then(note => {
note.detail.encryption = 1;
saveNoteToServer(note);
@@ -286,6 +291,18 @@ function encryptNote() {
});
}
+function decryptNoteIfNecessary(note) {
+ let decryptPromise;
+
+ if (note.detail.encryption === 1) {
+ decryptPromise = decryptNote(note.detail.note_text);
+ }
+ else {
+ decryptPromise = Promise.resolve(note.detail.note_text);
+ }
+ return decryptPromise;
+}
+
function decryptNote(encryptedBase64) {
return getAes().then(aes => {
const encryptedBytes = base64ToUint8Array(encryptedBase64);