Search engine completions

jh-changes
Shin'ya Ueoka 6 years ago
parent 87ed1f43a9
commit a2345f55d6
  1. 70
      src/background/usecases/completions.js

@ -4,12 +4,14 @@ import Completions from '../domains/completions';
import CompletionRepository from '../repositories/completions'; import CompletionRepository from '../repositories/completions';
import CommandDocs from 'background/shared/commands/docs'; import CommandDocs from 'background/shared/commands/docs';
import * as filters from './filters'; import * as filters from './filters';
import SettingRepository from '../repositories/setting';
const COMPLETION_ITEM_LIMIT = 10; const COMPLETION_ITEM_LIMIT = 10;
export default class CompletionsInteractor { export default class CompletionsInteractor {
constructor() { constructor() {
this.completionRepository = new CompletionRepository(); this.completionRepository = new CompletionRepository();
this.settingRepository = new SettingRepository();
} }
queryConsoleCommand(prefix) { queryConsoleCommand(prefix) {
@ -31,34 +33,18 @@ export default class CompletionsInteractor {
} }
async queryOpen(name, keywords) { async queryOpen(name, keywords) {
// TODO get search engines from settings
let groups = []; 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) { if (histories.length > 0) {
histories = [histories] groups.push(new CompletionGroup('History', 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));
} }
let bookmarks = await this.queryBookmarkItems(name, keywords);
let bookmarks = await this.completionRepository.queryBookmarks(keywords);
if (bookmarks.length > 0) { if (bookmarks.length > 0) {
let items = bookmarks.map(page => new CompletionItem({ groups.push(new CompletionGroup('Bookmarks', bookmarks));
caption: page.title,
content: name + ' ' + page.url,
url: page.url
}));
groups.push(new CompletionGroup('Bookmarks', items));
} }
return new Completions(groups); return new Completions(groups);
} }
@ -89,4 +75,40 @@ export default class CompletionsInteractor {
})); }));
return new Completions([new CompletionGroup('Buffers', items)]); 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
}));
}
} }