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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
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 hide = (): actions.ConsoleAction => {
return {
type: actions.CONSOLE_HIDE,
};
};
const showCommand = async (text: string): Promise<actions.ShowCommand> => {
const completionTypes = await completionClient.getCompletionTypes();
return {
type: actions.CONSOLE_SHOW_COMMAND,
completionTypes,
text,
};
};
const showFind = (): actions.ShowFindAction => {
return {
type: actions.CONSOLE_SHOW_FIND,
};
};
const showError = (text: string): actions.ShowErrorAction => {
return {
type: actions.CONSOLE_SHOW_ERROR,
text: text
};
};
const showInfo = (text: string): actions.ShowInfoAction => {
return {
type: actions.CONSOLE_SHOW_INFO,
text: text
};
};
const hideCommand = (): actions.HideCommandAction => {
window.top.postMessage(JSON.stringify({
type: messages.CONSOLE_UNFOCUS,
}), '*');
return {
type: actions.CONSOLE_HIDE_COMMAND,
};
};
const enterCommand = async(text: string): Promise<actions.HideCommandAction> => {
await browser.runtime.sendMessage({
type: messages.CONSOLE_ENTER_COMMAND,
text,
});
return hideCommand();
};
const enterFind = (text?: string): actions.HideCommandAction => {
window.top.postMessage(JSON.stringify({
type: messages.CONSOLE_ENTER_FIND,
text,
}), '*');
return hideCommand();
};
const setConsoleText = (consoleText: string): actions.SetConsoleTextAction => {
return {
type: actions.CONSOLE_SET_CONSOLE_TEXT,
consoleText,
};
};
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 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 getCompletions = async(text: string): Promise<actions.SetCompletionsAction> => {
const completions = await browser.runtime.sendMessage({
type: messages.CONSOLE_QUERY_COMPLETIONS,
text,
});
return {
type: actions.CONSOLE_SET_COMPLETIONS,
completions,
completionSource: text,
};
};
const completionNext = (): actions.CompletionNextAction => {
return {
type: actions.CONSOLE_COMPLETION_NEXT,
};
};
const completionPrev = (): actions.CompletionPrevAction => {
return {
type: actions.CONSOLE_COMPLETION_PREV,
};
};
export {
hide, showCommand, showFind, showError, showInfo, hideCommand, setConsoleText, enterCommand, enterFind,
getCompletions, getCommandCompletions, getOpenCompletions, getTabCompletions,
completionNext, completionPrev,
};
|