aboutsummaryrefslogtreecommitdiff
path: root/src/background
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2020-03-26 22:17:00 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2020-03-26 22:17:00 +0900
commitb2a37b8fc3e273dd71e1e3558c58be8002aa3789 (patch)
tree9e6f0d354beca92e975dc97462b3860833112f81 /src/background
parent6829e24c62c0291336502b3390905b57b81abd21 (diff)
Query completions on open command by a completion source
Diffstat (limited to 'src/background')
-rw-r--r--src/background/completion/CompletionUseCase.ts21
-rw-r--r--src/background/controllers/CompletionController.ts31
-rw-r--r--src/background/infrastructures/ContentMessageListener.ts10
3 files changed, 62 insertions, 0 deletions
diff --git a/src/background/completion/CompletionUseCase.ts b/src/background/completion/CompletionUseCase.ts
index 898efd4..fd3c279 100644
--- a/src/background/completion/CompletionUseCase.ts
+++ b/src/background/completion/CompletionUseCase.ts
@@ -2,6 +2,7 @@ import { inject, injectable } from "tsyringe";
import HistoryRepository from "./HistoryRepository";
import BookmarkRepository from "./BookmarkRepository";
import CachedSettingRepository from "../repositories/CachedSettingRepository";
+import CompletionType from "../../shared/CompletionType";
export type BookmarkItem = {
title: string
@@ -23,6 +24,26 @@ export default class CompletionUseCase {
) {
}
+ async getCompletionTypes(): Promise<CompletionType[]> {
+ const settings = await this.cachedSettingRepository.get();
+ const types: CompletionType[] = [];
+ for (const c of settings.properties.complete) {
+ switch (c) {
+ case 's':
+ types.push(CompletionType.SearchEngines);
+ break;
+ case 'h':
+ types.push(CompletionType.History);
+ break;
+ case 'b':
+ types.push(CompletionType.Bookmarks);
+ break;
+ }
+ // ignore invalid characters in the complete property
+ }
+ return types;
+ }
+
async requestSearchEngines(query: string): Promise<string[]> {
const settings = await this.cachedSettingRepository.get();
return Object.keys(settings.search.engines)
diff --git a/src/background/controllers/CompletionController.ts b/src/background/controllers/CompletionController.ts
new file mode 100644
index 0000000..313f38b
--- /dev/null
+++ b/src/background/controllers/CompletionController.ts
@@ -0,0 +1,31 @@
+import {
+ ConsoleGetCompletionTypesResponse,
+ ConsoleRequestBookmarksResponse, ConsoleRequestHistoryResponse, ConsoleRequestSearchEnginesResponse
+} from "../../shared/messages";
+import { injectable } from "tsyringe";
+import CompletionUseCase from "../completion/CompletionUseCase";
+
+@injectable()
+export default class CompletionController {
+ constructor(
+ private completionUseCase: CompletionUseCase,
+ ) {
+ }
+
+ async getCompletionTypes(): Promise<ConsoleGetCompletionTypesResponse> {
+ return this.completionUseCase.getCompletionTypes();
+ }
+
+ async requestSearchEngines(query: string): Promise<ConsoleRequestSearchEnginesResponse> {
+ const items = await this.completionUseCase.requestSearchEngines(query);
+ return items.map(name => ({ title: name }));
+ }
+
+ async requestBookmarks(query: string): Promise<ConsoleRequestBookmarksResponse> {
+ return this.completionUseCase.requestBookmarks(query);
+ }
+
+ async requestHistory(query: string): Promise<ConsoleRequestHistoryResponse> {
+ return this.completionUseCase.requestHistory(query);
+ }
+} \ No newline at end of file
diff --git a/src/background/infrastructures/ContentMessageListener.ts b/src/background/infrastructures/ContentMessageListener.ts
index d063810..62cd49f 100644
--- a/src/background/infrastructures/ContentMessageListener.ts
+++ b/src/background/infrastructures/ContentMessageListener.ts
@@ -9,6 +9,7 @@ import AddonEnabledController from '../controllers/AddonEnabledController';
import LinkController from '../controllers/LinkController';
import OperationController from '../controllers/OperationController';
import MarkController from '../controllers/MarkController';
+import CompletionController from "../controllers/CompletionController";
@injectable()
export default class ContentMessageListener {
@@ -17,6 +18,7 @@ export default class ContentMessageListener {
constructor(
private settingController: SettingController,
private commandController: CommandController,
+ private completionController: CompletionController,
private findController: FindController,
private addonEnabledController: AddonEnabledController,
private linkController: LinkController,
@@ -63,6 +65,14 @@ export default class ContentMessageListener {
switch (message.type) {
case messages.CONSOLE_QUERY_COMPLETIONS:
return this.onConsoleQueryCompletions(message.text);
+ case messages.CONSOLE_GET_COMPLETION_TYPES:
+ return this.completionController.getCompletionTypes();
+ case messages.CONSOLE_REQUEST_SEARCH_ENGINES_MESSAGE:
+ return this.completionController.requestSearchEngines(message.query);
+ case messages.CONSOLE_REQUEST_BOOKMARKS:
+ return this.completionController.requestBookmarks(message.query);
+ case messages.CONSOLE_REQUEST_HISTORY:
+ return this.completionController.requestHistory(message.query);
case messages.CONSOLE_ENTER_COMMAND:
return this.onConsoleEnterCommand(message.text);
case messages.SETTINGS_QUERY: