mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-11-04 07:01:31 +08:00 
			
		
		
		
	encrypt and decrypt subtrees (in tree context menu)
This commit is contained in:
		
							parent
							
								
									52034e0cdc
								
							
						
					
					
						commit
						d6ffae2035
					
				@ -6,6 +6,9 @@ const contextMenuSetup = {
 | 
			
		||||
        {title: "Insert child note", cmd: "insertChildNote", uiIcon: "ui-icon-pencil"},
 | 
			
		||||
        {title: "Delete", cmd: "delete", uiIcon: "ui-icon-trash"},
 | 
			
		||||
        {title: "----"},
 | 
			
		||||
        {title: "Encrypt sub-tree", cmd: "encryptSubTree", uiIcon: "ui-icon-locked"},
 | 
			
		||||
        {title: "Decrypt sub-tree", cmd: "decryptSubTree", uiIcon: "ui-icon-unlocked"},
 | 
			
		||||
        {title: "----"},
 | 
			
		||||
        {title: "Cut", cmd: "cut", uiIcon: "ui-icon-scissors"},
 | 
			
		||||
        {title: "Copy / clone", cmd: "copy", uiIcon: "ui-icon-copy"},
 | 
			
		||||
        {title: "Paste after", cmd: "pasteAfter", uiIcon: "ui-icon-clipboard"},
 | 
			
		||||
@ -36,6 +39,12 @@ const contextMenuSetup = {
 | 
			
		||||
        else if (ui.cmd === "insertChildNote") {
 | 
			
		||||
            createNote(node, node.key, 'into');
 | 
			
		||||
        }
 | 
			
		||||
        else if (ui.cmd === "encryptSubTree") {
 | 
			
		||||
            encryptSubTree(node.key);
 | 
			
		||||
        }
 | 
			
		||||
        else if (ui.cmd === "decryptSubTree") {
 | 
			
		||||
            decryptSubTree(node.key);
 | 
			
		||||
        }
 | 
			
		||||
        else if (ui.cmd === "cut") {
 | 
			
		||||
            globalClipboardNoteId = node.key;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -82,8 +82,6 @@ $("#encryptionPasswordForm").submit(function() {
 | 
			
		||||
 | 
			
		||||
        globalDataKey = key;
 | 
			
		||||
 | 
			
		||||
        console.log("got the key", key);
 | 
			
		||||
 | 
			
		||||
        for (const noteId of globalAllNoteIds) {
 | 
			
		||||
            const note = getNodeByKey(noteId);
 | 
			
		||||
 | 
			
		||||
@ -100,7 +98,11 @@ $("#encryptionPasswordForm").submit(function() {
 | 
			
		||||
            globalEncryptionCallback = null;
 | 
			
		||||
        }
 | 
			
		||||
    })
 | 
			
		||||
        .catch(reason => alert(reason));
 | 
			
		||||
        .catch(reason => {
 | 
			
		||||
            console.log(reason);
 | 
			
		||||
 | 
			
		||||
            alert(reason);
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
    return false;
 | 
			
		||||
});
 | 
			
		||||
@ -265,4 +267,109 @@ function decryptNoteIfNecessary(note) {
 | 
			
		||||
function decryptNote(note) {
 | 
			
		||||
    note.detail.note_title = decryptString(note.detail.note_title);
 | 
			
		||||
    note.detail.note_text = decryptString(note.detail.note_text);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function encryptSubTree(noteId) {
 | 
			
		||||
    handleEncryption(true, true, () => {
 | 
			
		||||
        updateSubTreeRecursively(noteId, note => {
 | 
			
		||||
            if (note.detail.encryption === null || note.detail.encryption === 0) {
 | 
			
		||||
                encryptNote(note);
 | 
			
		||||
 | 
			
		||||
                note.detail.encryption = 1;
 | 
			
		||||
 | 
			
		||||
                return true;
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
        },
 | 
			
		||||
            note => {
 | 
			
		||||
                if (note.detail.note_id === globalCurrentNote.detail.note_id) {
 | 
			
		||||
                    loadNote(note.detail.note_id);
 | 
			
		||||
                }
 | 
			
		||||
                else {
 | 
			
		||||
                    setTreeBasedOnEncryption(note);
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
        alert("Encryption finished.");
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function decryptSubTree(noteId) {
 | 
			
		||||
    handleEncryption(true, true, () => {
 | 
			
		||||
        updateSubTreeRecursively(noteId, note => {
 | 
			
		||||
            if (note.detail.encryption === 1) {
 | 
			
		||||
                decryptNote(note);
 | 
			
		||||
 | 
			
		||||
                note.detail.encryption = 0;
 | 
			
		||||
 | 
			
		||||
                return true;
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
        },
 | 
			
		||||
            note => {
 | 
			
		||||
                if (note.detail.note_id === globalCurrentNote.detail.note_id) {
 | 
			
		||||
                    loadNote(note.detail.note_id);
 | 
			
		||||
                }
 | 
			
		||||
                else {
 | 
			
		||||
                    setTreeBasedOnEncryption(note);
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
        alert("Decryption finished.");
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function updateSubTreeRecursively(noteId, updateCallback, successCallback) {
 | 
			
		||||
    updateNoteSynchronously(noteId, updateCallback, successCallback);
 | 
			
		||||
 | 
			
		||||
    const node = getNodeByKey(noteId);
 | 
			
		||||
    if (!node || !node.getChildren()) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (const child of node.getChildren()) {
 | 
			
		||||
        updateSubTreeRecursively(child.key, updateCallback, successCallback);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function updateNoteSynchronously(noteId, updateCallback, successCallback) {
 | 
			
		||||
    $.ajax({
 | 
			
		||||
        url: baseUrl + 'notes/' + noteId,
 | 
			
		||||
        type: 'GET',
 | 
			
		||||
        async: false,
 | 
			
		||||
        success: function (note) {
 | 
			
		||||
            const needSave = updateCallback(note);
 | 
			
		||||
 | 
			
		||||
            if (!needSave) {
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            for (const link of note.links) {
 | 
			
		||||
                delete link.type;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            $.ajax({
 | 
			
		||||
                url: baseUrl + 'notes/' + noteId,
 | 
			
		||||
                type: 'PUT',
 | 
			
		||||
                data: JSON.stringify(note),
 | 
			
		||||
                contentType: "application/json",
 | 
			
		||||
                async: false,
 | 
			
		||||
                success: function () {
 | 
			
		||||
                    if (successCallback) {
 | 
			
		||||
                        successCallback(note);
 | 
			
		||||
                    }
 | 
			
		||||
                },
 | 
			
		||||
                error: function () {
 | 
			
		||||
                    console.log("Updating " + noteId + " failed.");
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
        },
 | 
			
		||||
        error: function () {
 | 
			
		||||
            console.log("Reading " + noteId + " failed.");
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
@ -149,6 +149,11 @@ function createNote(node, parentKey, target, encryption) {
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function setTreeBasedOnEncryption(note) {
 | 
			
		||||
    const node = getNodeByKey(note.detail.note_id);
 | 
			
		||||
    node.toggleClass("encrypted", note.detail.encryption > 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function setNoteBackgroundIfEncrypted(note) {
 | 
			
		||||
    if (note.detail.encryption > 0) {
 | 
			
		||||
        $(".note-editable").addClass("encrypted");
 | 
			
		||||
@ -161,8 +166,7 @@ function setNoteBackgroundIfEncrypted(note) {
 | 
			
		||||
        $("#decryptButton").hide();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const node = getNodeByKey(note.detail.note_id);
 | 
			
		||||
    node.toggleClass("encrypted", note.detail.encryption > 0);
 | 
			
		||||
    setTreeBasedOnEncryption(note);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function loadNote(noteId) {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user