diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2020-03-29 21:10:47 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2020-03-29 21:11:13 +0900 |
commit | 0340c82bc82738a63c8a374930cf39cbed5c7c8c (patch) | |
tree | 12e13959351bd8c726258adc24a83b9e579be74a /src/background | |
parent | 1368fa2fc0f22d1ec4763c77c2ae983cf5037e92 (diff) |
Complete tab by an index and a flag
Diffstat (limited to 'src/background')
-rw-r--r-- | src/background/completion/TabCompletionUseCase.ts | 25 | ||||
-rw-r--r-- | src/background/completion/TabRepository.ts | 2 | ||||
-rw-r--r-- | src/background/completion/impl/TabRepositoryImpl.ts | 32 |
3 files changed, 48 insertions, 11 deletions
diff --git a/src/background/completion/TabCompletionUseCase.ts b/src/background/completion/TabCompletionUseCase.ts index 7e6dce3..dec86e9 100644 --- a/src/background/completion/TabCompletionUseCase.ts +++ b/src/background/completion/TabCompletionUseCase.ts @@ -1,6 +1,6 @@ import { inject, injectable } from "tsyringe"; import TabItem from "./TabItem"; -import TabRepository from "./TabRepository"; +import TabRepository, { Tab } from "./TabRepository"; import TabPresenter from "../presenters/TabPresenter"; import TabFlag from "../../shared/TabFlag"; @@ -14,7 +14,28 @@ export default class TabCompletionUseCase { async queryTabs(query: string, excludePinned: boolean): Promise<TabItem[]> { const lastTabId = await this.tabPresenter.getLastSelectedId(); - const tabs = await this.tabRepository.queryTabs(query, excludePinned); + const allTabs = await this.tabRepository.getAllTabs(excludePinned); + const num = parseInt(query, 10); + let tabs: Tab[] = []; + if (!isNaN(num)) { + const tab = allTabs.find(t => t.index === num - 1); + if (tab) { + tabs = [tab]; + } + } else if (query == '%') { + const tab = allTabs.find(t => t.active); + if (tab) { + tabs = [tab]; + } + } else if (query == '#') { + const tab = allTabs.find(t => t.id === lastTabId); + if (tab) { + tabs = [tab]; + } + } else { + tabs = await this.tabRepository.queryTabs(query, excludePinned); + } + return tabs.map(tab => { let flag = TabFlag.None; if (tab.active) { diff --git a/src/background/completion/TabRepository.ts b/src/background/completion/TabRepository.ts index 61fac3b..fe1b601 100644 --- a/src/background/completion/TabRepository.ts +++ b/src/background/completion/TabRepository.ts @@ -9,4 +9,6 @@ export type Tab = { export default interface TabRepository { queryTabs(query: string, excludePinned: boolean): Promise<Tab[]>; + + getAllTabs(excludePinned: boolean): Promise<Tab[]> } diff --git a/src/background/completion/impl/TabRepositoryImpl.ts b/src/background/completion/impl/TabRepositoryImpl.ts index 6692b27..adcaba7 100644 --- a/src/background/completion/impl/TabRepositoryImpl.ts +++ b/src/background/completion/impl/TabRepositoryImpl.ts @@ -15,13 +15,27 @@ export default class TabRepositoryImpl implements TabRepository { }) .filter(item => item.id && item.title && item.url) .slice(0, COMPLETION_ITEM_LIMIT) - .map(item => ({ - id: item.id!!, - url: item.url!!, - active: item.active, - title: item.title!!, - faviconUrl: item.favIconUrl, - index: item.index, - })) + .map(TabRepositoryImpl.toEntity); } -}
\ No newline at end of file + + async getAllTabs(excludePinned: boolean): Promise<Tab[]> { + if (excludePinned) { + return (await browser.tabs.query({ currentWindow: true, pinned: true })) + .map(TabRepositoryImpl.toEntity) + + } + return (await browser.tabs.query({ currentWindow: true })) + .map(TabRepositoryImpl.toEntity) + } + + private static toEntity(tab: browser.tabs.Tab,): Tab { + return { + id: tab.id!!, + url: tab.url!!, + active: tab.active, + title: tab.title!!, + faviconUrl: tab.favIconUrl, + index: tab.index, + } + } +} |