diff options
-rw-r--r-- | src/background/completion/BookmarkRepository.ts | 32 | ||||
-rw-r--r-- | src/background/completion/CompletionUseCase.ts | 39 | ||||
-rw-r--r-- | src/background/completion/HistoryRepository.ts | 32 | ||||
-rw-r--r-- | src/background/completion/filters.ts (renamed from src/background/usecases/filters.ts) | 0 | ||||
-rw-r--r-- | src/background/repositories/CompletionsRepository.ts | 24 | ||||
-rw-r--r-- | src/background/usecases/CompletionsUseCase.ts | 55 |
6 files changed, 123 insertions, 59 deletions
diff --git a/src/background/completion/BookmarkRepository.ts b/src/background/completion/BookmarkRepository.ts new file mode 100644 index 0000000..12f5455 --- /dev/null +++ b/src/background/completion/BookmarkRepository.ts @@ -0,0 +1,32 @@ +import { injectable } from "tsyringe"; + +export type BookmarkItem = { + title: string + url: string +} + +const COMPLETION_ITEM_LIMIT = 10; + +@injectable() +export default class BookmarkRepository { + async queryBookmarks(query: string): Promise<BookmarkItem[]> { + const items = await browser.bookmarks.search({ query }); + return items + .filter(item => item.title && item.title.length > 0) + .filter(item => item.type === 'bookmark' && item.url) + .filter((item) => { + let url = undefined; + try { + url = new URL(item.url!!); + } catch (e) { + return false; + } + return url.protocol !== 'place:'; + }) + .slice(0, COMPLETION_ITEM_LIMIT) + .map(item => ({ + title: item.title!!, + url: item.url!!, + })); + } +} diff --git a/src/background/completion/CompletionUseCase.ts b/src/background/completion/CompletionUseCase.ts new file mode 100644 index 0000000..898efd4 --- /dev/null +++ b/src/background/completion/CompletionUseCase.ts @@ -0,0 +1,39 @@ +import { inject, injectable } from "tsyringe"; +import HistoryRepository from "./HistoryRepository"; +import BookmarkRepository from "./BookmarkRepository"; +import CachedSettingRepository from "../repositories/CachedSettingRepository"; + +export type BookmarkItem = { + title: string + url: string +} + +export type HistoryItem = { + title: string + url: string +} + +@injectable() +export default class CompletionUseCase { + constructor( + private bookmarkRepository: BookmarkRepository, + private historyRepository: HistoryRepository, + @inject("CachedSettingRepository") + private cachedSettingRepository: CachedSettingRepository, + ) { + } + + async requestSearchEngines(query: string): Promise<string[]> { + const settings = await this.cachedSettingRepository.get(); + return Object.keys(settings.search.engines) + .filter(key => key.startsWith(query)) + } + + requestBookmarks(query: any): Promise<BookmarkItem[]> { + return this.bookmarkRepository.queryBookmarks(query); + } + + async requestHistory(query: string): Promise<HistoryItem[]> { + return this.historyRepository.queryHistories(query); + } +}
\ No newline at end of file diff --git a/src/background/completion/HistoryRepository.ts b/src/background/completion/HistoryRepository.ts new file mode 100644 index 0000000..1cfaf1b --- /dev/null +++ b/src/background/completion/HistoryRepository.ts @@ -0,0 +1,32 @@ +import * as filters from "./filters"; +import { injectable } from "tsyringe"; + +export type HistoryItem = { + title: string + url: string +} + +const COMPLETION_ITEM_LIMIT = 10; + +@injectable() +export default class HistoryRepository { + async queryHistories(keywords: string): Promise<HistoryItem[]> { + const items = await browser.history.search({ + text: keywords, + startTime: 0, + }); + + return [items] + .map(filters.filterBlankTitle) + .map(filters.filterHttp) + .map(filters.filterByTailingSlash) + .map(pages => filters.filterByPathname(pages, COMPLETION_ITEM_LIMIT)) + .map(pages => filters.filterByOrigin(pages, COMPLETION_ITEM_LIMIT))[0] + .sort((x, y) => Number(y.visitCount) - Number(x.visitCount)) + .slice(0, COMPLETION_ITEM_LIMIT) + .map(item => ({ + title: item.title!!, + url: item.url!!, + })) + } +} diff --git a/src/background/usecases/filters.ts b/src/background/completion/filters.ts index 98957a7..98957a7 100644 --- a/src/background/usecases/filters.ts +++ b/src/background/completion/filters.ts diff --git a/src/background/repositories/CompletionsRepository.ts b/src/background/repositories/CompletionsRepository.ts index dfecff0..7d0434c 100644 --- a/src/background/repositories/CompletionsRepository.ts +++ b/src/background/repositories/CompletionsRepository.ts @@ -1,33 +1,9 @@ import { injectable } from 'tsyringe'; type Tab = browser.tabs.Tab; -type BookmarkTreeNode = browser.bookmarks.BookmarkTreeNode; @injectable() export default class CompletionsRepository { - async queryBookmarks(keywords: string): Promise<BookmarkTreeNode[]> { - const items = await browser.bookmarks.search({ query: keywords }); - return items.filter((item) => { - if (!item.url) { - return false; - } - let url = undefined; - try { - url = new URL(item.url); - } catch (e) { - return false; - } - return item.type === 'bookmark' && url.protocol !== 'place:'; - }); - } - - queryHistories(keywords: string): Promise<browser.history.HistoryItem[]> { - return browser.history.search({ - text: keywords, - startTime: 0, - }); - } - async queryTabs(keywords: string, excludePinned: boolean): Promise<Tab[]> { const tabs = await browser.tabs.query({ currentWindow: true }); return tabs.filter((t) => { diff --git a/src/background/usecases/CompletionsUseCase.ts b/src/background/usecases/CompletionsUseCase.ts index 9874644..b75b635 100644 --- a/src/background/usecases/CompletionsUseCase.ts +++ b/src/background/usecases/CompletionsUseCase.ts @@ -2,15 +2,12 @@ import { injectable, inject } from 'tsyringe'; import CompletionGroup from '../domains/CompletionGroup'; import CommandDocs from '../domains/CommandDocs'; import CompletionsRepository from '../repositories/CompletionsRepository'; -import * as filters from './filters'; import CachedSettingRepository from '../repositories/CachedSettingRepository'; import TabPresenter from '../presenters/TabPresenter'; import Properties from '../../shared/settings/Properties'; - -const COMPLETION_ITEM_LIMIT = 10; +import CompletionUseCase from "../completion/CompletionUseCase"; type Tab = browser.tabs.Tab; -type HistoryItem = browser.history.HistoryItem; @injectable() export default class CompletionsUseCase { @@ -18,6 +15,7 @@ export default class CompletionsUseCase { private tabPresenter: TabPresenter, private completionsRepository: CompletionsRepository, @inject("CachedSettingRepository") private cachedSettingRepository: CachedSettingRepository, + private completionUseCase: CompletionUseCase ) { } @@ -179,42 +177,29 @@ export default class CompletionsUseCase { return [{ name: 'Buffers', items }]; } - async querySearchEngineItems(name: string, keywords: string) { - const settings = await this.cachedSettingRepository.get(); - const engines = Object.keys(settings.search.engines) - .filter(key => key.startsWith(keywords)); - return engines.map(key => ({ - caption: key, - content: name + ' ' + key, + async querySearchEngineItems(name: string, query: string) { + const engines = await this.completionUseCase.requestSearchEngines(query); + return engines.map(item => ({ + caption: item, + content: name + ' ' + item, })); } - async queryHistoryItems(name: string, keywords: string) { - let histories = await this.completionsRepository.queryHistories(keywords); - histories = [histories] - .map(filters.filterBlankTitle) - .map(filters.filterHttp) - .map(filters.filterByTailingSlash) - .map(pages => filters.filterByPathname(pages, COMPLETION_ITEM_LIMIT)) - .map(pages => filters.filterByOrigin(pages, COMPLETION_ITEM_LIMIT))[0] - .sort((x: HistoryItem, y: HistoryItem): number => { - return Number(y.visitCount) - Number(x.visitCount); - }) - .slice(0, COMPLETION_ITEM_LIMIT); - return histories.map(page => ({ - caption: page.title, - content: name + ' ' + page.url, - url: page.url + async queryHistoryItems(name: string, query: string) { + const items = await this.completionUseCase.requestHistory(query); + return items.map(item => ({ + caption: item.title, + content: name + ' ' + item.url, + url: item.url })); } - async queryBookmarkItems(name: string, keywords: string) { - const bookmarks = await this.completionsRepository.queryBookmarks(keywords); - return bookmarks.slice(0, COMPLETION_ITEM_LIMIT) - .map(page => ({ - caption: page.title, - content: name + ' ' + page.url, - url: page.url - })); + async queryBookmarkItems(name: string, query: string) { + const items = await this.completionUseCase.requestHistory(query); + return items.map(item => ({ + caption: item.title, + content: name + ' ' + item.url, + url: item.url + })); } } |