-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
code-mirror.js
79 lines (67 loc) · 1.93 KB
/
code-mirror.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
import { getOwner } from '@ember/application';
import { guidFor } from '@ember/object/internals';
import Modifier from 'ember-modifier';
import codemirror from 'codemirror';
import 'codemirror/addon/edit/matchbrackets';
import 'codemirror/addon/selection/active-line';
import 'codemirror/mode/javascript/javascript';
import 'codemirror/mode/ruby/ruby';
// import '@hashicorp/sentinel-codemirror/sentinel';
import 'codemirror/keymap/sublime';
import 'codemirror/addon/search/search';
import 'codemirror/addon/search/searchcursor';
import 'codemirror/addon/dialog/dialog';
export default class CodeMirrorModifier extends Modifier {
get cmService() {
return getOwner(this).lookup('service:code-mirror');
}
didInstall() {
this._setup();
}
willRemove() {
this._cleanup();
}
didUpdateArguments() {
if (this._editor.getValue() !== this.args.named.value) {
this._editor.setValue(this.args.named.value);
}
}
_onChange(editor) {
if (this.args.named.valueUpdated) {
this.args.named.valueUpdated(editor.getValue());
}
}
_setup() {
if (!this.element) {
throw new Error('CodeMirror modifier has no element');
}
// Assign an ID to this element if there is none. This is to
// ensure that there are unique IDs in the code-mirror service
// registry.
if (!this.element.id) {
this.element.id = guidFor(this.element);
}
let editor = codemirror(
this.element,
Object.assign(
{
value: this.args.named.value ? this.args.named.value : '',
inputStyle: 'contenteditable',
},
this.args.named.options
)
);
editor.on('change', editor => {
this._onChange(editor);
});
if (this.cmService) {
this.cmService.registerInstance(this.element.id, editor);
}
this._editor = editor;
}
_cleanup() {
if (this.cmService) {
this.cmService.unregisterInstance(this.element.id);
}
}
}