/
two-paned-editor.js
50 lines (48 loc) · 1.37 KB
/
two-paned-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
function TwoPanedEditor(settings) {
var nextUpdateIsSilent = false;
var nextUpdateIsInstant = false;
var DELAY_MS = 300;
var delay = null;
var editor = CodeMirror(function(element) {
$(settings.editor).append(element);
}, {
mode: "text/html",
theme: "jsbin",
tabMode: "indent",
lineWrapping: true,
lineNumbers: true,
readOnly: true,
onCursorActivity: function() {
if (editor.nextCursorActivityIsAutomated) {
editor.lastCursorPos = editor.getCursor();
editor.nextCursorActivityIsAutomated = false;
} else {
var canChange = !editor.getOption("readOnly");
if (!canChange && editor.lastCursorPos) {
editor.nextCursorActivityIsAutomated = true;
editor.setCursor(editor.lastCursorPos);
}
}
},
onChange: function schedulePreviewRefresh() {
if (nextUpdateIsSilent) {
nextUpdateIsSilent = false;
} else {
clearTimeout(delay);
if (nextUpdateIsInstant) {
nextUpdateIsInstant = false;
updatePreview();
} else
delay = setTimeout(updatePreview, DELAY_MS);
}
}
});
function updatePreview() {
var previewDocument = $(settings.preview).contents()[0];
previewDocument.open();
previewDocument.write(editor.getValue());
previewDocument.close();
}
updatePreview();
return editor;
}