refactored TabContext => NoteContext

This commit is contained in:
zadam 2021-05-22 13:04:08 +02:00
parent 1a0aaf4a30
commit 98dfc77195
4 changed files with 29 additions and 21 deletions

View File

@ -27,7 +27,7 @@ async function setupActionsForElement(scope, $el, component) {
for (const action of actions) {
for (const shortcut of action.effectiveShortcuts) {
utils.bindElShortcut($el, shortcut, () => component.triggerCommand(action.actionName, {ntxId: appContext.tabManager.activeTabId}));
utils.bindElShortcut($el, shortcut, () => component.triggerCommand(action.actionName, {ntxId: appContext.tabManager.activeNtxId}));
}
}
}
@ -35,7 +35,7 @@ async function setupActionsForElement(scope, $el, component) {
getActionsForScope("window").then(actions => {
for (const action of actions) {
for (const shortcut of action.effectiveShortcuts) {
utils.bindGlobalShortcut(shortcut, () => appContext.triggerCommand(action.actionName, {ntxId: appContext.tabManager.activeTabId}));
utils.bindGlobalShortcut(shortcut, () => appContext.triggerCommand(action.actionName, {ntxId: appContext.tabManager.activeNtxId}));
}
}
});

View File

@ -60,11 +60,11 @@ class NoteContext extends Component {
}
}
getAllSubNoteContexts() {
getSubContexts() {
return appContext.tabManager.noteContexts.filter(nc => nc.ntxId === this.ntxId || nc.mainNtxId === this.ntxId);
}
getMainNoteContext() {
getMainContext() {
if (this.mainNtxId) {
return appContext.tabManager.getNoteContextById(this.mainNtxId);
}
@ -138,7 +138,7 @@ class NoteContext extends Component {
}
isActive() {
return appContext.tabManager.activeTabId === this.ntxId;
return appContext.tabManager.activeNtxId === this.ntxId;
}
getTabState() {

View File

@ -12,7 +12,7 @@ export default class TabManager extends Component {
constructor() {
super();
this.activeTabId = null;
this.activeNtxId = null;
this.tabsUpdate = new SpacedUpdate(async () => {
if (!appContext.isMainWindow) {
@ -156,8 +156,15 @@ export default class TabManager extends Component {
/** @returns {NoteContext} */
getActiveContext() {
return this.activeTabId
? this.getNoteContextById(this.activeTabId)
return this.activeNtxId
? this.getNoteContextById(this.activeNtxId)
: null;
}
/** @returns {NoteContext} */
getActiveMainContext() {
return this.activeNtxId
? this.getNoteContextById(this.activeNtxId).getMainContext()
: null;
}
@ -271,11 +278,11 @@ export default class TabManager extends Component {
}
activateNoteContext(ntxId, triggerEvent = true) {
if (ntxId === this.activeTabId) {
if (ntxId === this.activeNtxId) {
return;
}
this.activeTabId = ntxId;
this.activeNtxId = ntxId;
if (triggerEvent) {
this.triggerEvent('activeTabChanged', {
@ -289,19 +296,19 @@ export default class TabManager extends Component {
}
async removeNoteContext(ntxId) {
const mainNoteContextToRemove = this.getNoteContextById(ntxId).getMainNoteContext();
const mainNoteContextToRemove = this.getNoteContextById(ntxId).getMainContext();
// close dangling autocompletes after closing the tab
$(".aa-input").autocomplete("close");
const ntxIdsToRemove = mainNoteContextToRemove.getAllSubNoteContexts().map(nc => nc.ntxId);
const ntxIdsToRemove = mainNoteContextToRemove.getSubContexts().map(nc => nc.ntxId);
await this.triggerEvent('beforeTabRemove', { ntxIds: ntxIdsToRemove });
if (this.mainNoteContexts.length <= 1) {
await this.openAndActivateEmptyTab();
}
else if (ntxIdsToRemove.includes(this.activeTabId)) {
else if (ntxIdsToRemove.includes(this.activeNtxId)) {
const idx = this.mainNoteContexts.findIndex(nc => nc.ntxId === mainNoteContextToRemove.ntxId);
if (idx === this.mainNoteContexts.length - 1) {
@ -332,21 +339,21 @@ export default class TabManager extends Component {
}
activateNextTabCommand() {
const oldIdx = this.mainNoteContexts.findIndex(nc => nc.ntxId === this.activeTabId);
const oldIdx = this.mainNoteContexts.findIndex(nc => nc.ntxId === this.activeNtxId);
const newActiveTabId = this.mainNoteContexts[oldIdx === this.noteContexts.length - 1 ? 0 : oldIdx + 1].ntxId;
this.activateNoteContext(newActiveTabId);
}
activatePreviousTabCommand() {
const oldIdx = this.mainNoteContexts.findIndex(nc => nc.ntxId === this.activeTabId);
const oldIdx = this.mainNoteContexts.findIndex(nc => nc.ntxId === this.activeNtxId);
const newActiveTabId = this.mainNoteContexts[oldIdx === 0 ? this.noteContexts.length - 1 : oldIdx - 1].ntxId;
this.activateNoteContext(newActiveTabId);
}
closeActiveTabCommand() {
this.removeNoteContext(this.activeTabId);
this.removeNoteContext(this.activeNtxId);
}
beforeUnloadEvent() {

View File

@ -49,13 +49,14 @@ export default class PaneContainer extends FlexContainer {
toggleInt(show) {} // not needed
toggleExt(show) {
const activeTabId = appContext.tabManager.getActiveContext().getMainNoteContext().ntxId;
const activeMainContext = appContext.tabManager.getActiveMainContext();
const activeNtxId = activeMainContext ? activeMainContext.ntxId : null;
for (const ntxId in this.widgets) {
const noteContext = appContext.tabManager.getNoteContextById(ntxId);
const widget = this.widgets[ntxId];
widget.toggleExt(show && activeTabId && [noteContext.ntxId, noteContext.mainNtxId].includes(activeTabId));
widget.toggleExt(show && activeNtxId && [noteContext.ntxId, noteContext.mainNtxId].includes(activeNtxId));
if (!widget.hasBeenAlreadyShown) {
widget.handleEvent('activeTabChanged', {noteContext});
@ -79,11 +80,11 @@ export default class PaneContainer extends FlexContainer {
const promises = [];
if (appContext.tabManager.getActiveContext().getMainNoteContext() === data.noteContext.getMainNoteContext()) {
if (appContext.tabManager.getActiveMainContext() === data.noteContext.getMainContext()) {
promises.push(widget.handleEvent('activeTabChanged', data));
}
for (const subNoteContext of data.noteContext.getMainNoteContext().getAllSubNoteContexts()) {
for (const subNoteContext of data.noteContext.getMainContext().getSubContexts()) {
const subWidget = this.widgets[subNoteContext.ntxId];
if (!subWidget) {
@ -112,7 +113,7 @@ export default class PaneContainer extends FlexContainer {
if (name === 'activeTabChanged') {
const promises = [];
for (const subNoteContext of data.noteContext.getMainNoteContext().getAllSubNoteContexts()) {
for (const subNoteContext of data.noteContext.getMainContext().getSubContexts()) {
console.log("subNoteContext", subNoteContext);
const widget = this.widgets[subNoteContext.ntxId];