-
Notifications
You must be signed in to change notification settings - Fork 434
/
use-codemirror.js
114 lines (101 loc) · 3.58 KB
/
use-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
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
var editors = new Array();
var unstaged = false;
// Show a dialog when there are unsaved changes (redo or undo) and the user is about to leave the page
window.onbeforeunload = function() {
if (unstaged) {
return true;
}
}
function cmMarkDiffLines(id) {
// as we can't use css to mark parents we use javascript to propagate diff lines
$('#revision_details_' + id + ' .cm-positive').parents('.CodeMirror-line').addClass('CodeMirror-positive-line');
$('#revision_details_' + id + ' .cm-negative').parents('.CodeMirror-line').addClass('CodeMirror-negative-line');
}
function use_codemirror(id, read_only, mode, big_editor) {
var codeMirrorOptions = {
lineNumbers: true,
matchBrackets: false,
fontSize: '1em',
mode: mode,
theme: "bootstrap"
};
if (read_only) {
codeMirrorOptions['readOnly'] = true;
}
else {
codeMirrorOptions['addToolBars'] = 0;
if (mode.length) {
codeMirrorOptions['mode'] = mode;
}
codeMirrorOptions['extraKeys'] = {
// Insert spaces instead of a tab
"Tab": function(cm) {
var spaces = '';
var indentUnit = cm.getOption('indentUnit');
for (var i = 0; i < indentUnit; i++) {
spaces += ' ';
}
cm.replaceSelection(spaces);
},
"Shift-Tab": "indentLess"
};
}
var textarea = $('#editor_' + id);
var editor = CodeMirror.fromTextArea(document.getElementById("editor_" + id), codeMirrorOptions);
editor.id = id;
if (big_editor) {
$(editor.getWrapperElement()).addClass('big-editor');
editor.refresh();
}
cmMarkDiffLines(id);
editor.on('scroll', function() { cmMarkDiffLines(id); });
if (!read_only) {
editor.setSelections(editor);
editor.on('change', function (cm) {
var undoChanged = cm.historySize().undo > 0,
redoChanged = cm.historySize().redo > 0;
cm.updateHistory(cm);
unstaged = undoChanged || redoChanged;
$("#undo_" + id).prop('disabled', !undoChanged);
$("#redo_" + id).prop('disabled', !redoChanged);
$("#save_" + id).prop('disabled', !undoChanged);
$("#comment_" + id).prop('disabled', !undoChanged);
});
CodeMirror.signal(editor, 'cursorActivity', editor);
}
if (textarea.data('save-url')) {
$('#save_' + id).click(function () {
var data = textarea.data('data');
data[data['submit']] = editors[id].getValue();
data['comment'] = $("#comment_" + id).val();
$(this).prop('disabled', true);
$("#loading_" + id).attr('disabled', true).removeClass("d-none");
$.ajax({
url: textarea.data('save-url'),
type: (textarea.data('save-method') || 'put'),
data: data,
success: function (data, textStatus, xhdr) {
$("#loading_" + id).addClass("d-none");
// The filter is necessary because we don't return a flash everywhere atm
$("#flash").show().html(data);
unstaged = false;
$("#undo_" + id).prop('disabled', true);
$("#redo_" + id).prop('disabled', true);
$("#save_" + id).prop('disabled', true);
$("#comment_" + id).prop('disabled', true);
$("#comment_" + id).val('');
},
error: function (xhdr, textStatus, errorThrown) {
$("#loading_" + id).removeAttr('disabled').addClass("d-none");
// The filter is necessary because we don't return a flash everywhere atm
$("#flash").show().html(xhdr.responseText);
unstaged = true;
}
});
});
} else {
$("#save_" + id).hide();
$("#comment_" + id).hide();
}
editors[id] = editor;
}