refactoring, tab switching handling shouldn't be on the widget level

This commit is contained in:
zadam
2023-09-13 09:01:55 +02:00
parent 7848c7e319
commit d46801ff1f
9 changed files with 553 additions and 52 deletions

View File

@@ -105,11 +105,24 @@ class NoteContext extends Component {
return appContext.tabManager.noteContexts.filter(nc => nc.ntxId === this.ntxId || nc.mainNtxId === this.ntxId);
}
/**
* A main context represents a tab and also the first split. Further splits are the children contexts of the main context.
* Imagine you have a tab with 3 splits, each showing notes A, B, C (in this order).
* In such a scenario, A context is the main context (also representing the tab as a whole), and B, C are the children
* of context A.
*
* @returns {boolean} true if the context is main (= tab)
*/
isMainContext() {
// if null, then this is a main context
return !this.mainNtxId;
}
/**
* See docs for isMainContext() for better explanation.
*
* @returns {NoteContext}
*/
getMainContext() {
if (this.mainNtxId) {
try {

View File

@@ -154,4 +154,24 @@ export default class RootCommandExecutor extends Component {
});
}
}
firstTabCommand() {this.#goToTab(1);}
secondTabCommand() {this.#goToTab(2);}
thirdTabCommand() {this.#goToTab(3);}
fourthTabCommand() {this.#goToTab(4);}
fifthTabCommand() {this.#goToTab(5);}
sixthTabCommand() {this.#goToTab(6);}
seventhTabCommand() {this.#goToTab(7);}
eigthTabCommand() {this.#goToTab(8);}
ninthTabCommand() {this.#goToTab(9);}
lastTabCommand() {this.#goToTab(Number.POSITIVE_INFINITY);}
#goToTab(tabNumber) {
const mainNoteContexts = appContext.tabManager.getMainNoteContexts();
const index = tabNumber === Number.POSITIVE_INFINITY ? mainNoteContexts.length - 1 : tabNumber - 1;
const tab = mainNoteContexts[index];
appContext.tabManager.activateNoteContext(tab.ntxId);
}
}

View File

@@ -173,7 +173,10 @@ export default class TabManager extends Component {
return this.noteContexts;
}
/** @returns {NoteContext[]} */
/**
* Main context is essentially a tab (children are splits), so this returns tabs.
* @returns {NoteContext[]}
*/
getMainNoteContexts() {
return this.noteContexts.filter(nc => nc.isMainContext());
}
@@ -189,14 +192,22 @@ export default class TabManager extends Component {
return noteContext;
}
/** @returns {NoteContext} */
/**
* Get active context which represents the visible split with focus. Active context can, but doesn't have to be "main".
*
* @returns {NoteContext}
*/
getActiveContext() {
return this.activeNtxId
? this.getNoteContextById(this.activeNtxId)
: null;
}
/** @returns {NoteContext} */
/**
* Get active main context which corresponds to the active tab.
*
* @returns {NoteContext}
*/
getActiveMainContext() {
return this.activeNtxId
? this.getNoteContextById(this.activeNtxId).getMainContext()

View File

@@ -349,10 +349,34 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null, $contain
/**
* @method
* @returns {FNote} active note (loaded into right pane)
* @returns {FNote} active note (loaded into center pane)
*/
this.getActiveContextNote = () => appContext.tabManager.getActiveContextNote();
/**
* @method
* @returns {NoteContext} - returns active context (split)
*/
this.getActiveContext = () => appContext.tabManager.getActiveContext();
/**
* @method
* @returns {NoteContext} - returns active main context (first split in a tab, represents the tab as a whole)
*/
this.getActiveMainContext = () => appContext.tabManager.getActiveMainContext();
/**
* @method
* @returns {NoteContext[]} - returns all note contexts (splits) in all tabs
*/
this.getNoteContexts = () => appContext.tabManager.getNoteContexts();
/**
* @method
* @returns {NoteContext[]} - returns all main contexts representing tabs
*/
this.getMainNoteContexts = () => appContext.tabManager.getMainNoteContexts();
/**
* See https://ckeditor.com/docs/ckeditor5/latest/api/module_core_editor_editor-Editor.html for documentation on the returned instance.
*

View File

@@ -267,28 +267,8 @@ export default class TabRowWidget extends BasicWidget {
}
});
});
keyboardActionService.setupActionsForElement('window', $(document), this);
}
goToTab(tabNumber) {
const index = tabNumber === Number.POSITIVE_INFINITY ? this.tabEls.length - 1 : tabNumber - 1;
const tab = this.tabEls[index];
if (!tab) return;
appContext.tabManager.activateNoteContext(tab.getAttribute('data-ntx-id'));
}
firstTabCommand() {this.goToTab(1);}
secondTabCommand() {this.goToTab(2);}
thirdTabCommand() {this.goToTab(3);}
fourthTabCommand() {this.goToTab(4);}
fifthTabCommand() {this.goToTab(5);}
sixthTabCommand() {this.goToTab(6);}
seventhTabCommand() {this.goToTab(7);}
eigthTabCommand() {this.goToTab(8);}
ninthTabCommand() {this.goToTab(9);}
lastTabCommand() {this.goToTab(Number.POSITIVE_INFINITY);}
setupStyle() {
this.$style = $("<style>");
this.$widget.append(this.$style);
@@ -637,7 +617,7 @@ export default class TabRowWidget extends BasicWidget {
// update tab id for the new main context
this.getTabById(oldMainNtxId).attr("data-ntx-id", newMainNtxId);
this.updateTabById(newMainNtxId);
this.updateTabById(newMainNtxId);
}
contextsReopenedEvent({mainNtxId, tabPosition}) {