From a2345f55d64c041f7da079cf36eb4039c43013df Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 22 Jul 2018 11:13:21 +0900 Subject: [PATCH] Search engine completions --- src/background/usecases/completions.js | 70 +++++++++++++++++--------- 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/src/background/usecases/completions.js b/src/background/usecases/completions.js index 1c0fe38..18174bd 100644 --- a/src/background/usecases/completions.js +++ b/src/background/usecases/completions.js @@ -4,12 +4,14 @@ import Completions from '../domains/completions'; import CompletionRepository from '../repositories/completions'; import CommandDocs from 'background/shared/commands/docs'; import * as filters from './filters'; +import SettingRepository from '../repositories/setting'; const COMPLETION_ITEM_LIMIT = 10; export default class CompletionsInteractor { constructor() { this.completionRepository = new CompletionRepository(); + this.settingRepository = new SettingRepository(); } queryConsoleCommand(prefix) { @@ -31,34 +33,18 @@ export default class CompletionsInteractor { } async queryOpen(name, keywords) { - // TODO get search engines from settings let groups = []; - let histories = await this.completionRepository.queryHistories(keywords); + let engines = await this.querySearchEngineItems(name, keywords); + if (engines.length > 0) { + groups.push(new CompletionGroup('Search Engines', engines)); + } + let histories = await this.queryHistoryItems(name, keywords); if (histories.length > 0) { - 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, y) => x.visitCount < y.visitCount) - .slice(0, COMPLETION_ITEM_LIMIT); - let items = histories.map(page => new CompletionItem({ - caption: page.title, - content: name + ' ' + page.url, - url: page.url - })); - groups.push(new CompletionGroup('History', items)); + groups.push(new CompletionGroup('History', histories)); } - - let bookmarks = await this.completionRepository.queryBookmarks(keywords); + let bookmarks = await this.queryBookmarkItems(name, keywords); if (bookmarks.length > 0) { - let items = bookmarks.map(page => new CompletionItem({ - caption: page.title, - content: name + ' ' + page.url, - url: page.url - })); - groups.push(new CompletionGroup('Bookmarks', items)); + groups.push(new CompletionGroup('Bookmarks', bookmarks)); } return new Completions(groups); } @@ -89,4 +75,40 @@ export default class CompletionsInteractor { })); return new Completions([new CompletionGroup('Buffers', items)]); } + + async querySearchEngineItems(name, keywords) { + let settings = await this.settingRepository.get(); + let engines = Object.keys(settings.search.engines) + .filter(key => key.startsWith(keywords)); + return engines.map(key => new CompletionItem({ + caption: key, + content: name + ' ' + key, + })); + } + + async queryHistoryItems(name, keywords) { + let histories = await this.completionRepository.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, y) => x.visitCount < y.visitCount) + .slice(0, COMPLETION_ITEM_LIMIT); + return histories.map(page => new CompletionItem({ + caption: page.title, + content: name + ' ' + page.url, + url: page.url + })); + } + + async queryBookmarkItems(name, keywords) { + let bookmarks = await this.completionRepository.queryBookmarks(keywords); + return bookmarks.map(page => new CompletionItem({ + caption: page.title, + content: name + ' ' + page.url, + url: page.url + })); + } }