tree active note now follows switched tabs (without changing scroll position)

This commit is contained in:
zadam 2019-05-09 21:30:08 +02:00
parent 0a2acbe4be
commit 89d4be504d
4 changed files with 710 additions and 345 deletions

950
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -33,14 +33,14 @@ const componentClasses = {
let tabIdCounter = 1;
class TabContext {
constructor(chromeTabs, openOnBackground) {
constructor(chromeTabs) {
this.tabId = tabIdCounter++;
this.chromeTabs = chromeTabs;
this.tab = this.chromeTabs.addTab({
title: '', // will be set later
id: this.tabId
}, {
background: openOnBackground
background: true
});
this.$tabContent = $(".note-tab-content-template").clone();

View file

@ -96,12 +96,28 @@ function getActiveContext() {
}
}
function showTab(tabId) {
async function showTab(tabId) {
tabId = parseInt(tabId);
for (const ctx of tabContexts) {
ctx.$tabContent.toggle(ctx.tabId === tabId);
}
const oldActiveNode = treeService.getActiveNode();
if (oldActiveNode) {
oldActiveNode.setActive(false);
}
treeService.clearSelectedNodes();
const newActiveTabContext = getActiveContext();
const newActiveNode = await treeService.getNodeFromPath(newActiveTabContext.notePath);
if (newActiveNode && newActiveNode.isVisible()) {
newActiveNode.setActive(true, { noEvents: true });
newActiveNode.setSelected(true);
}
}
/**
@ -171,10 +187,6 @@ async function loadNoteDetail(notePath, newTab = false) {
// if it's a new tab explicitly by user then it's in background
ctx = new TabContext(chromeTabs, newTab);
tabContexts.push(ctx);
if (!newTab) {
showTab(ctx.tabId);
}
}
else {
ctx = getActiveContext();
@ -190,6 +202,14 @@ async function loadNoteDetail(notePath, newTab = false) {
}
await loadNoteDetailToContext(ctx, loadedNote, notePath);
if (!chromeTabs.activeTabEl) {
// will also trigger showTab via event
chromeTabs.setCurrentTab(ctx.tab);
}
else if (!newTab) {
await showTab(ctx.tabId);
}
}
async function loadNote(noteId) {

View file

@ -82,51 +82,53 @@ async function setNodeTitleWithPrefix(node) {
node.setTitle(utils.escapeHtml(title));
}
function getNode(childNoteId, parentNoteId) {
return getNodesByNoteId(childNoteId).find(node => !parentNoteId || node.data.parentNoteId === parentNoteId);
async function expandToNote(notePath, expandOpts) {
return await getNodeFromPath(notePath, true, expandOpts);
}
async function expandToNote(notePath, expandOpts) {
async function getNodeFromPath(notePath, expand = false, expandOpts = {}) {
utils.assertArguments(notePath);
const runPath = await getRunPath(notePath);
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
const hoistedNoteId = await hoistedNoteService.getHoistedNoteId();
let hoistedNoteFound = false;
let parentNode = null;
let parentNoteId = null;
for (const childNoteId of runPath) {
for (const childNoteId of await getRunPath(notePath)) {
if (childNoteId === hoistedNoteId) {
hoistedNoteFound = true;
// there must be exactly one node with given hoistedNoteId
parentNode = getNodesByNoteId(childNoteId)[0];
continue;
}
// we expand only after hoisted note since before then nodes are not actually present in the tree
if (hoistedNoteFound) {
// for first node (!parentNoteId) it doesn't matter which node is found
let node = getNode(childNoteId, parentNoteId);
if (!node && parentNoteId) {
await reloadNote(parentNoteId);
node = getNode(childNoteId, parentNoteId);
if (parentNode) {
if (!parentNode.isLoaded()) {
await parentNode.load();
}
if (!node) {
console.error(`Can't find node for noteId=${childNoteId} with parentNoteId=${parentNoteId} and hoistedNoteId=${hoistedNoteId}`);
if (expand) {
parentNode.setExpanded(true, expandOpts);
}
if (childNoteId === noteId) {
return node;
} else {
await node.setExpanded(true, expandOpts);
let foundChildNode = null;
for (const childNode of parentNode.getChildren()) {
if (childNode.data.noteId === childNoteId) {
foundChildNode = childNode;
break;
}
}
if (!foundChildNode) {
console.error(`Can't find node for child node of noteId=${childNoteId} for parent of noteId=${parentNode.data.noteId} and hoistedNoteId=${hoistedNoteId}`);
return;
}
parentNode = foundChildNode;
}
parentNoteId = childNoteId;
}
return parentNode;
}
async function activateNote(notePath, noteLoadedListener) {
@ -557,13 +559,17 @@ async function collapseTree(node = null) {
node.visit(node => node.setExpanded(false));
}
function scrollToActiveNote() {
const node = getActiveNode();
async function scrollToActiveNote() {
const activeContext = noteDetailService.getActiveContext();
if (activeContext) {
const node = await expandToNote(activeContext.notePath);
if (node) {
node.makeVisible({scrollIntoView: true});
node.setFocus();
await activateNote(activeContext.notePath);
}
}
@ -846,5 +852,6 @@ export default {
checkFolderStatus,
reloadNote,
loadTreeCache,
expandToNote
expandToNote,
getNodeFromPath
};