mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-11-04 07:01:31 +08:00 
			
		
		
		
	tree utils as a module
This commit is contained in:
		
							parent
							
								
									900f169dab
								
							
						
					
					
						commit
						30df3cba1c
					
				@ -4,7 +4,7 @@ const contextMenu = (function() {
 | 
			
		||||
    const treeEl = $("#tree");
 | 
			
		||||
 | 
			
		||||
    function pasteAfter(node) {
 | 
			
		||||
        const subjectNode = getNodeByKey(noteTree.getClipboardNoteId());
 | 
			
		||||
        const subjectNode = treeUtils.getNodeByKey(noteTree.getClipboardNoteId());
 | 
			
		||||
 | 
			
		||||
        treeChanges.moveAfterNode(subjectNode, node);
 | 
			
		||||
 | 
			
		||||
@ -12,7 +12,7 @@ const contextMenu = (function() {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function pasteInto(node) {
 | 
			
		||||
        const subjectNode = getNodeByKey(noteTree.getClipboardNoteId());
 | 
			
		||||
        const subjectNode = treeUtils.getNodeByKey(noteTree.getClipboardNoteId());
 | 
			
		||||
 | 
			
		||||
        treeChanges.moveToNode(subjectNode, node);
 | 
			
		||||
 | 
			
		||||
@ -56,8 +56,8 @@ const contextMenu = (function() {
 | 
			
		||||
            const node = $.ui.fancytree.getNode(ui.target);
 | 
			
		||||
 | 
			
		||||
            if (ui.cmd === "insertNoteHere") {
 | 
			
		||||
                const parentKey = getParentKey(node);
 | 
			
		||||
                const encryption = getParentEncryption(node);
 | 
			
		||||
                const parentKey = treeUtils.getParentKey(node);
 | 
			
		||||
                const encryption = treeUtils.getParentEncryption(node);
 | 
			
		||||
 | 
			
		||||
                noteEditor.createNote(node, parentKey, 'after', encryption);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
@ -21,7 +21,7 @@ const addLink = (function() {
 | 
			
		||||
        linkTitleEl.val('');
 | 
			
		||||
 | 
			
		||||
        function setDefaultLinkTitle(noteId) {
 | 
			
		||||
            const noteTitle = getNoteTitle(noteId);
 | 
			
		||||
            const noteTitle = treeUtils.getNoteTitle(noteId);
 | 
			
		||||
 | 
			
		||||
            linkTitleEl.val(noteTitle);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -28,7 +28,7 @@ const jumpToNote = (function() {
 | 
			
		||||
        const noteId = link.getNodeIdFromLabel(val);
 | 
			
		||||
 | 
			
		||||
        if (noteId) {
 | 
			
		||||
            getNodeByKey(noteId).setActive();
 | 
			
		||||
            treeUtils.activateNode(noteId);
 | 
			
		||||
 | 
			
		||||
            dialogEl.dialog('close');
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -40,7 +40,7 @@ const recentNotes = (function() {
 | 
			
		||||
        const recNotes = list.filter(note => note !== noteEditor.getCurrentNoteId());
 | 
			
		||||
 | 
			
		||||
        $.each(recNotes, (key, valueNoteId) => {
 | 
			
		||||
            const noteTitle = getFullName(valueNoteId);
 | 
			
		||||
            const noteTitle = treeUtils.getFullName(valueNoteId);
 | 
			
		||||
 | 
			
		||||
            if (!noteTitle) {
 | 
			
		||||
                return;
 | 
			
		||||
@ -66,7 +66,7 @@ const recentNotes = (function() {
 | 
			
		||||
    function setActiveNoteBasedOnRecentNotes() {
 | 
			
		||||
        const noteId = getSelectedNoteIdFromRecentNotes();
 | 
			
		||||
 | 
			
		||||
        getNodeByKey(noteId).setActive();
 | 
			
		||||
        treeUtils.activateNode(noteId);
 | 
			
		||||
 | 
			
		||||
        dialogEl.dialog('close');
 | 
			
		||||
    }
 | 
			
		||||
@ -74,7 +74,7 @@ const recentNotes = (function() {
 | 
			
		||||
    function addLinkBasedOnRecentNotes() {
 | 
			
		||||
        const noteId = getSelectedNoteIdFromRecentNotes();
 | 
			
		||||
 | 
			
		||||
        const linkTitle = getNoteTitle(noteId);
 | 
			
		||||
        const linkTitle = treeUtils.getNoteTitle(noteId);
 | 
			
		||||
 | 
			
		||||
        dialogEl.dialog("close");
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -42,7 +42,7 @@ const encryption = (function() {
 | 
			
		||||
                open: () => {
 | 
			
		||||
                    if (!modal) {
 | 
			
		||||
                        // dialog steals focus for itself, which is not what we want for non-modal (viewing)
 | 
			
		||||
                        getNodeByKey(noteEditor.getCurrentNoteId()).setFocus();
 | 
			
		||||
                        treeUtils.getNodeByKey(noteEditor.getCurrentNoteId()).setFocus();
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
@ -105,7 +105,7 @@ const encryption = (function() {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        for (const noteId of glob.allNoteIds) {
 | 
			
		||||
            const note = getNodeByKey(noteId);
 | 
			
		||||
            const note = treeUtils.getNodeByKey(noteId);
 | 
			
		||||
 | 
			
		||||
            if (note.data.encryption > 0) {
 | 
			
		||||
                const title = decryptString(note.data.note_title);
 | 
			
		||||
@ -375,7 +375,7 @@ const encryption = (function() {
 | 
			
		||||
    function updateSubTreeRecursively(noteId, updateCallback, successCallback) {
 | 
			
		||||
        updateNoteSynchronously(noteId, updateCallback, successCallback);
 | 
			
		||||
 | 
			
		||||
        const node = getNodeByKey(noteId);
 | 
			
		||||
        const node = treeUtils.getNodeByKey(noteId);
 | 
			
		||||
        if (!node || !node.getChildren()) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -25,7 +25,7 @@ const link = (function() {
 | 
			
		||||
    function createNoteLink(noteId) {
 | 
			
		||||
        const noteLink = $("<a>", {
 | 
			
		||||
            href: 'javascript:',
 | 
			
		||||
            text: getFullName(noteId)
 | 
			
		||||
            text: treeUtils.getFullName(noteId)
 | 
			
		||||
        }).attr('action', 'note')
 | 
			
		||||
            .attr('note-id', noteId);
 | 
			
		||||
 | 
			
		||||
@ -41,7 +41,7 @@ const link = (function() {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (noteId) {
 | 
			
		||||
            getNodeByKey(noteId).setActive();
 | 
			
		||||
            treeUtils.activateNode(noteId);
 | 
			
		||||
 | 
			
		||||
            // this is quite ugly hack, but it seems like we can't close the tooltip otherwise
 | 
			
		||||
            $("[role='tooltip']").remove();
 | 
			
		||||
 | 
			
		||||
@ -78,7 +78,7 @@ const noteEditor = (function() {
 | 
			
		||||
 | 
			
		||||
        const title = noteTitleEl.val();
 | 
			
		||||
 | 
			
		||||
        getNodeByKey(note.detail.note_id).setTitle(title);
 | 
			
		||||
        treeUtils.getNodeByKey(note.detail.note_id).setTitle(title);
 | 
			
		||||
 | 
			
		||||
        note.detail.note_title = title;
 | 
			
		||||
    }
 | 
			
		||||
@ -155,7 +155,7 @@ const noteEditor = (function() {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function setTreeBasedOnEncryption(note) {
 | 
			
		||||
        const node = getNodeByKey(note.detail.note_id);
 | 
			
		||||
        const node = treeUtils.getNodeByKey(note.detail.note_id);
 | 
			
		||||
        node.toggleClass("encrypted", note.detail.encryption > 0);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -58,8 +58,8 @@ const noteTree = (function() {
 | 
			
		||||
    function initFancyTree(notes, startNoteId) {
 | 
			
		||||
        const keybindings = {
 | 
			
		||||
            "insert": node => {
 | 
			
		||||
                const parentKey = getParentKey(node);
 | 
			
		||||
                const encryption = getParentEncryption(node);
 | 
			
		||||
                const parentKey = treeUtils.getParentKey(node);
 | 
			
		||||
                const encryption = treeUtils.getParentEncryption(node);
 | 
			
		||||
 | 
			
		||||
                noteEditor.createNote(node, parentKey, 'after', encryption);
 | 
			
		||||
            },
 | 
			
		||||
@ -227,7 +227,7 @@ const noteTree = (function() {
 | 
			
		||||
    $(document).bind('keydown', 'alt+c', collapseTree);
 | 
			
		||||
 | 
			
		||||
    function scrollToCurrentNote() {
 | 
			
		||||
        const node = getNodeByKey(noteEditor.getCurrentNoteId());
 | 
			
		||||
        const node = treeUtils.getNodeByKey(noteEditor.getCurrentNoteId());
 | 
			
		||||
 | 
			
		||||
        if (node) {
 | 
			
		||||
            node.makeVisible({scrollIntoView: true});
 | 
			
		||||
 | 
			
		||||
@ -1,61 +1,78 @@
 | 
			
		||||
"use strict";
 | 
			
		||||
 | 
			
		||||
const treeEl = $("#tree");
 | 
			
		||||
const treeUtils = (function() {
 | 
			
		||||
    const treeEl = $("#tree");
 | 
			
		||||
 | 
			
		||||
function getParentKey(node) {
 | 
			
		||||
    return (node.getParent() === null || node.getParent().key === "root_1") ? "root" : node.getParent().key;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function getParentEncryption(node) {
 | 
			
		||||
    return node.getParent() === null ? 0 : node.getParent().data.encryption;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function getNodeByKey(noteId) {
 | 
			
		||||
    return treeEl.fancytree('getNodeByKey', noteId);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function getNoteTitle(noteId) {
 | 
			
		||||
    const note = getNodeByKey(noteId);
 | 
			
		||||
    if (!note) {
 | 
			
		||||
        return;
 | 
			
		||||
    function getParentKey(node) {
 | 
			
		||||
        return (node.getParent() === null || node.getParent().key === "root_1") ? "root" : node.getParent().key;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let noteTitle = note.title;
 | 
			
		||||
 | 
			
		||||
    if (noteTitle.endsWith(" (clone)")) {
 | 
			
		||||
        noteTitle = noteTitle.substr(0, noteTitle.length - 8);
 | 
			
		||||
    function getParentEncryption(node) {
 | 
			
		||||
        return node.getParent() === null ? 0 : node.getParent().data.encryption;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return noteTitle;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function getFullName(noteId) {
 | 
			
		||||
    let note = getNodeByKey(noteId);
 | 
			
		||||
 | 
			
		||||
    if (note === null) {
 | 
			
		||||
        return "[unknown]";
 | 
			
		||||
    function getNodeByKey(noteId) {
 | 
			
		||||
        return treeEl.fancytree('getNodeByKey', noteId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // why?
 | 
			
		||||
    if (note.data.is_clone) {
 | 
			
		||||
        return null;
 | 
			
		||||
    function activateNode(noteId) {
 | 
			
		||||
        const node = treeUtils.getNodeByKey(noteId);
 | 
			
		||||
 | 
			
		||||
        node.setActive();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const path = [];
 | 
			
		||||
 | 
			
		||||
    while (note) {
 | 
			
		||||
        if (note.data.encryption > 0 && !encryption.isEncryptionAvailable()) {
 | 
			
		||||
            path.push("[encrypted]");
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            path.push(note.title);
 | 
			
		||||
    function getNoteTitle(noteId) {
 | 
			
		||||
        const note = treeUtils.getNodeByKey(noteId);
 | 
			
		||||
        if (!note) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        note = note.getParent();
 | 
			
		||||
        let noteTitle = note.title;
 | 
			
		||||
 | 
			
		||||
        if (noteTitle.endsWith(" (clone)")) {
 | 
			
		||||
            noteTitle = noteTitle.substr(0, noteTitle.length - 8);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return noteTitle;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // remove "root" element
 | 
			
		||||
    path.pop();
 | 
			
		||||
    function getFullName(noteId) {
 | 
			
		||||
        let note = treeUtils.getNodeByKey(noteId);
 | 
			
		||||
 | 
			
		||||
    return path.reverse().join(" > ");
 | 
			
		||||
}
 | 
			
		||||
        if (note === null) {
 | 
			
		||||
            return "[unknown]";
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // why?
 | 
			
		||||
        if (note.data.is_clone) {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const path = [];
 | 
			
		||||
 | 
			
		||||
        while (note) {
 | 
			
		||||
            if (note.data.encryption > 0 && !encryption.isEncryptionAvailable()) {
 | 
			
		||||
                path.push("[encrypted]");
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                path.push(note.title);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            note = note.getParent();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // remove "root" element
 | 
			
		||||
        path.pop();
 | 
			
		||||
 | 
			
		||||
        return path.reverse().join(" > ");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return {
 | 
			
		||||
        getParentKey,
 | 
			
		||||
        getParentEncryption,
 | 
			
		||||
        getNodeByKey,
 | 
			
		||||
        activateNode,
 | 
			
		||||
        getNoteTitle,
 | 
			
		||||
        getFullName
 | 
			
		||||
    };
 | 
			
		||||
})();
 | 
			
		||||
@ -20,7 +20,7 @@ function getAutocompleteItems(noteIds) {
 | 
			
		||||
    const autocompleteItems = [];
 | 
			
		||||
 | 
			
		||||
    for (const noteId of noteIds) {
 | 
			
		||||
        const fullName = getFullName(noteId);
 | 
			
		||||
        const fullName = treeUtils.getFullName(noteId);
 | 
			
		||||
 | 
			
		||||
        if (fullName !== null) {
 | 
			
		||||
            autocompleteItems.push({
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user