forked from DegrangeM/H5PEditor.CodeEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh5p-code-editor.js
191 lines (173 loc) · 5.49 KB
/
h5p-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
/**
* Code editor widget module
*
* @param {jQuery} $
*/
H5PEditor.widgets.codeEditor = H5PEditor.codeEditor = (function ($) {
/**
* Creates an input to write code with highlight.
*
* @param {mixed} parent
* @param {object} field
* @param {mixed} params
* @param {function} setValue
* @returns {C}
*/
function C(parent, field, params, setValue) {
this.parent = parent;
this.field = field;
this.params = params;
this.setValue = setValue;
}
/**
* Append the field to the wrapper.
*
* @param {jQuery} $wrapper
* @returns {undefined}
*/
C.prototype.appendTo = function ($wrapper) {
const that = this;
this.$item = $(this.createHtml()).appendTo($wrapper);
this.$item.addClass('h5p-code-editor');
this.$inputs = this.$item.find('input');
this.editor = CodeMirror(this.$item.find('.h5p-code-editor-editor')[0], {
value: CodeMirror.H5P.decode(this.params || ''),
inputStyle: 'textarea',
keyMap: 'sublime',
tabSize: 2,
indentWithTabs: true,
lineNumbers: true,
lineWrapping: true,
matchBrackets: true,
matchTags: {
bothTags: true
},
foldGutter: true,
gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'],
styleActiveLine: {
nonEmpty: true
},
showHint: true,
extraKeys: {
'F11': function (cm) {
cm.setOption('fullScreen', !cm.getOption('fullScreen'));
},
'Esc': function (cm) {
if (cm.getOption('fullScreen')) {
cm.setOption('fullScreen', false);
}
else {
// The user pressed the escape key, tab will now tab to the next element
// instead of adding a tab in the code. This is important for accessibility
// The tab behaviour will revert to default the next time the editor is focused.
if (!cm.state.keyMaps.some(x => x.name === 'tabAccessibility')) {
cm.addKeyMap({
'name': 'tabAccessibility',
'Tab': false,
'Shift-Tab': false
});
}
}
},
'Ctrl-Space': 'autocomplete'
}
});
this.editor.refresh(); // required to avoid bug where line number overlap code that might happen in some condition
this.editor.on('focus', function (cm) { // On focus, make tab add tab in editor
cm.removeKeyMap('tabAccessibility');
});
this.editor.on('change', function () {
C.saveChange(that);
});
// Check if the language attribute is set and is a path.
// If this is the case, set the field in a variable and
// add a watcher to call the applyLanguage function each
// time the field value is modified.
if (this.field.language && this.field.language[0] === '.') {
let fieldPath = this.field.language;
// language is a path, it should start with ./ or ../
// if it start with ./ we remove this part because findField doesn't handle it
if (fieldPath[1] === '/') {
fieldPath = fieldPath.substring(2);
}
this.languageField = H5PEditor.findField(fieldPath, this.parent);
if (this.languageField === false) {
this.field.language = 'null';
}
else {
if (this.languageField.field.type === 'text' && this.languageField.changeCallbacks) {
this.languageField.changeCallbacks.push(function () {
that.applyLanguage();
});
}
else {
H5PEditor.followField(this.parent, fieldPath, function () {
that.applyLanguage();
});
}
}
}
this.applyLanguage();
};
/**
* Apply the language to the code editor.
* By default, the code editor language will be set to 'HTML'
* You can choose to set an other language by setting the language attribute
* You can for example set language to "Python".
* You can also set the language attribute to a path to an other field
* like a text or select field. The language will be set to the value of this field
* In order to do that, your path need to start with a dot. If you other field
* is at the same depth-level, use the ./ notation. For exemple "./language"
* Be warned that the other field need to be before the code editor field.
*/
C.prototype.applyLanguage = function () {
if (this.field.language) {
if (this.languageField) {
CodeMirror.H5P.setLanguage(this.editor, this.languageField.value);
}
else {
CodeMirror.H5P.setLanguage(this.editor, this.field.language);
}
}
else {
CodeMirror.H5P.setLanguage(this.editor, 'HTML');
}
};
/**
* Creates HTML for the widget.
*/
C.prototype.createHtml = function () {
const id = H5PEditor.getNextFieldId(this.field);
return H5PEditor.createFieldMarkup(this.field, '<div class="h5p-code-editor-editor"></div>', id);
};
/**
* Save changes
*/
C.saveChange = function (that) {
that.params = CodeMirror.H5P.encode(that.editor.getValue());
that.setValue(that.field, that.params);
};
/**
* Validate the current values.
*/
C.prototype.validate = function () {
return true;
};
/**
* Remove this item.
*/
C.prototype.remove = function () {
this.$item.remove();
};
/**
* Local translate function.
*
* @param {Atring} key
* @param {Object} params
* @returns {@exp;H5PEditor@call;t}
*/
C.t = function (key, params) {
return H5PEditor.t('H5PEditor.CodeEditor', key, params);
};
return C;
})(H5P.jQuery);