diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2020-04-09 10:38:37 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-09 10:38:37 +0900 |
commit | 1656d52d2cefb3846d968c6117484e6aefe7dabe (patch) | |
tree | ab58a99b832d2571e2168f2ee0e328bc12d9580e /src/background/usecases | |
parent | c6c2da8547891b50aef2f08e5f36d258183831ff (diff) | |
parent | 5176643e64d8f4a6be5fc73f0eb48dc65322e496 (diff) |
Merge pull request #730 from ueokande/refactor-console-and-completion
Refactor console and completions
Diffstat (limited to 'src/background/usecases')
-rw-r--r-- | src/background/usecases/AddonEnabledUseCase.ts | 4 | ||||
-rw-r--r-- | src/background/usecases/CommandUseCase.ts | 4 | ||||
-rw-r--r-- | src/background/usecases/CompletionsUseCase.ts | 220 | ||||
-rw-r--r-- | src/background/usecases/ConsoleUseCase.ts | 4 | ||||
-rw-r--r-- | src/background/usecases/FindUseCase.ts | 4 | ||||
-rw-r--r-- | src/background/usecases/LinkUseCase.ts | 4 | ||||
-rw-r--r-- | src/background/usecases/MarkUseCase.ts | 4 | ||||
-rw-r--r-- | src/background/usecases/NavigateUseCase.ts | 4 | ||||
-rw-r--r-- | src/background/usecases/TabSelectUseCase.ts | 4 | ||||
-rw-r--r-- | src/background/usecases/TabUseCase.ts | 4 | ||||
-rw-r--r-- | src/background/usecases/VersionUseCase.ts | 2 | ||||
-rw-r--r-- | src/background/usecases/ZoomUseCase.ts | 4 | ||||
-rw-r--r-- | src/background/usecases/filters.ts | 76 |
13 files changed, 21 insertions, 317 deletions
diff --git a/src/background/usecases/AddonEnabledUseCase.ts b/src/background/usecases/AddonEnabledUseCase.ts index 9abd3dc..51f02e1 100644 --- a/src/background/usecases/AddonEnabledUseCase.ts +++ b/src/background/usecases/AddonEnabledUseCase.ts @@ -1,4 +1,4 @@ -import { injectable } from 'tsyringe'; +import { inject, injectable } from 'tsyringe'; import IndicatorPresenter from '../presenters/IndicatorPresenter'; import TabPresenter from '../presenters/TabPresenter'; import ContentMessageClient from '../infrastructures/ContentMessageClient'; @@ -7,7 +7,7 @@ import ContentMessageClient from '../infrastructures/ContentMessageClient'; export default class AddonEnabledUseCase { constructor( private indicatorPresentor: IndicatorPresenter, - private tabPresenter: TabPresenter, + @inject("TabPresenter") private tabPresenter: TabPresenter, private contentMessageClient: ContentMessageClient, ) { this.indicatorPresentor.onClick((tab) => { diff --git a/src/background/usecases/CommandUseCase.ts b/src/background/usecases/CommandUseCase.ts index 7dba664..d2d707e 100644 --- a/src/background/usecases/CommandUseCase.ts +++ b/src/background/usecases/CommandUseCase.ts @@ -14,7 +14,7 @@ import RepeatUseCase from '../usecases/RepeatUseCase'; @injectable() export default class CommandIndicator { constructor( - private tabPresenter: TabPresenter, + @inject('TabPresenter') private tabPresenter: TabPresenter, private windowPresenter: WindowPresenter, private helpPresenter: HelpPresenter, @inject("CachedSettingRepository") private cachedSettingRepository: CachedSettingRepository, @@ -80,7 +80,7 @@ export default class CommandIndicator { } const current = await this.tabPresenter.getCurrent(); - const tabs = await this.tabPresenter.getByKeyword(keywords); + const tabs = await this.tabPresenter.getByKeyword(keywords, false); if (tabs.length === 0) { throw new RangeError('No matching buffer for ' + keywords); } diff --git a/src/background/usecases/CompletionsUseCase.ts b/src/background/usecases/CompletionsUseCase.ts deleted file mode 100644 index 9874644..0000000 --- a/src/background/usecases/CompletionsUseCase.ts +++ /dev/null @@ -1,220 +0,0 @@ -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; - -type Tab = browser.tabs.Tab; -type HistoryItem = browser.history.HistoryItem; - -@injectable() -export default class CompletionsUseCase { - constructor( - private tabPresenter: TabPresenter, - private completionsRepository: CompletionsRepository, - @inject("CachedSettingRepository") private cachedSettingRepository: CachedSettingRepository, - ) { - } - - queryConsoleCommand(prefix: string): Promise<CompletionGroup[]> { - const keys = Object.keys(CommandDocs); - const items = keys - .filter(name => name.startsWith(prefix)) - .map(name => ({ - caption: name, - content: name, - url: CommandDocs[name], - })); - - if (items.length === 0) { - return Promise.resolve([]); - } - return Promise.resolve([{ name: 'Console Command', items }]); - } - - async queryOpen(name: string, keywords: string): Promise<CompletionGroup[]> { - // TODO This logic contains view entities. They should be defined on - // content script - - const settings = await this.cachedSettingRepository.get(); - const groups: CompletionGroup[] = []; - - const complete = settings.properties.complete; - for (const c of complete) { - if (c === 's') { - // eslint-disable-next-line no-await-in-loop - const engines = await this.querySearchEngineItems(name, keywords); - if (engines.length > 0) { - groups.push({ name: 'Search Engines', items: engines }); - } - // browser.history not supported on Android - } else if (c === 'h' && typeof browser.history === 'object') { - // eslint-disable-next-line no-await-in-loop - const histories = await this.queryHistoryItems(name, keywords); - if (histories.length > 0) { - groups.push({ name: 'History', items: histories }); - } - // browser.bookmarks not supported on Android - } else if (c === 'b' && typeof browser.bookmarks === 'object') { - // eslint-disable-next-line no-await-in-loop - const bookmarks = await this.queryBookmarkItems(name, keywords); - if (bookmarks.length > 0) { - groups.push({ name: 'Bookmarks', items: bookmarks }); - } - } - } - return groups; - } - - // eslint-disable-next-line max-statements - async queryBuffer( - name: string, - keywords: string, - ): Promise<CompletionGroup[]> { - const lastId = await this.tabPresenter.getLastSelectedId(); - const trimmed = keywords.trim(); - let tabs: Tab[] = []; - if (trimmed.length > 0 && !isNaN(Number(trimmed))) { - const all = await this.tabPresenter.getAll(); - const index = parseInt(trimmed, 10) - 1; - if (index >= 0 && index < all.length) { - tabs = [all[index]]; - } - } else if (trimmed === '%') { - const all = await this.tabPresenter.getAll(); - const tab = all.find(t => t.active) as Tab; - tabs = [tab]; - } else if (trimmed === '#') { - if (typeof lastId !== 'undefined' && lastId !== null) { - const all = await this.tabPresenter.getAll(); - const tab = all.find(t => t.id === lastId) as Tab; - tabs = [tab]; - } - } else { - tabs = await this.completionsRepository.queryTabs(keywords, false); - } - const flag = (tab: Tab) => { - if (tab.active) { - return '%'; - } else if (tab.id === lastId) { - return '#'; - } - return ' '; - }; - const items = tabs.map(tab => ({ - caption: tab.index + 1 + ': ' + flag(tab) + ' ' + tab.title, - content: name + ' ' + tab.title, - url: tab.url, - icon: tab.favIconUrl, - })); - if (items.length === 0) { - return Promise.resolve([]); - } - return [{ name: 'Buffers', items }]; - } - - queryBdelete(name: string, keywords: string): Promise<CompletionGroup[]> { - return this.queryTabs(name, true, keywords); - } - - queryBdeleteForce( - name: string, keywords: string, - ): Promise<CompletionGroup[]> { - return this.queryTabs(name, false, keywords); - } - - querySet(name: string, keywords: string): Promise<CompletionGroup[]> { - const items = Properties.defs().map((def) => { - if (def.type === 'boolean') { - return [ - { - caption: def.name, - content: name + ' ' + def.name, - url: 'Enable ' + def.description, - }, { - caption: 'no' + def.name, - content: name + ' no' + def.name, - url: 'Disable ' + def.description - } - ]; - } - return [ - { - caption: def.name, - content: name + ' ' + def.name, - url: 'Set ' + def.description, - } - ]; - }); - let flatten = items.reduce((acc, val) => acc.concat(val), []); - flatten = flatten.filter((item) => { - return item.caption.startsWith(keywords); - }); - if (flatten.length === 0) { - return Promise.resolve([]); - } - return Promise.resolve( - [{ name: 'Properties', items: flatten }], - ); - } - - async queryTabs( - name: string, excludePinned: boolean, args: string, - ): Promise<CompletionGroup[]> { - const tabs = await this.completionsRepository.queryTabs(args, excludePinned); - const items = tabs.map(tab => ({ - caption: tab.title, - content: name + ' ' + tab.title, - url: tab.url, - icon: tab.favIconUrl - })); - if (items.length === 0) { - return Promise.resolve([]); - } - 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 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 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 - })); - } -} diff --git a/src/background/usecases/ConsoleUseCase.ts b/src/background/usecases/ConsoleUseCase.ts index 775a1e0..2de5bc1 100644 --- a/src/background/usecases/ConsoleUseCase.ts +++ b/src/background/usecases/ConsoleUseCase.ts @@ -1,4 +1,4 @@ -import { injectable } from 'tsyringe'; +import { inject, injectable } from 'tsyringe'; import TabPresenter from '../presenters/TabPresenter'; import ConsoleClient from '../infrastructures/ConsoleClient'; @@ -6,7 +6,7 @@ import ConsoleClient from '../infrastructures/ConsoleClient'; export default class ConsoleUseCase { constructor( - private tabPresenter: TabPresenter, + @inject('TabPresenter') private tabPresenter: TabPresenter, private consoleClient: ConsoleClient, ) { } diff --git a/src/background/usecases/FindUseCase.ts b/src/background/usecases/FindUseCase.ts index b8593c6..cb41cd5 100644 --- a/src/background/usecases/FindUseCase.ts +++ b/src/background/usecases/FindUseCase.ts @@ -1,4 +1,4 @@ -import { injectable } from 'tsyringe'; +import { inject, injectable } from 'tsyringe'; import FindRepository from '../repositories/FindRepository'; import TabPresenter from '../presenters/TabPresenter'; import ConsoleClient from '../infrastructures/ConsoleClient'; @@ -6,7 +6,7 @@ import ConsoleClient from '../infrastructures/ConsoleClient'; @injectable() export default class FindUseCase { constructor( - private tabPresenter: TabPresenter, + @inject('TabPresenter') private tabPresenter: TabPresenter, private findRepository: FindRepository, private consoleClient: ConsoleClient, ) { diff --git a/src/background/usecases/LinkUseCase.ts b/src/background/usecases/LinkUseCase.ts index 9c0eab5..be076c7 100644 --- a/src/background/usecases/LinkUseCase.ts +++ b/src/background/usecases/LinkUseCase.ts @@ -1,10 +1,10 @@ -import { injectable } from 'tsyringe'; +import { inject, injectable } from 'tsyringe'; import TabPresenter from '../presenters/TabPresenter'; @injectable() export default class LinkUseCase { constructor( - private tabPresenter: TabPresenter, + @inject('TabPresenter') private tabPresenter: TabPresenter, ) { } diff --git a/src/background/usecases/MarkUseCase.ts b/src/background/usecases/MarkUseCase.ts index eeac40f..2c0bc13 100644 --- a/src/background/usecases/MarkUseCase.ts +++ b/src/background/usecases/MarkUseCase.ts @@ -1,4 +1,4 @@ -import { injectable } from 'tsyringe'; +import { inject, injectable } from 'tsyringe'; import TabPresenter from '../presenters/TabPresenter'; import MarkRepository from '../repositories/MarkRepository'; import ConsoleClient from '../infrastructures/ConsoleClient'; @@ -7,7 +7,7 @@ import ContentMessageClient from '../infrastructures/ContentMessageClient'; @injectable() export default class MarkUseCase { constructor( - private tabPresenter: TabPresenter, + @inject('TabPresenter') private tabPresenter: TabPresenter, private markRepository: MarkRepository, private consoleClient: ConsoleClient, private contentMessageClient: ContentMessageClient, diff --git a/src/background/usecases/NavigateUseCase.ts b/src/background/usecases/NavigateUseCase.ts index 25e7f20..3aa1ed6 100644 --- a/src/background/usecases/NavigateUseCase.ts +++ b/src/background/usecases/NavigateUseCase.ts @@ -1,11 +1,11 @@ -import { injectable } from 'tsyringe'; +import { inject, injectable } from 'tsyringe'; import NavigateClient from '../clients/NavigateClient'; import TabPresenter from '../presenters/TabPresenter'; @injectable() export default class NavigateUseCase { constructor( - private tabPresenter: TabPresenter, + @inject('TabPresenter') private tabPresenter: TabPresenter, private navigateClient: NavigateClient, ) { } diff --git a/src/background/usecases/TabSelectUseCase.ts b/src/background/usecases/TabSelectUseCase.ts index 62098de..271bb6c 100644 --- a/src/background/usecases/TabSelectUseCase.ts +++ b/src/background/usecases/TabSelectUseCase.ts @@ -1,10 +1,10 @@ -import { injectable } from 'tsyringe'; +import { inject, injectable } from 'tsyringe'; import TabPresenter from '../presenters/TabPresenter'; @injectable() export default class TabSelectUseCase { constructor( - private tabPresenter: TabPresenter, + @inject('TabPresenter') private tabPresenter: TabPresenter, ) { } diff --git a/src/background/usecases/TabUseCase.ts b/src/background/usecases/TabUseCase.ts index 66f8573..418dde5 100644 --- a/src/background/usecases/TabUseCase.ts +++ b/src/background/usecases/TabUseCase.ts @@ -1,4 +1,4 @@ -import { injectable } from 'tsyringe'; +import {inject, injectable} from 'tsyringe'; import TabPresenter from '../presenters/TabPresenter'; import WindowPresenter from '../presenters/WindowPresenter'; import BrowserSettingRepository from '../repositories/BrowserSettingRepository'; @@ -6,7 +6,7 @@ import BrowserSettingRepository from '../repositories/BrowserSettingRepository'; @injectable() export default class TabUseCase { constructor( - private tabPresenter: TabPresenter, + @inject('TabPresenter') private tabPresenter: TabPresenter, private windowPresenter: WindowPresenter, private browserSettingRepository: BrowserSettingRepository, ) { diff --git a/src/background/usecases/VersionUseCase.ts b/src/background/usecases/VersionUseCase.ts index 9ea8af9..21a5e2c 100644 --- a/src/background/usecases/VersionUseCase.ts +++ b/src/background/usecases/VersionUseCase.ts @@ -5,7 +5,7 @@ import Notifier from '../presenters/Notifier'; @injectable() export default class VersionUseCase { constructor( - private tabPresenter: TabPresenter, + @inject('TabPresenter') private tabPresenter: TabPresenter, @inject("Notifier") private notifier: Notifier, ) { } diff --git a/src/background/usecases/ZoomUseCase.ts b/src/background/usecases/ZoomUseCase.ts index f598871..ca1368d 100644 --- a/src/background/usecases/ZoomUseCase.ts +++ b/src/background/usecases/ZoomUseCase.ts @@ -1,4 +1,4 @@ -import { injectable } from 'tsyringe'; +import { inject, injectable } from 'tsyringe'; import TabPresenter from '../presenters/TabPresenter'; const ZOOM_SETTINGS: number[] = [ @@ -9,7 +9,7 @@ const ZOOM_SETTINGS: number[] = [ @injectable() export default class ZoomUseCase { constructor( - private tabPresenter: TabPresenter, + @inject('TabPresenter') private tabPresenter: TabPresenter, ) { } diff --git a/src/background/usecases/filters.ts b/src/background/usecases/filters.ts deleted file mode 100644 index 98957a7..0000000 --- a/src/background/usecases/filters.ts +++ /dev/null @@ -1,76 +0,0 @@ -type Item = browser.history.HistoryItem; - -const filterHttp = (items: Item[]): Item[] => { - const httpsHosts = items.map(x => new URL(x.url as string)) - .filter(x => x.protocol === 'https:') - .map(x => x.host); - const hostsSet = new Set(httpsHosts); - - return items.filter((item: Item) => { - const url = new URL(item.url as string); - return url.protocol === 'https:' || !hostsSet.has(url.host); - }); -}; - -const filterBlankTitle = (items: Item[]): Item[] => { - return items.filter(item => item.title && item.title !== ''); -}; - -const filterByTailingSlash = (items: Item[]): Item[] => { - const urls = items.map(item => new URL(item.url as string)); - const simplePaths = urls - .filter(url => url.hash === '' && url.search === '') - .map(url => url.origin + url.pathname); - const pathsSet = new Set(simplePaths); - - return items.filter((item) => { - const url = new URL(item.url as string); - if (url.hash !== '' || url.search !== '' || - url.pathname.slice(-1) !== '/') { - return true; - } - return !pathsSet.has(url.origin + url.pathname.slice(0, -1)); - }); -}; - -const filterByPathname = (items: Item[], min: number): Item[] => { - const hash: {[key: string]: Item} = {}; - for (const item of items) { - const url = new URL(item.url as string); - const pathname = url.origin + url.pathname; - if (!hash[pathname]) { - hash[pathname] = item; - } else if ((hash[pathname].url as string).length > - (item.url as string).length) { - hash[pathname] = item; - } - } - const filtered = Object.values(hash); - if (filtered.length < min) { - return items; - } - return filtered; -}; - -const filterByOrigin = (items: Item[], min: number): Item[] => { - const hash: {[key: string]: Item} = {}; - for (const item of items) { - const origin = new URL(item.url as string).origin; - if (!hash[origin]) { - hash[origin] = item; - } else if ((hash[origin].url as string).length > - (item.url as string).length) { - hash[origin] = item; - } - } - const filtered = Object.values(hash); - if (filtered.length < min) { - return items; - } - return filtered; -}; - -export { - filterHttp, filterBlankTitle, filterByTailingSlash, - filterByPathname, filterByOrigin -}; |