-
Notifications
You must be signed in to change notification settings - Fork 1
/
h5p-code-highlighter.js
91 lines (80 loc) · 2.46 KB
/
h5p-code-highlighter.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
var H5P = H5P || {};
H5P.CodeHighlighter = (function ($) {
/**
* Constructor function.
*/
function C(options, id) {
this.$ = $(this);
// Extend defaults with provided options
this.options = $.extend(true, {}, {
'language': 'HTML',
'code': '',
'lineNumbers': true,
'readOnly': true,
'advancedOptions': {
'lineWrapping': true,
'foldGutter': true,
'matchTags': false,
'tabSize': 2,
'firstLineNumber': 1
},
'maxHeight': 0,
'highlightLines': ''
}, options);
// Keep provided id.
this.id = id;
}
/**
* Attach function called by H5P framework to insert H5P content into
* page
*
* @param {jQuery} $container
*/
C.prototype.attach = function ($container) {
const self = this;
$container.addClass('h5p-code-highlighter');
this.editor = CodeMirror($container[0], {
value: CodeMirror.H5P.decode(this.options.code || ''),
inputStyle: 'textarea',
keyMap: 'sublime',
tabSize: this.options.tabSize,
indentWithTabs: true,
lineNumbers: this.options.lineNumbers,
firstLineNumber: this.options.advancedOptions.firstLineNumber,
readOnly: this.options.readOnly,
lineWrapping: this.options.advancedOptions.lineWrapping,
matchBrackets: true,
matchTags: this.options.advancedOptions.matchTags ? {
bothTags: true
} : false,
foldGutter: this.options.advancedOptions.foldGutter,
gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'],
styleActiveLine: {
nonEmpty: true
},
extraKeys: {
'F11': function (cm) {
cm.setOption('fullScreen', !cm.getOption('fullScreen'));
},
'Esc': function (cm) {
if (cm.getOption('fullScreen')) {
cm.setOption('fullScreen', false);
}
}
}
});
this.editor.on('changes', function () {
self.trigger('resize');
});
if (this.options.maxHeight !== 0) {
$container.find('.CodeMirror, .CodeMirror-scroll').css('max-height', this.options.maxHeight);
}
if (this.options.highlightLines !== '') {
CodeMirror.H5P.highlightLines(this.editor, this.options.highlightLines);
}
this.editor.refresh(); // required to avoid bug where line number overlap code that might happen in some condition
CodeMirror.H5P.setLanguage(this.editor, this.options.language);
this.trigger('resize');
};
return C;
})(H5P.jQuery);