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 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
}));
}
}