aboutsummaryrefslogtreecommitdiff
path: root/src/console/actions/console.ts
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2020-04-09 10:38:37 +0900
committerGitHub <noreply@github.com>2020-04-09 10:38:37 +0900
commit1656d52d2cefb3846d968c6117484e6aefe7dabe (patch)
treeab58a99b832d2571e2168f2ee0e328bc12d9580e /src/console/actions/console.ts
parentc6c2da8547891b50aef2f08e5f36d258183831ff (diff)
parent5176643e64d8f4a6be5fc73f0eb48dc65322e496 (diff)
Merge pull request #730 from ueokande/refactor-console-and-completion
Refactor console and completions
Diffstat (limited to 'src/console/actions/console.ts')
-rw-r--r--src/console/actions/console.ts193
1 files changed, 173 insertions, 20 deletions
diff --git a/src/console/actions/console.ts b/src/console/actions/console.ts
index f7fa7a2..e44c974 100644
--- a/src/console/actions/console.ts
+++ b/src/console/actions/console.ts
@@ -1,5 +1,32 @@
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";
+import TabFlag from "../../shared/TabFlag";
+
+const completionClient = new CompletionClient();
+
+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 propertyDocs: {[key: string]: string} = {
+ 'hintchars': 'hint characters on follow mode',
+ 'smoothscroll': 'smooth scroll',
+ 'complete': 'which are completed at the open page',
+};
const hide = (): actions.ConsoleAction => {
return {
@@ -7,34 +34,36 @@ const hide = (): actions.ConsoleAction => {
};
};
-const showCommand = (text: string): actions.ConsoleAction => {
+const showCommand = async (text: string): Promise<actions.ShowCommand> => {
+ const completionTypes = await completionClient.getCompletionTypes();
return {
type: actions.CONSOLE_SHOW_COMMAND,
- text: text
+ completionTypes,
+ text,
};
};
-const showFind = (): actions.ConsoleAction => {
+const showFind = (): actions.ShowFindAction => {
return {
type: actions.CONSOLE_SHOW_FIND,
};
};
-const showError = (text: string): actions.ConsoleAction => {
+const showError = (text: string): actions.ShowErrorAction => {
return {
type: actions.CONSOLE_SHOW_ERROR,
text: text
};
};
-const showInfo = (text: string): actions.ConsoleAction => {
+const showInfo = (text: string): actions.ShowInfoAction => {
return {
type: actions.CONSOLE_SHOW_INFO,
text: text
};
};
-const hideCommand = (): actions.ConsoleAction => {
+const hideCommand = (): actions.HideCommandAction => {
window.top.postMessage(JSON.stringify({
type: messages.CONSOLE_UNFOCUS,
}), '*');
@@ -43,9 +72,7 @@ const hideCommand = (): actions.ConsoleAction => {
};
};
-const enterCommand = async(
- text: string,
-): Promise<actions.ConsoleAction> => {
+const enterCommand = async(text: string): Promise<actions.HideCommandAction> => {
await browser.runtime.sendMessage({
type: messages.CONSOLE_ENTER_COMMAND,
text,
@@ -53,7 +80,7 @@ const enterCommand = async(
return hideCommand();
};
-const enterFind = (text?: string): actions.ConsoleAction => {
+const enterFind = (text?: string): actions.HideCommandAction => {
window.top.postMessage(JSON.stringify({
type: messages.CONSOLE_ENTER_FIND,
text,
@@ -61,38 +88,164 @@ const enterFind = (text?: string): actions.ConsoleAction => {
return hideCommand();
};
-const setConsoleText = (consoleText: string): actions.ConsoleAction => {
+const setConsoleText = (consoleText: string): actions.SetConsoleTextAction => {
return {
type: actions.CONSOLE_SET_CONSOLE_TEXT,
consoleText,
};
};
-const getCompletions = async(text: string): Promise<actions.ConsoleAction> => {
- const completions = await browser.runtime.sendMessage({
- type: messages.CONSOLE_QUERY_COMPLETIONS,
- text,
- });
+const getCommandCompletions = (text: string): actions.SetCompletionsAction => {
+ 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 getOpenCompletions = async(
+ types: CompletionType[], original: string, command: Command, query: string,
+): Promise<actions.SetCompletionsAction> => {
+ const completions: Completions = [];
+ for (const type of types) {
+ switch (type) {
+ case CompletionType.SearchEngines: {
+ const items = await completionClient.requestSearchEngines(query);
+ if (items.length === 0) {
+ break;
+ }
+ completions.push({
+ name: 'Search Engines',
+ items: items.map(key => ({
+ caption: key.title,
+ content: command + ' ' + key.title,
+ }))
+ });
+ break;
+ }
+ case CompletionType.History: {
+ const items = await completionClient.requestHistory(query);
+ if (items.length === 0) {
+ break;
+ }
+ completions.push({
+ name: 'History',
+ items: items.map(item => ({
+ caption: item.title,
+ content: command + ' ' + item.url,
+ url: item.url
+ })),
+ });
+ break;
+ }
+ case CompletionType.Bookmarks: {
+ const items = await completionClient.requestBookmarks(query);
+ if (items.length === 0) {
+ break;
+ }
+ completions.push({
+ name: 'Bookmarks',
+ items: items.map(item => ({
+ caption: item.title,
+ content: command + ' ' + item.url,
+ url: item.url
+ }))
+ });
+ break;
+ }
+ }
+ }
+
+ return {
+ type: actions.CONSOLE_SET_COMPLETIONS,
+ completions,
+ completionSource: original,
};
};
-const completionNext = (): actions.ConsoleAction => {
+const getTabCompletions = async (
+ original: string, command: Command, query: string, excludePinned: boolean,
+): Promise<actions.SetCompletionsAction> => {
+ const items = await completionClient.requestTabs(query, excludePinned);
+ let completions: Completions = [];
+ if (items.length > 0) {
+ completions = [{
+ name: 'Buffers',
+ items: items.map(item => ({
+ content: command + ' ' + item.url,
+ caption: `${item.index}: ${item.flag != TabFlag.None ? item.flag : ' ' } ${item.title}`,
+ url: item.url,
+ icon: item.faviconUrl,
+ })),
+ }];
+ }
+ return {
+ type: actions.CONSOLE_SET_COMPLETIONS,
+ completions,
+ completionSource: original,
+ }
+};
+
+const getPropertyCompletions = async(
+ original: string, command: Command, query: string,
+): Promise<actions.SetCompletionsAction> => {
+ const properties = await completionClient.getProperties();
+ const items = properties
+ .map(item => {
+ const desc = propertyDocs[item.name] || '';
+ if (item.type === 'boolean') {
+ return [{
+ caption: item.name,
+ content: command + ' ' + item.name,
+ url: 'Enable ' + desc,
+ }, {
+ caption: 'no' + item.name,
+ content: command + ' no' + item.name,
+ url: 'Disable ' + desc,
+ }];
+ } else {
+ return [{
+ caption: item.name,
+ content: name + ' ' + item.name,
+ url: 'Set ' + desc,
+ }];
+ }
+ })
+ .reduce((acc, val) => acc.concat(val), [])
+ .filter(item => item.caption.startsWith(query));
+ const completions: Completions = [{ name: 'Properties', items }];
+ return {
+ type: actions.CONSOLE_SET_COMPLETIONS,
+ completions,
+ completionSource: original,
+ }
+};
+
+const completionNext = (): actions.CompletionNextAction => {
return {
type: actions.CONSOLE_COMPLETION_NEXT,
};
};
-const completionPrev = (): actions.ConsoleAction => {
+const completionPrev = (): actions.CompletionPrevAction => {
return {
type: actions.CONSOLE_COMPLETION_PREV,
};
};
export {
- hide, showCommand, showFind, showError, showInfo, hideCommand, setConsoleText,
- enterCommand, enterFind, getCompletions, completionNext, completionPrev,
+ hide, showCommand, showFind, showError, showInfo, hideCommand, setConsoleText, enterCommand, enterFind,
+ getCommandCompletions, getOpenCompletions, getTabCompletions, getPropertyCompletions,
+ completionNext, completionPrev,
};