aboutsummaryrefslogtreecommitdiff
path: root/src/background/completion
diff options
context:
space:
mode:
Diffstat (limited to 'src/background/completion')
-rw-r--r--src/background/completion/OpenCompletionUseCase.ts (renamed from src/background/completion/CompletionUseCase.ts)2
-rw-r--r--src/background/completion/TabCompletionUseCase.ts34
-rw-r--r--src/background/completion/TabItem.ts11
-rw-r--r--src/background/completion/TabRepository.ts12
-rw-r--r--src/background/completion/impl/TabRepositoryImpl.ts27
5 files changed, 85 insertions, 1 deletions
diff --git a/src/background/completion/CompletionUseCase.ts b/src/background/completion/OpenCompletionUseCase.ts
index f7531e7..1b63e7c 100644
--- a/src/background/completion/CompletionUseCase.ts
+++ b/src/background/completion/OpenCompletionUseCase.ts
@@ -15,7 +15,7 @@ export type HistoryItem = {
}
@injectable()
-export default class CompletionUseCase {
+export default class OpenCompletionUseCase {
constructor(
@inject('BookmarkRepository') private bookmarkRepository: BookmarkRepository,
@inject('HistoryRepository') private historyRepository: HistoryRepository,
diff --git a/src/background/completion/TabCompletionUseCase.ts b/src/background/completion/TabCompletionUseCase.ts
new file mode 100644
index 0000000..7e6dce3
--- /dev/null
+++ b/src/background/completion/TabCompletionUseCase.ts
@@ -0,0 +1,34 @@
+import { inject, injectable } from "tsyringe";
+import TabItem from "./TabItem";
+import TabRepository from "./TabRepository";
+import TabPresenter from "../presenters/TabPresenter";
+import TabFlag from "../../shared/TabFlag";
+
+@injectable()
+export default class TabCompletionUseCase {
+ constructor(
+ @inject('TabRepository') private tabRepository: TabRepository,
+ @inject('TabPresenter') private tabPresenter: TabPresenter,
+ ) {
+ }
+
+ async queryTabs(query: string, excludePinned: boolean): Promise<TabItem[]> {
+ const lastTabId = await this.tabPresenter.getLastSelectedId();
+ const tabs = await this.tabRepository.queryTabs(query, excludePinned);
+ return tabs.map(tab => {
+ let flag = TabFlag.None;
+ if (tab.active) {
+ flag = TabFlag.CurrentTab
+ } else if (tab.id == lastTabId) {
+ flag = TabFlag.LastTab
+ }
+ return {
+ index: tab.index + 1,
+ flag: flag,
+ title: tab.title,
+ url: tab.url,
+ faviconUrl : tab.faviconUrl
+ }
+ });
+ }
+}
diff --git a/src/background/completion/TabItem.ts b/src/background/completion/TabItem.ts
new file mode 100644
index 0000000..630855a
--- /dev/null
+++ b/src/background/completion/TabItem.ts
@@ -0,0 +1,11 @@
+import TabFlag from "../../shared/TabFlag";
+
+type TabItem = {
+ index: number
+ flag: TabFlag
+ title: string
+ url: string
+ faviconUrl?: string
+}
+
+export default TabItem; \ No newline at end of file
diff --git a/src/background/completion/TabRepository.ts b/src/background/completion/TabRepository.ts
new file mode 100644
index 0000000..61fac3b
--- /dev/null
+++ b/src/background/completion/TabRepository.ts
@@ -0,0 +1,12 @@
+export type Tab = {
+ id: number
+ index: number
+ active: boolean
+ title: string
+ url: string
+ faviconUrl?: string
+}
+
+export default interface TabRepository {
+ queryTabs(query: string, excludePinned: boolean): Promise<Tab[]>;
+}
diff --git a/src/background/completion/impl/TabRepositoryImpl.ts b/src/background/completion/impl/TabRepositoryImpl.ts
new file mode 100644
index 0000000..6692b27
--- /dev/null
+++ b/src/background/completion/impl/TabRepositoryImpl.ts
@@ -0,0 +1,27 @@
+import TabRepository, { Tab } from "../TabRepository";
+
+const COMPLETION_ITEM_LIMIT = 10;
+
+export default class TabRepositoryImpl implements TabRepository {
+ async queryTabs(query: string, excludePinned: boolean): Promise<Tab[]> {
+ const tabs = await browser.tabs.query({ currentWindow: true });
+ return tabs
+ .filter((t) => {
+ return t.url && t.url.toLowerCase().includes(query.toLowerCase()) ||
+ t.title && t.title.toLowerCase().includes(query.toLowerCase());
+ })
+ .filter((t) => {
+ return !(excludePinned && t.pinned);
+ })
+ .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,
+ }))
+ }
+} \ No newline at end of file