aboutsummaryrefslogtreecommitdiff
path: root/src/console/actions/console.ts
blob: cef04fe1605e2d28369f96b082014412f541dc0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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 {
    type: actions.CONSOLE_HIDE,
  };
};

const showCommand = (text: string): actions.ConsoleAction => {
  return {
    type: actions.CONSOLE_SHOW_COMMAND,
    text: text
  };
};

const showFind = (): actions.ConsoleAction => {
  return {
    type: actions.CONSOLE_SHOW_FIND,
  };
};

const showError = (text: string): actions.ConsoleAction => {
  return {
    type: actions.CONSOLE_SHOW_ERROR,
    text: text
  };
};

const showInfo = (text: string): actions.ConsoleAction => {
  return {
    type: actions.CONSOLE_SHOW_INFO,
    text: text
  };
};

const hideCommand = (): actions.ConsoleAction => {
  window.top.postMessage(JSON.stringify({
    type: messages.CONSOLE_UNFOCUS,
  }), '*');
  return {
    type: actions.CONSOLE_HIDE_COMMAND,
  };
};

const enterCommand = async(
  text: string,
): Promise<actions.ConsoleAction> => {
  await browser.runtime.sendMessage({
    type: messages.CONSOLE_ENTER_COMMAND,
    text,
  });
  return hideCommand();
};

const enterFind = (text?: string): actions.ConsoleAction => {
  window.top.postMessage(JSON.stringify({
    type: messages.CONSOLE_ENTER_FIND,
    text,
  }), '*');
  return hideCommand();
};

const setConsoleText = (consoleText: string): actions.ConsoleAction => {
  return {
    type: actions.CONSOLE_SET_CONSOLE_TEXT,
    consoleText,
  };
};

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,
    text,
  });
  return {
    type: actions.CONSOLE_SET_COMPLETIONS,
    completions,
    completionSource: text,
  };
};

const completionNext = (): actions.ConsoleAction => {
  return {
    type: actions.CONSOLE_COMPLETION_NEXT,
  };
};

const completionPrev = (): actions.ConsoleAction => {
  return {
    type: actions.CONSOLE_COMPLETION_PREV,
  };
};

export {
  hide, showCommand, showFind, showError, showInfo, hideCommand, setConsoleText,
  enterCommand, enterFind, getCompletions, getCommandCompletions, completionNext, completionPrev,
};