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; let tabIdCounter = 1;
class TabContext { class TabContext {
constructor(chromeTabs, openOnBackground) { constructor(chromeTabs) {
this.tabId = tabIdCounter++; this.tabId = tabIdCounter++;
this.chromeTabs = chromeTabs; this.chromeTabs = chromeTabs;
this.tab = this.chromeTabs.addTab({ this.tab = this.chromeTabs.addTab({
title: '', // will be set later title: '', // will be set later
id: this.tabId id: this.tabId
}, { }, {
background: openOnBackground background: true
}); });
this.$tabContent = $(".note-tab-content-template").clone(); 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); tabId = parseInt(tabId);
for (const ctx of tabContexts) { for (const ctx of tabContexts) {
ctx.$tabContent.toggle(ctx.tabId === tabId); 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 // if it's a new tab explicitly by user then it's in background
ctx = new TabContext(chromeTabs, newTab); ctx = new TabContext(chromeTabs, newTab);
tabContexts.push(ctx); tabContexts.push(ctx);
if (!newTab) {
showTab(ctx.tabId);
}
} }
else { else {
ctx = getActiveContext(); ctx = getActiveContext();
@ -190,6 +202,14 @@ async function loadNoteDetail(notePath, newTab = false) {
} }
await loadNoteDetailToContext(ctx, loadedNote, notePath); 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) { async function loadNote(noteId) {

View File

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