diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2020-03-25 20:59:29 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2020-03-25 20:59:29 +0900 |
commit | 70b08f1025d3e00a016843669d61c56789bc0028 (patch) | |
tree | e1f99494016a0b13894f1e739f1ff87102c4fd13 /src/console | |
parent | 7d51364584e9081f71f4691a713bb737f7573a74 (diff) |
Complete commands on the console script
Diffstat (limited to 'src/console')
-rw-r--r-- | src/console/actions/console.ts | 37 | ||||
-rw-r--r-- | src/console/components/Console.tsx | 20 |
2 files changed, 52 insertions, 5 deletions
diff --git a/src/console/actions/console.ts b/src/console/actions/console.ts index f7fa7a2..cef04fe 100644 --- a/src/console/actions/console.ts +++ b/src/console/actions/console.ts @@ -1,5 +1,20 @@ import * as messages from '../../shared/messages'; import * as actions from './index'; +import { Command } from "../../shared/Command"; + +const commandDocs = { + [Command.Set]: 'Set a value of the property', + [Command.Open]: 'Open a URL or search by keywords in current tab', + [Command.TabOpen]: 'Open a URL or search by keywords in new tab', + [Command.WindowOpen]: 'Open a URL or search by keywords in new window', + [Command.Buffer]: 'Select tabs by matched keywords', + [Command.BufferDelete]: 'Close a certain tab matched by keywords', + [Command.BuffersDelete]: 'Close all tabs matched by keywords', + [Command.Quit]: 'Close the current tab', + [Command.QuitAll]: 'Close all tabs', + [Command.AddBookmark]: 'Add current page to bookmarks', + [Command.Help]: 'Open Vim Vixen help in new tab', +}; const hide = (): actions.ConsoleAction => { return { @@ -68,6 +83,26 @@ const setConsoleText = (consoleText: string): actions.ConsoleAction => { }; }; +const getCommandCompletions = (text: string): actions.ConsoleAction => { + const items = Object.entries(commandDocs) + .filter(([name]) => name.startsWith(text.trimLeft())) + .map(([name, doc]) => ({ + caption: name, + content: name, + url: doc, + })); + const completions = [{ + name: "Console Command", + items, + }]; + return { + type: actions.CONSOLE_SET_COMPLETIONS, + completions, + completionSource: text, + } +}; + + const getCompletions = async(text: string): Promise<actions.ConsoleAction> => { const completions = await browser.runtime.sendMessage({ type: messages.CONSOLE_QUERY_COMPLETIONS, @@ -94,5 +129,5 @@ const completionPrev = (): actions.ConsoleAction => { export { hide, showCommand, showFind, showError, showInfo, hideCommand, setConsoleText, - enterCommand, enterFind, getCompletions, completionNext, completionPrev, + enterCommand, enterFind, getCompletions, getCommandCompletions, completionNext, completionPrev, }; diff --git a/src/console/components/Console.tsx b/src/console/components/Console.tsx index eafe2a7..0a102a0 100644 --- a/src/console/components/Console.tsx +++ b/src/console/components/Console.tsx @@ -6,6 +6,7 @@ import Completion from './console/Completion'; import Message from './console/Message'; import * as consoleActions from '../../console/actions/console'; import { State as AppState } from '../reducers'; +import CommandLineParser, { InputPhase } from "../commandline/CommandLineParser"; const COMPLETION_MAX_ITEMS = 33; @@ -18,6 +19,8 @@ type Props = StateProps & DispatchProps; class Console extends React.Component<Props> { private input: React.RefObject<Input>; + private commandLineParser: CommandLineParser = new CommandLineParser(); + constructor(props: Props) { super(props); @@ -103,16 +106,16 @@ class Console extends React.Component<Props> { onChange(e: React.ChangeEvent<HTMLInputElement>) { const text = e.target.value; this.props.dispatch(consoleActions.setConsoleText(text)); - if (this.props.mode === 'command') { - this.props.dispatch(consoleActions.getCompletions(text)); + if (this.props.mode !== 'command') { + return } + this.updateCompletions(text) } componentDidUpdate(prevProps: Props) { if (prevProps.mode !== 'command' && this.props.mode === 'command') { - this.props.dispatch( - consoleActions.getCompletions(this.props.consoleText)); + this.updateCompletions(this.props.consoleText); this.focus(); } else if (prevProps.mode !== 'find' && this.props.mode === 'find') { this.focus(); @@ -154,6 +157,15 @@ class Console extends React.Component<Props> { this.input.current.focus(); } } + + private updateCompletions(text: string) { + const phase = this.commandLineParser.inputPhase(text); + if (phase === InputPhase.OnCommand) { + return this.props.dispatch(consoleActions.getCommandCompletions(text)); + } else { + this.props.dispatch(consoleActions.getCompletions(text)); + } + } } const mapStateToProps = (state: AppState) => ({ ...state }); |