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
|
import './console.scss';
import Completion from './completion';
import * as messages from '../shared/messages';
const parent = window.parent;
// TODO consider object-oriented
var prevValue = "";
window.completion = null;
var completionOrigin = "";
const blurMessage = () => {
return {
type: 'vimvixen.command.blur'
};
};
const keydownMessage = (input) => {
return {
type: 'vimvixen.command.enter',
value: input.value
};
};
const keyupMessage = (input) => {
return {
type: 'vimvixen.command.change',
value: input.value
};
};
const handleBlur = () => {
messages.send(parent, blurMessage());
};
const completeNext = () => {
if (!window.completion) {
return;
}
let item = window.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 (!window.completion) {
return;
}
let item = window.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) => {
switch(e.keyCode) {
case KeyboardEvent.DOM_VK_ESCAPE:
messages.send(parent, blurMessage());
break;
case KeyboardEvent.DOM_VK_RETURN:
messages.send(parent, keydownMessage(e.target));
break;
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;
}
messages.send(parent, keyupMessage(e.target));
prevValue = 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 showCommand = (text) => {
let command = window.document.querySelector('#vimvixen-console-command');
command.style.display = 'block';
let error = window.document.querySelector('#vimvixen-console-error');
error.style.display = 'none';
let input = window.document.querySelector('#vimvixen-console-command-input');
input.value = text;
input.focus();
}
const showError = (text) => {
let error = window.document.querySelector('#vimvixen-console-error');
error.textContent = text;
error.style.display = 'block';
let command = window.document.querySelector('#vimvixen-console-command');
command.style.display = 'none';
let completion = window.document.querySelector('#vimvixen-console-completion');
completion.style.display = 'none';
}
const setCompletions = (completions) => {
let completion = window.document.querySelector('#vimvixen-console-completion');
completion.style.display = 'block';
completion.innerHTML = '';
let pairs = [];
for (let group of completions) {
let title = window.document.createElement('li');
title.className = 'vimvixen-console-completion-title';
title.textContent = group.name;
completion.append(title);
for (let item of group.items) {
let caption = window.document.createElement('span');
caption.textContent = item.caption;
caption.className = 'vimvixen-console-completion-item-caption';
let url = window.document.createElement('span');
url.textContent = item.url;
url.className = 'vimvixen-console-completion-item-url';
let li = window.document.createElement('li');
li.style.backgroundImage = 'url(' + item.icon + ')';
li.className = 'vimvixen-console-completion-item';
li.append(caption);
li.append(url);
completion.append(li);
pairs.push([item, li]);
}
}
window.completion = new Completion(pairs);
let input = window.document.querySelector('#vimvixen-console-command-input');
completionOrigin = input.value.split(' ')[0];
}
const selectCompletion = (element) => {
let elements = window.document.querySelectorAll('.vimvixen-console-completion-item');
Array.prototype.forEach.call(elements, (ele) => {
ele.className = 'vimvixen-console-completion-item';
});
element.classList.add('vimvixen-completion-selected');
};
messages.receive(window, (message) => {
switch (message.type) {
case 'vimvixen.console.show.command':
showCommand(message.text);
break;
case 'vimvixen.console.show.error':
showError(message.text);
break;
case 'vimvixen.console.set.completions':
setCompletions(message.completions);
break;
}
});
|