further tab management moved to app context

This commit is contained in:
zadam
2020-01-12 12:48:17 +01:00
parent 4d16018f6c
commit f25d735b9d
5 changed files with 111 additions and 115 deletions

View File

@@ -36,7 +36,7 @@ async function switchToNote(notePath) {
await loadNoteDetail(notePath);
openTabsChanged();
appContext.openTabsChanged();
}
function onNoteChange(func) {
@@ -87,7 +87,7 @@ async function activateOrOpenNote(noteId) {
async function loadNoteDetailToContext(ctx, note, notePath) {
await ctx.setNote(note, notePath);
openTabsChanged();
appContext.openTabsChanged();
fireDetailLoaded();
}
@@ -141,23 +141,6 @@ async function loadNote(noteId) {
return new NoteFull(treeCache, row, noteShort);
}
async function filterTabs(noteId) {
for (const tc of appContext.getTabContexts()) {
if (tc.notePath && !tc.notePath.split("/").includes(noteId)) {
await tabRow.removeTab(tc.$tab[0]);
}
}
if (appContext.getTabContexts().length === 0) {
await loadNoteDetail(noteId, {
newTab: true,
activate: true
});
}
await saveOpenTabs();
}
async function noteDeleted(noteId) {
for (const tc of appContext.getTabContexts()) {
// not removing active even if it contains deleted note since that one will move to another note (handled by deletion logic)
@@ -250,96 +233,6 @@ $tabContentsContainer.on("drop", async e => {
});
});
$(tabRow.el).on('contextmenu', '.note-tab', e => {
e.preventDefault();
const tab = $(e.target).closest(".note-tab");
contextMenuService.initContextMenu(e, {
getContextMenuItems: () => {
return [
{title: "Close all tabs", cmd: "removeAllTabs", uiIcon: "empty"},
{title: "Close all tabs except for this", cmd: "removeAllTabsExceptForThis", uiIcon: "empty"}
];
},
selectContextMenuItem: (e, cmd) => {
if (cmd === 'removeAllTabs') {
tabRow.removeAllTabs();
} else if (cmd === 'removeAllTabsExceptForThis') {
tabRow.removeAllTabsExceptForThis(tab[0]);
}
}
});
});
keyboardActionService.setGlobalActionHandler('OpenNewTab', () => {
openEmptyTab();
});
keyboardActionService.setGlobalActionHandler('CloseActiveTab', () => {
if (tabRow.activeTabEl) {
tabRow.removeTab(tabRow.activeTabEl);
}
});
keyboardActionService.setGlobalActionHandler('ActivateNextTab', () => {
const nextTab = tabRow.nextTabEl;
if (nextTab) {
tabRow.activateTab(nextTab);
}
});
keyboardActionService.setGlobalActionHandler('ActivatePreviousTab', () => {
const prevTab = tabRow.previousTabEl;
if (prevTab) {
tabRow.activateTab(prevTab);
}
});
tabRow.addListener('activeTabChange', openTabsChanged);
tabRow.addListener('tabRemove', openTabsChanged);
tabRow.addListener('tabReorder', openTabsChanged);
let tabsChangedTaskId = null;
function clearOpenTabsTask() {
if (tabsChangedTaskId) {
clearTimeout(tabsChangedTaskId);
}
}
function openTabsChanged() {
// we don't want to send too many requests with tab changes so we always schedule task to do this in 1 seconds,
// but if there's any change in between, we cancel the old one and schedule new one
// so effectively we kind of wait until user stopped e.g. quickly switching tabs
clearOpenTabsTask();
tabsChangedTaskId = setTimeout(saveOpenTabs, 1000);
}
async function saveOpenTabs() {
const openTabs = [];
for (const tabEl of tabRow.tabEls) {
const tabId = tabEl.getAttribute('data-tab-id');
const tabContext = appContext.getTabContexts().find(tc => tc.tabId === tabId);
if (tabContext) {
const tabState = tabContext.getTabState();
if (tabState) {
openTabs.push(tabState);
}
}
}
await server.put('options', {
openTabs: JSON.stringify(openTabs)
});
}
function noteChanged() {
const activeTabContext = appContext.getActiveTabContext();
@@ -367,10 +260,7 @@ export default {
addDetailLoadedListener,
getActiveEditor,
activateOrOpenNote,
clearOpenTabsTask,
filterTabs,
noteDeleted,
noteChanged,
openTabsChanged,
reloadNote
};