mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
tree active note now follows switched tabs (without changing scroll position)
This commit is contained in:
parent
0a2acbe4be
commit
89d4be504d
950
package-lock.json
generated
950
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -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();
|
||||
|
@ -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) {
|
||||
|
@ -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
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user