From b2a37b8fc3e273dd71e1e3558c58be8002aa3789 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Thu, 26 Mar 2020 22:17:00 +0900 Subject: Query completions on open command by a completion source --- src/console/actions/console.ts | 61 ++++++++++++++++++++++++++++++--- src/console/actions/index.ts | 2 ++ src/console/clients/CompletionClient.ts | 54 +++++++++++++++++++++++++++++ src/console/components/Console.tsx | 12 ++++++- src/console/index.tsx | 4 +-- src/console/reducers/index.ts | 4 +++ 6 files changed, 130 insertions(+), 7 deletions(-) create mode 100644 src/console/clients/CompletionClient.ts (limited to 'src/console') diff --git a/src/console/actions/console.ts b/src/console/actions/console.ts index cef04fe..99e58f7 100644 --- a/src/console/actions/console.ts +++ b/src/console/actions/console.ts @@ -1,6 +1,11 @@ import * as messages from '../../shared/messages'; import * as actions from './index'; import { Command } from "../../shared/Command"; +import CompletionClient from "../clients/CompletionClient"; +import CompletionType from "../../shared/CompletionType"; +import Completions from "../Completions"; + +const completionClient = new CompletionClient(); const commandDocs = { [Command.Set]: 'Set a value of the property', @@ -22,10 +27,12 @@ const hide = (): actions.ConsoleAction => { }; }; -const showCommand = (text: string): actions.ConsoleAction => { +const showCommand = async (text: string): Promise => { + const completionTypes = await completionClient.getCompletionTypes(); return { type: actions.CONSOLE_SHOW_COMMAND, - text: text + completionTypes, + text, }; }; @@ -102,6 +109,51 @@ const getCommandCompletions = (text: string): actions.ConsoleAction => { } }; +const getOpenCompletions = async(types: CompletionType[], original: string, command: Command, query: string): Promise => { + const completions: Completions = []; + for (const type of types) { + switch (type) { + case CompletionType.SearchEngines: + completions.push({ + name: 'Search Engines', + items: (await completionClient.requestSearchEngines(query)) + .map(key => ({ + caption: key.title, + content: command + ' ' + key.title, + })) + }); + break; + case CompletionType.History: + completions.push({ + name: 'History', + items: (await completionClient.requestHistory(query)) + .map(item => ({ + caption: item.title, + content: command + ' ' + item.url, + url: item.url + })), + }); + break; + case CompletionType.Bookmarks: + completions.push({ + name: 'Bookmarks', + items: (await completionClient.requestBookmarks(query)) + .map(item => ({ + caption: item.title, + content: command + ' ' + item.url, + url: item.url + })) + }); + break; + } + } + + return { + type: actions.CONSOLE_SET_COMPLETIONS, + completions, + completionSource: original, + }; +}; const getCompletions = async(text: string): Promise => { const completions = await browser.runtime.sendMessage({ @@ -128,6 +180,7 @@ const completionPrev = (): actions.ConsoleAction => { }; export { - hide, showCommand, showFind, showError, showInfo, hideCommand, setConsoleText, - enterCommand, enterFind, getCompletions, getCommandCompletions, completionNext, completionPrev, + hide, showCommand, showFind, showError, showInfo, hideCommand, setConsoleText, enterCommand, enterFind, + getCompletions, getCommandCompletions, getOpenCompletions, + completionNext, completionPrev, }; diff --git a/src/console/actions/index.ts b/src/console/actions/index.ts index d36f8cd..8448e04 100644 --- a/src/console/actions/index.ts +++ b/src/console/actions/index.ts @@ -1,4 +1,5 @@ import Completions from "../Completions"; +import CompletionType from "../../shared/CompletionType"; export const CONSOLE_HIDE = 'console.hide'; export const CONSOLE_SHOW_COMMAND = 'console.show.command'; @@ -18,6 +19,7 @@ interface HideAction { interface ShowCommand { type: typeof CONSOLE_SHOW_COMMAND; text: string; + completionTypes: CompletionType[]; } interface ShowFindAction { diff --git a/src/console/clients/CompletionClient.ts b/src/console/clients/CompletionClient.ts new file mode 100644 index 0000000..d5f9b01 --- /dev/null +++ b/src/console/clients/CompletionClient.ts @@ -0,0 +1,54 @@ +import * as messages from "../../shared/messages"; +import { + ConsoleGetCompletionTypesResponse, + ConsoleRequestBookmarksResponse, + ConsoleRequestHistoryResponse, ConsoleRequestSearchEnginesResponse +} from "../../shared/messages"; +import CompletionType from "../../shared/CompletionType"; + +export type SearchEngines = { + title: string +} + +export type BookmarkItem = { + title: string + url: string +} + +export type HistoryItem = { + title: string + url: string +} + +export default class CompletionClient { + async getCompletionTypes(): Promise { + const resp = await browser.runtime.sendMessage({ + type: messages.CONSOLE_GET_COMPLETION_TYPES, + }) as ConsoleGetCompletionTypesResponse; + return resp; + } + + async requestSearchEngines(query: string): Promise { + const resp = await browser.runtime.sendMessage({ + type: messages.CONSOLE_REQUEST_SEARCH_ENGINES_MESSAGE, + query, + }) as ConsoleRequestSearchEnginesResponse; + return resp; + } + + async requestBookmarks(query: string): Promise { + const resp = await browser.runtime.sendMessage({ + type: messages.CONSOLE_REQUEST_BOOKMARKS, + query, + }) as ConsoleRequestBookmarksResponse; + return resp; + } + + async requestHistory(query: string): Promise { + const resp = await browser.runtime.sendMessage({ + type: messages.CONSOLE_REQUEST_HISTORY, + query, + }) as ConsoleRequestHistoryResponse; + return resp; + } +} diff --git a/src/console/components/Console.tsx b/src/console/components/Console.tsx index 0a102a0..7be073e 100644 --- a/src/console/components/Console.tsx +++ b/src/console/components/Console.tsx @@ -7,6 +7,7 @@ import Message from './console/Message'; import * as consoleActions from '../../console/actions/console'; import { State as AppState } from '../reducers'; import CommandLineParser, { InputPhase } from "../commandline/CommandLineParser"; +import { Command } from "../../shared/Command"; const COMPLETION_MAX_ITEMS = 33; @@ -163,7 +164,16 @@ class Console extends React.Component { if (phase === InputPhase.OnCommand) { return this.props.dispatch(consoleActions.getCommandCompletions(text)); } else { - this.props.dispatch(consoleActions.getCompletions(text)); + const cmd = this.commandLineParser.parse(text); + switch (cmd.command) { + case Command.Open: + case Command.TabOpen: + case Command.WindowOpen: + this.props.dispatch(consoleActions.getOpenCompletions(this.props.completionTypes, text, cmd.command, cmd.args)); + break; + default: + this.props.dispatch(consoleActions.getCompletions(text)); + } } } } diff --git a/src/console/index.tsx b/src/console/index.tsx index 1209ec2..7bee746 100644 --- a/src/console/index.tsx +++ b/src/console/index.tsx @@ -22,11 +22,11 @@ window.addEventListener('load', () => { wrapper); }); -const onMessage = (message: any): any => { +const onMessage = async (message: any): Promise => { const msg = messages.valueOf(message); switch (msg.type) { case messages.CONSOLE_SHOW_COMMAND: - return store.dispatch(consoleActions.showCommand(msg.command)); + return store.dispatch(await consoleActions.showCommand(msg.command)); case messages.CONSOLE_SHOW_FIND: return store.dispatch(consoleActions.showFind()); case messages.CONSOLE_SHOW_ERROR: diff --git a/src/console/reducers/index.ts b/src/console/reducers/index.ts index 677a982..f1508bb 100644 --- a/src/console/reducers/index.ts +++ b/src/console/reducers/index.ts @@ -1,10 +1,12 @@ import * as actions from '../actions'; import Completions from "../Completions"; +import CompletionType from "../../shared/CompletionType"; export interface State { mode: string; messageText: string; consoleText: string; + completionTypes: CompletionType[]; completionSource: string; completions: Completions; select: number; @@ -15,6 +17,7 @@ const defaultState = { mode: '', messageText: '', consoleText: '', + completionTypes: [], completionSource: '', completions: [], select: -1, @@ -69,6 +72,7 @@ export default function reducer( return { ...state, mode: 'command', consoleText: action.text, + completionTypes: action.completionTypes, completions: []}; case actions.CONSOLE_SHOW_FIND: return { ...state, -- cgit v1.2.3