aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2018-07-22 11:13:21 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2018-07-22 11:13:21 +0900
commita2345f55d64c041f7da079cf36eb4039c43013df (patch)
tree0394108813e37fbdf87e2f38fa64b6c75e45f850
parent87ed1f43a96a67c4901c267aee96784de1d45889 (diff)
Search engine completions
-rw-r--r--src/background/usecases/completions.js70
1 files 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
+ }));
+ }
}