forked from elm/elm-lang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode-editor.js
366 lines (287 loc) · 8.87 KB
/
code-editor.js
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
(function() {
// POLYFILL
window.requestIdleCallback =
window.requestIdleCallback ||
function (cb) {
var start = Date.now();
return setTimeout(function () {
cb({
didTimeout: false,
timeRemaining: function () {
return Math.max(0, 50 - (Date.now() - start));
}
});
}, 1);
}
window.cancelIdleCallback =
window.cancelIdleCallback ||
function (id) {
clearTimeout(id);
}
// DEBOUNCER
var debounce = function(func) {
var token;
return function() {
var later = function() {
token = null;
func.apply(null, arguments);
};
cancelIdleCallback(token);
token = requestIdleCallback(later);
};
};
// EDITOR
function CodeEditor() {
this._editor = null;
this._theme = 'light';
this._source = null;
this._start = null;
this._end = null;
this._importEnd = 0;
this._init = this._init.bind(this);
this._updateTheme = this._updateTheme.bind(this);
this._updateSource = this._updateSource.bind(this);
this._updateCursor = this._updateCursor.bind(this);
return Reflect.construct(HTMLElement, [], this.constructor);
}
CodeEditor.prototype = Object.create(HTMLElement.prototype, {
constructor: {
value: CodeEditor
},
connectedCallback: {
value: function () {
this._init();
}
},
disconnectedCallback: {
value: function () {
this._editor = null;
this._theme = 'light';
this._source = null;
this._start = null;
this._end = null;
this._importEnd = 0;
}
},
// INIT EDITOR
_init: {
value: function () {
var sendChangeEvent = debounce((function() {
var previous = this._source;
this._source = this._editor.getValue();
if (previous === this._source) return;
this.dispatchEvent(new Event('change'));
}).bind(this));
var sendSaveEvent = debounce((function() {
this.dispatchEvent(new Event('save'));
}).bind(this));
var sendHintEvent = (function() {
this.dispatchEvent(new Event('hint'));
}).bind(this);
this._editor = CodeMirror(this, {
mode: "elm",
lineNumbers: true,
keyMap: "sublime",
matchBrackets: true,
autoCloseBrackets: true,
styleActiveLine: true,
theme: this._theme,
value: this._source,
tabSize: 2,
indentWithTabs: false,
extraKeys: {
"Tab": handleTab,
"Shift-Tab": handleUntab,
"Cmd-S": function(cm) { sendSaveEvent(); },
"Ctrl-Enter": function(cm) { sendSaveEvent(); }
}
});
this._editor.on('changes', sendChangeEvent);
this._editor.focus();
requestIdleCallback((function() {
// Make sure Elm is ready to receive messages.
this._editor.on('cursorActivity', sendHintEvent)
}).bind(this));
this._updateSource();
this._updateCursor();
}
},
// UPDATE EDITOR
_updateSource: {
value: function () {
if (!this._editor) return;
this._editor.setValue(this._source);
}
},
_updateTheme: {
value: function () {
if (!this._editor) return;
this._editor.setOption('theme', this._theme);
}
},
_updateCursor: {
value: function () {
if (!this._editor) return;
var isStartNull = isPositionNull(this._start);
var isEndNull = isPositionNull(this._end);
if (!(isStartNull && isEndNull)) {
var start_ = isStartNull ? this._end : this._start;
var end_ = isEndNull ? this._start : this._end;
var start = { line: start_.line - 1, ch: start_.column - 1 };
var end = { line: end_.line - 1, ch: end_.column - 1 }
this._editor.setSelection(start, end, { scroll: false });
this._editor.scrollIntoView({ from: start, to: end }, 200);
this._editor.focus();
this._start = null;
this._end = null;
}
}
},
// PROPERTY: SOURCE
source: {
get: function () {
return this._source;
},
set: function (updated) {
var oldSource = this._source;
this._source = updated;
if (updated !== oldSource) {
this._updateSource();
}
}
},
// PROPERTY: THEME
theme: {
get: function () {
return this._theme;
},
set: function (updated) {
var oldTheme = this._theme;
this._theme = updated;
if (updated !== oldTheme) {
this._updateTheme();
}
}
},
// PROPERTY: CURSOR
selection: {
get: function () {
return { start: this._start, end: this._end };
},
set: function (updated) {
var oldStart = this._start;
var oldEnd = this._end;
this._start = updated.start;
this._end = updated.end;
var isSame = isPositionEqual(updated.start, oldStart) && isPositionEqual(updated.end, oldEnd);
if (!isSame) { this._updateCursor(); }
}
},
// PROPERTY: IMPORT END
importEnd: {
get: function () {
return this._importEnd;
},
set: function (updated) {
this._importEnd = updated;
}
},
// PROPERTY: HINT
hint: {
get: function () {
if (!this._editor) return null;
return getHint(this._editor, this._importEnd);
}
}
});
// HELPERS
function isPositionNull(position) {
return position === null || position.line === null || position.column === null;
}
function isPositionEqual(a, b) {
return (b && b.line) === (a && a.line) && (b && b.column) === (a && a.column);
}
// CODEMIRROR: EXTRA KEYS
function handleTab(cm) {
cm.execCommand("indentMore");
}
function handleUntab(cm) {
cm.execCommand("indentLess");
}
// GET HINT
function getHint(editor, importEnd) {
var start = editor.getCursor('anchor');
var cursor = editor.getCursor('head');
if (start.line !== cursor.line || start.ch !== cursor.ch) {
return null;
}
var line = cursor.line;
var token = editor.getTokenAt(cursor);
var type = token.type;
return type === 'variable' ? getLowerHint(editor, line, token)
: type === 'variable-2' ? getUpperHint(editor, importEnd, line, token)
: type === 'keyword' ? getKeywordHint(editor, importEnd, line, token)
: type === 'def' ? getDefHint(token.string)
: null;
}
function getLowerHint(editor, line, token) {
return getPrefix(editor, line, token) + token.string;
}
function getUpperHint(editor, importEnd, line, token) {
var name = getPrefix(editor, line, token) + token.string + getPostfix(editor, line, token);
if (line < importEnd) {
var content = editor.getLine(line);
if (!/^import\b/.test(content)) return name;
var i1 = content.indexOf('as');
var i2 = content.indexOf('exposing');
if (i1 < 0 && i2 < 0) return 'module:' + name;
if (i1 > 0 && token.end < i1) return 'module:' + name;
if (i2 > 0 && token.end < i2) return 'module:' + name;
}
return name;
}
function getKeywordHint(editor, importEnd, line, token) {
switch (token.string) {
case '.':
var next = getTokenAfter(editor, line, token);
return next.type === 'variable' ? getLowerHint(editor, line, next)
: next.type === 'variable-2' ? getUpperHint(editor, importEnd, line, next) : null;
case 'type':
return /^type\salias\b/.test(editor.getLine(line)) ? 'alias' : 'type';
case 'as':
return line < importEnd ? 'import' : 'as';
default:
return token.string;
}
}
function getDefHint(name) {
return name === 'init' || name === 'view' || name === 'update' || name === 'subscriptions'
? 'def:' + name
: null;
}
function getPrefix(editor, line, token) {
var dot = getTokenBefore(editor, line, token);
if (dot.string !== '.') { return ''; }
var qualifier = getTokenBefore(editor, line, dot);
return qualifier.type === 'variable-2'
? getPrefix(editor, line, qualifier) + qualifier.string + '.'
: '.';
}
function getPostfix(editor, line, token) {
var dot = getTokenAfter(editor, line, token);
if (dot.string !== '.') { return ''; }
var next = getTokenAfter(editor, line, dot);
return next.type === 'variable'
? '.' + next.string
: next.type === 'variable-2'
? '.' + next.string + getPostfix(editor, line, next)
: '.';
}
function getTokenBefore(editor, line, token) {
return editor.getTokenAt({ line: line, ch: token.start });
}
function getTokenAfter(editor, line, token) {
return editor.getTokenAt({ line: line, ch: token.end + 1 });
}
window.customElements.define('code-editor', CodeEditor);
})();