diff options
Diffstat (limited to 'src/background/usecases/TabSelectUseCase.ts')
-rw-r--r-- | src/background/usecases/TabSelectUseCase.ts | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/background/usecases/TabSelectUseCase.ts b/src/background/usecases/TabSelectUseCase.ts new file mode 100644 index 0000000..a0b52f0 --- /dev/null +++ b/src/background/usecases/TabSelectUseCase.ts @@ -0,0 +1,53 @@ +import TabPresenter from '../presenters/TabPresenter'; + +export default class TabSelectUseCase { + private tabPresenter: TabPresenter; + + constructor() { + this.tabPresenter = new TabPresenter(); + } + + async selectPrev(count: number): Promise<any> { + let tabs = await this.tabPresenter.getAll(); + if (tabs.length < 2) { + return; + } + let tab = tabs.find(t => t.active); + if (!tab) { + return; + } + let select = (tab.index - count + tabs.length) % tabs.length; + return this.tabPresenter.select(tabs[select].id as number); + } + + async selectNext(count: number): Promise<any> { + let tabs = await this.tabPresenter.getAll(); + if (tabs.length < 2) { + return; + } + let tab = tabs.find(t => t.active); + if (!tab) { + return; + } + let select = (tab.index + count) % tabs.length; + return this.tabPresenter.select(tabs[select].id as number); + } + + async selectFirst(): Promise<any> { + let tabs = await this.tabPresenter.getAll(); + return this.tabPresenter.select(tabs[0].id as number); + } + + async selectLast(): Promise<any> { + let tabs = await this.tabPresenter.getAll(); + return this.tabPresenter.select(tabs[tabs.length - 1].id as number); + } + + async selectPrevSelected(): Promise<any> { + let tabId = await this.tabPresenter.getLastSelectedId(); + if (tabId === null || typeof tabId === 'undefined') { + return Promise.resolve(); + } + return this.tabPresenter.select(tabId); + } +} |