forked from Redactor2/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodemirror.js
213 lines (173 loc) · 5.96 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
(function($)
{
$.Redactor.prototype.codemirror = function()
{
return {
init: function()
{
var button = this.button.addFirst('html', 'HTML');
this.button.addCallback(button, this.codemirror.toggle);
this.codemirror.$textarea = $('<textarea />');
this.codemirror.$textarea.hide();
if (this.opts.type === 'textarea')
{
this.core.box().append(this.codemirror.$textarea);
}
else
{
this.core.box().after(this.codemirror.$textarea);
}
this.core.element().on('destroy.callback.redactor', $.proxy(function()
{
this.codemirror.$textarea.remove();
}, this));
var settings = (typeof this.opts.codemirror !== 'undefined') ? this.opts.codemirror : { lineNumbers: true };
this.codemirror.editor = CodeMirror.fromTextArea(this.codemirror.$textarea[0], settings);
this.codemirror.$textarea.next('.CodeMirror').hide();
},
toggle: function()
{
return (this.codemirror.$textarea.hasClass('open')) ? this.codemirror.hide() : this.codemirror.show();
},
setCaretOnShow: function()
{
this.codemirror.offset = this.offset.get();
var scroll = $(window).scrollTop();
var width = this.core.editor().innerWidth();
var height = this.core.editor().innerHeight();
// caret position sync
this.codemirror.start = 0;
this.codemirror.end = 0;
var $editorDiv = $("<div/>").append($.parseHTML(this.core.editor().html(), document, true));
var $selectionMarkers = $editorDiv.find(".redactor-selection-marker");
if ($selectionMarkers.length > 0)
{
var editorHtml = $editorDiv.html().replace(/&/g, '&');
if ($selectionMarkers.length === 1)
{
this.codemirror.start = this.utils.strpos(editorHtml, $editorDiv.find("#selection-marker-1").prop("outerHTML"));
this.codemirror.end = this.codemirror.start;
}
else if ($selectionMarkers.length === 2)
{
this.codemirror.start = this.utils.strpos(editorHtml, $editorDiv.find("#selection-marker-1").prop("outerHTML"));
this.codemirror.end = this.utils.strpos(editorHtml, $editorDiv.find("#selection-marker-2").prop("outerHTML")) - $editorDiv.find("#selection-marker-1").prop("outerHTML").toString().length;
}
}
},
setCaretOnHide: function()
{
this.codemirror.start = 0;
this.codemirror.end = 0;
var self = this;
var html = '';
this.codemirror.$textarea.next('.CodeMirror').each(function(i, el)
{
self.codemirror.selection = el.CodeMirror.listSelections();
self.codemirror.start = el.CodeMirror.indexFromPos(self.codemirror.selection[0].anchor);
self.codemirror.end = el.CodeMirror.indexFromPos(self.codemirror.selection[0].head);
html = el.CodeMirror.getValue();
});
// if selection starts from end
if (this.codemirror.start > this.codemirror.end && this.codemirror.end > 0)
{
var tempStart = this.codemirror.end;
var tempEnd = this.codemirror.start;
this.codemirror.start = tempStart;
this.codemirror.end = tempEnd;
}
this.codemirror.start = this.codemirror.enlargeOffset(html, this.codemirror.start);
this.codemirror.end = this.codemirror.enlargeOffset(html, this.codemirror.end);
html = html.substr(0, this.codemirror.start) + this.marker.html(1) + html.substr(this.codemirror.start);
if (this.codemirror.end > this.codemirror.start)
{
var markerLength = this.marker.html(1).toString().length;
html = html.substr(0, this.codemirror.end + markerLength) + this.marker.html(2) + html.substr(this.codemirror.end + markerLength);
}
return html;
},
hide: function()
{
var code;
this.codemirror.$textarea.removeClass('open').next('.CodeMirror').hide();
this.codemirror.$textarea.next('.CodeMirror').off('.redactor-codemirror');
code = this.codemirror.setCaretOnHide(code);
code = this.paragraphize.load(code);
this.code.start(code);
this.button.enableAll();
this.core.editor().show().focus();
this.selection.restore();
//this.code.sync();
},
show: function()
{
this.selection.save();
this.codemirror.setCaretOnShow();
var height = this.core.editor().innerHeight();
var code = this.code.get();
code = code.replace(/\n\n\n/g, "\n");
code = code.replace(/\n\n/g, "\n");
this.core.editor().hide();
this.button.disableAll('html');
this.marker.remove();
this.codemirror.$textarea.val(code).height(height).addClass('open');
this.codemirror.$textarea.next('.CodeMirror').on('keyup.redactor-codemirror', $.proxy(function()
{
if (this.opts.type === 'textarea')
{
this.codemirror.$textarea.next('.CodeMirror').each($.proxy(function(i, el)
{
this.core.textarea().val(el.CodeMirror.getValue());
}, this));
}
}, this));
var self = this;
this.codemirror.$textarea.next('.CodeMirror').each(function(i, el)
{
$(el).show();
el.CodeMirror.setValue(code);
el.CodeMirror.setSize('100%', height);
el.CodeMirror.refresh();
if (self.codemirror.start === self.codemirror.end)
{
el.CodeMirror.setCursor(el.CodeMirror.posFromIndex(self.codemirror.start).line, el.CodeMirror.posFromIndex(self.codemirror.end).ch);
}
else
{
el.CodeMirror.setSelection({line: el.CodeMirror.posFromIndex(self.codemirror.start).line,
ch: el.CodeMirror.posFromIndex(self.codemirror.start).ch},
{line: el.CodeMirror.posFromIndex(self.codemirror.end).line,
ch: el.CodeMirror.posFromIndex(self.codemirror.end).ch});
}
el.CodeMirror.focus();
});
},
enlargeOffset: function(html, offset)
{
var htmlLength = html.length;
var c = 0;
if (html[offset] === '>')
{
c++;
}
else
{
for(var i = offset; i <= htmlLength; i++)
{
c++;
if (html[i] === '>')
{
break;
}
else if (html[i] === '<' || i === htmlLength)
{
c = 0;
break;
}
}
}
return offset + c;
}
};
};
})(jQuery);