small refactorings

This commit is contained in:
zadam 2019-10-20 13:09:00 +02:00
parent 2305ad7405
commit 358fd13c8d
7 changed files with 44 additions and 25 deletions

6
.idea/jsLibraryMappings.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptLibraryMappings">
<file url="PROJECT" libraries="{@types/jquery}" />
</component>
</project>

6
package-lock.json generated
View File

@ -13037,9 +13037,9 @@
} }
}, },
"ws": { "ws": {
"version": "7.1.2", "version": "7.2.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-7.1.2.tgz", "resolved": "https://registry.npmjs.org/ws/-/ws-7.2.0.tgz",
"integrity": "sha512-gftXq3XI81cJCgkUiAVixA0raD9IVmXqsylCrjRygw4+UOOGzPoxnQ6r/CnVL9i+mDncJo94tSkyrtuuQVBmrg==", "integrity": "sha512-+SqNqFbwTm/0DC18KYzIsMTnEWpLwJsiasW/O17la4iDRRIO9uaHbvKiAS3AHgTiuuWerK/brj4O6MYZkei9xg==",
"requires": { "requires": {
"async-limiter": "^1.0.0" "async-limiter": "^1.0.0"
} }

View File

@ -74,7 +74,7 @@
"turndown": "5.0.3", "turndown": "5.0.3",
"turndown-plugin-gfm": "1.0.2", "turndown-plugin-gfm": "1.0.2",
"unescape": "1.0.1", "unescape": "1.0.1",
"ws": "7.1.2", "ws": "7.2.0",
"xml2js": "0.4.22" "xml2js": "0.4.22"
}, },
"devDependencies": { "devDependencies": {

View File

@ -90,6 +90,7 @@ async function activateOrOpenNote(noteId) {
}); });
} }
/** @return {TabContext[]} */
function getTabContexts() { function getTabContexts() {
return tabContexts; return tabContexts;
} }
@ -119,12 +120,14 @@ function getActiveTabNote() {
return activeContext ? activeContext.note : null; return activeContext ? activeContext.note : null;
} }
/** @return {string|null} */
function getActiveTabNoteId() { function getActiveTabNoteId() {
const activeNote = getActiveTabNote(); const activeNote = getActiveTabNote();
return activeNote ? activeNote.noteId : null; return activeNote ? activeNote.noteId : null;
} }
/** @return {string|null} */
function getActiveTabNoteType() { function getActiveTabNoteType() {
const activeNote = getActiveTabNote(); const activeNote = getActiveTabNote();

View File

@ -25,19 +25,26 @@ const $scrollToActiveNoteButton = $("#scroll-to-active-note-button");
let setFrontendAsLoaded; let setFrontendAsLoaded;
const frontendLoaded = new Promise(resolve => { setFrontendAsLoaded = resolve; }); 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() { function getFocusedNode() {
const tree = $tree.fancytree("getTree"); const tree = $tree.fancytree("getTree");
return tree.getFocusNode(); 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() { function getActiveNode() {
return $tree.fancytree("getActiveNode"); return $tree.fancytree("getActiveNode");
} }
/** @return {FancytreeNode[]} */
async function getNodesByBranchId(branchId) { async function getNodesByBranchId(branchId) {
utils.assertArguments(branchId); utils.assertArguments(branchId);
@ -46,6 +53,7 @@ async function getNodesByBranchId(branchId) {
return getNodesByNoteId(branch.noteId).filter(node => node.data.branchId === branchId); return getNodesByNoteId(branch.noteId).filter(node => node.data.branchId === branchId);
} }
/** @return {FancytreeNode[]} */
function getNodesByNoteId(noteId) { function getNodesByNoteId(noteId) {
utils.assertArguments(noteId); utils.assertArguments(noteId);
@ -76,10 +84,12 @@ async function setNodeTitleWithPrefix(node) {
node.setTitle(utils.escapeHtml(title)); node.setTitle(utils.escapeHtml(title));
} }
/** @return {FancytreeNode} */
async function expandToNote(notePath, expandOpts) { async function expandToNote(notePath, expandOpts) {
return await getNodeFromPath(notePath, true, expandOpts); return await getNodeFromPath(notePath, true, expandOpts);
} }
/** @return {FancytreeNode} */
function findChildNode(parentNode, childNoteId) { function findChildNode(parentNode, childNoteId) {
let foundChildNode = null; let foundChildNode = null;
@ -89,13 +99,16 @@ function findChildNode(parentNode, childNoteId) {
break; break;
} }
} }
return foundChildNode; return foundChildNode;
} }
/** @return {FancytreeNode} */
async function getNodeFromPath(notePath, expand = false, expandOpts = {}) { async function getNodeFromPath(notePath, expand = false, expandOpts = {}) {
utils.assertArguments(notePath); utils.assertArguments(notePath);
const hoistedNoteId = await hoistedNoteService.getHoistedNoteId(); const hoistedNoteId = await hoistedNoteService.getHoistedNoteId();
/** @var {FancytreeNode} */
let parentNode = null; let parentNode = null;
const runPath = await getRunPath(notePath); const runPath = await getRunPath(notePath);
@ -145,6 +158,7 @@ async function getNodeFromPath(notePath, expand = false, expandOpts = {}) {
return parentNode; return parentNode;
} }
/** @return {FancytreeNode} */
async function activateNote(notePath, noteLoadedListener) { async function activateNote(notePath, noteLoadedListener) {
utils.assertArguments(notePath); 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 * Accepts notePath which might or might not be valid and returns an existing path as close to the original
* notePath as possible. * notePath as possible.
* @return {string|null}
*/ */
async function resolveNotePath(notePath) { async function resolveNotePath(notePath) {
const runPath = await getRunPath(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 * 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. * 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) { async function getRunPath(notePath) {
utils.assertArguments(notePath); utils.assertArguments(notePath);
@ -313,10 +330,12 @@ async function setExpandedToServer(branchId, isExpanded) {
await server.put('branches/' + branchId + '/expanded/' + expandedNum); await server.put('branches/' + branchId + '/expanded/' + expandedNum);
} }
/** @return {FancytreeNode[]} */
function getSelectedNodes(stopOnParents = false) { function getSelectedNodes(stopOnParents = false) {
return getTree().getSelectedNodes(stopOnParents); return getTree().getSelectedNodes(stopOnParents);
} }
/** @return {FancytreeNode[]} */
function getSelectedOrActiveNodes(node) { function getSelectedOrActiveNodes(node) {
let notes = getSelectedNodes(true); let notes = getSelectedNodes(true);
@ -506,6 +525,7 @@ function initFancyTree(tree) {
}); });
} }
/** @return {Fancytree} */
function getTree() { function getTree() {
return $tree.fancytree('getTree'); return $tree.fancytree('getTree');
} }
@ -759,27 +779,17 @@ ws.subscribeToMessages(message => {
}); });
ws.subscribeToOutsideSyncMessages(syncData => { ws.subscribeToOutsideSyncMessages(syncData => {
const noteIdsToRefresh = []; const noteIdsToRefresh = new Set();
for (const sync of syncData.filter(sync => sync.entityName === 'branches')) { // this has the problem that the former parentNoteId might not be invalidated
if (!noteIdsToRefresh.includes(sync.parentNoteId)) { // and the former location of the branch/note won't be removed.
noteIdsToRefresh.push(sync.parentNoteId); syncData.filter(sync => sync.entityName === 'branches').forEach(sync => noteIdsToRefresh.add(sync.parentNoteId));
}
}
for (const sync of syncData.filter(sync => sync.entityName === 'notes')) { syncData.filter(sync => sync.entityName === 'notes').forEach(sync => noteIdsToRefresh.add(sync.noteId));
if (!noteIdsToRefresh.includes(sync.noteId)) {
noteIdsToRefresh.push(sync.noteId);
}
}
for (const sync of syncData.filter(sync => sync.entityName === 'note_reordering')) { syncData.filter(sync => sync.entityName === 'note_reordering').forEach(sync => noteIdsToRefresh.add(sync.entityId));
if (!noteIdsToRefresh.includes(sync.entityId)) {
noteIdsToRefresh.push(sync.entityId);
}
}
reloadNotes(noteIdsToRefresh); reloadNotes(Array.from(noteIdsToRefresh));
}); });
utils.bindGlobalShortcut('ctrl+o', async () => { utils.bindGlobalShortcut('ctrl+o', async () => {

View File

@ -1,4 +1,3 @@
import noteDetailService from "./note_detail.js";
import utils from "./utils.js"; import utils from "./utils.js";
import Branch from "../entities/branch.js"; import Branch from "../entities/branch.js";
import server from "./server.js"; import server from "./server.js";

View File

@ -9,5 +9,6 @@
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="@types/jquery" level="application" />
</component> </component>
</module> </module>