From 358fd13c8dd297e62409f15c1ca5b67a477a552e Mon Sep 17 00:00:00 2001 From: zadam Date: Sun, 20 Oct 2019 13:09:00 +0200 Subject: [PATCH] small refactorings --- .idea/jsLibraryMappings.xml | 6 +++ package-lock.json | 6 +-- package.json | 2 +- .../javascripts/services/note_detail.js | 3 ++ src/public/javascripts/services/tree.js | 50 +++++++++++-------- .../javascripts/services/tree_builder.js | 1 - trilium.iml | 1 + 7 files changed, 44 insertions(+), 25 deletions(-) create mode 100644 .idea/jsLibraryMappings.xml diff --git a/.idea/jsLibraryMappings.xml b/.idea/jsLibraryMappings.xml new file mode 100644 index 000000000..d1d2927d2 --- /dev/null +++ b/.idea/jsLibraryMappings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 4ba4a0cb5..033bd0280 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13037,9 +13037,9 @@ } }, "ws": { - "version": "7.1.2", - "resolved": "https://registry.npmjs.org/ws/-/ws-7.1.2.tgz", - "integrity": "sha512-gftXq3XI81cJCgkUiAVixA0raD9IVmXqsylCrjRygw4+UOOGzPoxnQ6r/CnVL9i+mDncJo94tSkyrtuuQVBmrg==", + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.2.0.tgz", + "integrity": "sha512-+SqNqFbwTm/0DC18KYzIsMTnEWpLwJsiasW/O17la4iDRRIO9uaHbvKiAS3AHgTiuuWerK/brj4O6MYZkei9xg==", "requires": { "async-limiter": "^1.0.0" } diff --git a/package.json b/package.json index d304752d5..c625c6dfb 100644 --- a/package.json +++ b/package.json @@ -74,7 +74,7 @@ "turndown": "5.0.3", "turndown-plugin-gfm": "1.0.2", "unescape": "1.0.1", - "ws": "7.1.2", + "ws": "7.2.0", "xml2js": "0.4.22" }, "devDependencies": { diff --git a/src/public/javascripts/services/note_detail.js b/src/public/javascripts/services/note_detail.js index c5af66e72..96b5e39e6 100644 --- a/src/public/javascripts/services/note_detail.js +++ b/src/public/javascripts/services/note_detail.js @@ -90,6 +90,7 @@ async function activateOrOpenNote(noteId) { }); } +/** @return {TabContext[]} */ function getTabContexts() { return tabContexts; } @@ -119,12 +120,14 @@ function getActiveTabNote() { return activeContext ? activeContext.note : null; } +/** @return {string|null} */ function getActiveTabNoteId() { const activeNote = getActiveTabNote(); return activeNote ? activeNote.noteId : null; } +/** @return {string|null} */ function getActiveTabNoteType() { const activeNote = getActiveTabNote(); diff --git a/src/public/javascripts/services/tree.js b/src/public/javascripts/services/tree.js index 4ff366df8..891e6b733 100644 --- a/src/public/javascripts/services/tree.js +++ b/src/public/javascripts/services/tree.js @@ -25,19 +25,26 @@ const $scrollToActiveNoteButton = $("#scroll-to-active-note-button"); let setFrontendAsLoaded; const frontendLoaded = new Promise(resolve => { setFrontendAsLoaded = resolve; }); -// focused & not active node can happen during multiselection where the node is selected but not activated -// (its content is not displayed in the detail) +/** + * focused & not active node can happen during multiselection where the node is selected but not activated + * (its content is not displayed in the detail) + * @return {FancytreeNode|null} + */ function getFocusedNode() { const tree = $tree.fancytree("getTree"); return tree.getFocusNode(); } -// note that if you want to access data like noteId or isProtected, you need to go into "data" property +/** + * note that if you want to access data like noteId or isProtected, you need to go into "data" property + * @return {FancytreeNode|null} + */ function getActiveNode() { return $tree.fancytree("getActiveNode"); } +/** @return {FancytreeNode[]} */ async function getNodesByBranchId(branchId) { utils.assertArguments(branchId); @@ -46,6 +53,7 @@ async function getNodesByBranchId(branchId) { return getNodesByNoteId(branch.noteId).filter(node => node.data.branchId === branchId); } +/** @return {FancytreeNode[]} */ function getNodesByNoteId(noteId) { utils.assertArguments(noteId); @@ -76,10 +84,12 @@ async function setNodeTitleWithPrefix(node) { node.setTitle(utils.escapeHtml(title)); } +/** @return {FancytreeNode} */ async function expandToNote(notePath, expandOpts) { return await getNodeFromPath(notePath, true, expandOpts); } +/** @return {FancytreeNode} */ function findChildNode(parentNode, childNoteId) { let foundChildNode = null; @@ -89,13 +99,16 @@ function findChildNode(parentNode, childNoteId) { break; } } + return foundChildNode; } +/** @return {FancytreeNode} */ async function getNodeFromPath(notePath, expand = false, expandOpts = {}) { utils.assertArguments(notePath); const hoistedNoteId = await hoistedNoteService.getHoistedNoteId(); + /** @var {FancytreeNode} */ let parentNode = null; const runPath = await getRunPath(notePath); @@ -145,6 +158,7 @@ async function getNodeFromPath(notePath, expand = false, expandOpts = {}) { return parentNode; } +/** @return {FancytreeNode} */ async function activateNote(notePath, noteLoadedListener) { utils.assertArguments(notePath); @@ -190,6 +204,7 @@ async function activateNote(notePath, noteLoadedListener) { /** * Accepts notePath which might or might not be valid and returns an existing path as close to the original * notePath as possible. + * @return {string|null} */ async function resolveNotePath(notePath) { const runPath = await getRunPath(notePath); @@ -200,6 +215,8 @@ async function resolveNotePath(notePath) { /** * Accepts notePath and tries to resolve it. Part of the path might not be valid because of note moving (which causes * path change) or other corruption, in that case this will try to get some other valid path to the correct note. + * + * @return {string[]} */ async function getRunPath(notePath) { utils.assertArguments(notePath); @@ -313,10 +330,12 @@ async function setExpandedToServer(branchId, isExpanded) { await server.put('branches/' + branchId + '/expanded/' + expandedNum); } +/** @return {FancytreeNode[]} */ function getSelectedNodes(stopOnParents = false) { return getTree().getSelectedNodes(stopOnParents); } +/** @return {FancytreeNode[]} */ function getSelectedOrActiveNodes(node) { let notes = getSelectedNodes(true); @@ -506,6 +525,7 @@ function initFancyTree(tree) { }); } +/** @return {Fancytree} */ function getTree() { return $tree.fancytree('getTree'); } @@ -759,27 +779,17 @@ ws.subscribeToMessages(message => { }); ws.subscribeToOutsideSyncMessages(syncData => { - const noteIdsToRefresh = []; + const noteIdsToRefresh = new Set(); - for (const sync of syncData.filter(sync => sync.entityName === 'branches')) { - if (!noteIdsToRefresh.includes(sync.parentNoteId)) { - noteIdsToRefresh.push(sync.parentNoteId); - } - } + // this has the problem that the former parentNoteId might not be invalidated + // and the former location of the branch/note won't be removed. + syncData.filter(sync => sync.entityName === 'branches').forEach(sync => noteIdsToRefresh.add(sync.parentNoteId)); - for (const sync of syncData.filter(sync => sync.entityName === 'notes')) { - if (!noteIdsToRefresh.includes(sync.noteId)) { - noteIdsToRefresh.push(sync.noteId); - } - } + syncData.filter(sync => sync.entityName === 'notes').forEach(sync => noteIdsToRefresh.add(sync.noteId)); - for (const sync of syncData.filter(sync => sync.entityName === 'note_reordering')) { - if (!noteIdsToRefresh.includes(sync.entityId)) { - noteIdsToRefresh.push(sync.entityId); - } - } + syncData.filter(sync => sync.entityName === 'note_reordering').forEach(sync => noteIdsToRefresh.add(sync.entityId)); - reloadNotes(noteIdsToRefresh); + reloadNotes(Array.from(noteIdsToRefresh)); }); utils.bindGlobalShortcut('ctrl+o', async () => { diff --git a/src/public/javascripts/services/tree_builder.js b/src/public/javascripts/services/tree_builder.js index 0fc55ba5d..0480b480d 100644 --- a/src/public/javascripts/services/tree_builder.js +++ b/src/public/javascripts/services/tree_builder.js @@ -1,4 +1,3 @@ -import noteDetailService from "./note_detail.js"; import utils from "./utils.js"; import Branch from "../entities/branch.js"; import server from "./server.js"; diff --git a/trilium.iml b/trilium.iml index eb60d2818..6459dc93b 100644 --- a/trilium.iml +++ b/trilium.iml @@ -9,5 +9,6 @@ + \ No newline at end of file