-
Notifications
You must be signed in to change notification settings - Fork 36
/
codemirror.js
67 lines (52 loc) · 1.64 KB
/
codemirror.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
/* coremirror plugin
*/
import * as util from '../util.js'
export default class PluginCodeMirror {
constructor (jotted, options) {
var priority = 1
var i
this.editor = {}
this.jotted = jotted
// custom modemap for codemirror
var modemap = {
'html': 'htmlmixed'
}
options = util.extend(options, {
lineNumbers: true
})
// check if CodeMirror is loaded
if (typeof window.CodeMirror === 'undefined') {
return
}
var $editors = jotted.$container.querySelectorAll('.jotted-editor')
for (i = 0; i < $editors.length; i++) {
let $textarea = $editors[i].querySelector('textarea')
let type = util.data($textarea, 'jotted-type')
let file = util.data($textarea, 'jotted-file')
this.editor[type] = window.CodeMirror.fromTextArea($textarea, options)
this.editor[type].setOption('mode', util.getMode(type, file, modemap))
}
jotted.on('change', this.change.bind(this), priority)
}
editorChange (params) {
return () => {
// trigger a change event
this.jotted.trigger('change', params)
}
}
change (params, callback) {
var editor = this.editor[params.type]
// if the event is not started by the codemirror change.
// triggered only once per editor,
// when the textarea is populated/file is loaded.
if (!params.cmEditor) {
editor.setValue(params.content)
// attach the event only after the file is loaded
params.cmEditor = editor
editor.on('change', this.editorChange(params))
}
// manipulate the params and pass them on
params.content = editor.getValue()
callback(null, params)
}
}