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
|
import './console.scss';
import Completion from './completion';
import messages from '../content/messages';
// TODO consider object-oriented
let prevValue = '';
let completion = null;
let completionOrigin = '';
let prevState = {};
const handleBlur = () => {
return browser.runtime.sendMessage({
type: messages.CONSOLE_BLURRED,
});
};
const selectCompletion = (target) => {
let container = window.document.querySelector('#vimvixen-console-completion');
Array.prototype.forEach.call(container.children, (ele) => {
if (!ele.classList.contains('vimvixen-console-completion-item')) {
return;
}
if (ele === target) {
ele.classList.add('vimvixen-completion-selected');
} else {
ele.classList.remove('vimvixen-completion-selected');
}
});
};
const completeNext = () => {
if (!completion) {
return;
}
let item = completion.next();
if (!item) {
return;
}
let input = window.document.querySelector('#vimvixen-console-command-input');
input.value = completionOrigin + ' ' + item[0].content;
selectCompletion(item[1]);
};
const completePrev = () => {
if (!completion) {
return;
}
let item = completion.prev();
if (!item) {
return;
}
let input = window.document.querySelector('#vimvixen-console-command-input');
input.value = completionOrigin + ' ' + item[0].content;
selectCompletion(item[1]);
};
const handleKeydown = (e) => {
let input = window.document.querySelector('#vimvixen-console-command-input');
switch (e.keyCode) {
case KeyboardEvent.DOM_VK_ESCAPE:
return input.blur();
case KeyboardEvent.DOM_VK_RETURN:
return browser.runtime.sendMessage({
type: messages.CONSOLE_ENTERED,
text: e.target.value
});
case KeyboardEvent.DOM_VK_TAB:
if (e.shiftKey) {
completePrev();
} else {
completeNext();
}
e.stopPropagation();
e.preventDefault();
break;
}
};
const handleKeyup = (e) => {
if (e.keyCode === KeyboardEvent.DOM_VK_TAB) {
return;
}
if (e.target.value === prevValue) {
return;
}
prevValue = e.target.value;
return browser.runtime.sendMessage({
type: messages.CONSOLE_CHANGEED,
text: e.target.value
});
};
window.addEventListener('load', () => {
let input = window.document.querySelector('#vimvixen-console-command-input');
input.addEventListener('blur', handleBlur);
input.addEventListener('keydown', handleKeydown);
input.addEventListener('keyup', handleKeyup);
});
const createCompletionTitle = (text) => {
let li = document.createElement('li');
li.className = 'vimvixen-console-completion-title';
li.textContent = text;
return li;
};
const createCompletionItem = (icon, caption, url) => {
let captionEle = document.createElement('span');
captionEle.className = 'vimvixen-console-completion-item-caption';
captionEle.textContent = caption;
let urlEle = document.createElement('span');
urlEle.className = 'vimvixen-console-completion-item-url';
urlEle.textContent = url;
let li = document.createElement('li');
li.style.backgroundImage = 'url(' + icon + ')';
li.className = 'vimvixen-console-completion-item';
li.append(captionEle);
li.append(urlEle);
return li;
};
const updateCompletions = (completions) => {
let completionsContainer =
window.document.querySelector('#vimvixen-console-completion');
let input = window.document.querySelector('#vimvixen-console-command-input');
completionsContainer.innerHTML = '';
let pairs = [];
for (let group of completions) {
let title = createCompletionTitle(group.name);
completionsContainer.append(title);
for (let item of group.items) {
let li = createCompletionItem(item.icon, item.caption, item.url);
completionsContainer.append(li);
pairs.push([item, li]);
}
}
completion = new Completion(pairs);
completionOrigin = input.value.split(' ')[0];
};
const update = (state) => {
let error = window.document.querySelector('#vimvixen-console-error');
let command = window.document.querySelector('#vimvixen-console-command');
let input = window.document.querySelector('#vimvixen-console-command-input');
error.style.display = state.errorShown ? 'block' : 'none';
error.textContent = state.errorText;
command.style.display = state.commandShown ? 'block' : 'none';
if (state.commandShown && !prevState.commandShown) {
input.value = state.commandText;
input.focus();
}
if (JSON.stringify(state.completions) !==
JSON.stringify(prevState.completions)) {
updateCompletions(state.completions);
}
prevState = state;
};
browser.runtime.onMessage.addListener((action) => {
if (action.type === messages.STATE_UPDATE) {
return update(action.state.console);
}
});
window.addEventListener('load', () => {
let error = window.document.querySelector('#vimvixen-console-error');
let command = window.document.querySelector('#vimvixen-console-command');
error.style.display = 'none';
command.style.display = 'none';
});
|