forked from Redactor2/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.js
201 lines (165 loc) · 5.44 KB
/
source.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
(function($)
{
$.Redactor.prototype.source = function()
{
return {
init: function()
{
var button = this.button.addFirst('html', 'HTML');
this.button.addCallback(button, this.source.toggle);
var style = {
'width': '100%',
'margin': '0',
'background': '#111',
'box-sizing': 'border-box',
'color': 'rgba(255, 255, 255, .8)',
'font-size': '14px',
'outline': 'none',
'padding': '16px',
'line-height': '22px',
'font-family': 'Menlo, Monaco, Consolas, "Courier New", monospace'
};
// This line here is a fix so that we can use ng-model binding for the code view
// for the redactor class. It basically grabs the original textarea field and binds
// it to the text area of the code view in the redactor so that the ng-model bind will
// be bound to the code view as well as the rich content editor view.
this.source.$textarea = $(this.core.box()[0].getElementsByTagName('textarea')[0]);
this.source.$textarea.css(style).hide();
if (this.opts.type === 'textarea')
{
this.core.box().append(this.source.$textarea);
}
else
{
this.core.box().after(this.source.$textarea);
}
this.core.element().on('destroy.callback.redactor', $.proxy(function()
{
this.source.$textarea.remove();
}, this));
},
toggle: function()
{
return (this.source.$textarea.hasClass('open')) ? this.source.hide() : this.source.show();
},
setCaretOnShow: function()
{
this.source.offset = this.offset.get();
var scroll = $(window).scrollTop();
var width = this.core.editor().innerWidth();
var height = this.core.editor().innerHeight();
// caret position sync
this.source.start = 0;
this.source.end = 0;
var $editorDiv = $("<div/>").append($.parseHTML(this.core.editor().html(), document, true));
var $selectionMarkers = $editorDiv.find("span.redactor-selection-marker");
if ($selectionMarkers.length > 0)
{
var editorHtml = $editorDiv.html().replace(/&/g, '&');
if ($selectionMarkers.length === 1)
{
this.source.start = this.utils.strpos(editorHtml, $editorDiv.find("#selection-marker-1").prop("outerHTML"));
this.source.end = this.source.start;
}
else if ($selectionMarkers.length === 2)
{
this.source.start = this.utils.strpos(editorHtml, $editorDiv.find("#selection-marker-1").prop("outerHTML"));
this.source.end = this.utils.strpos(editorHtml, $editorDiv.find("#selection-marker-2").prop("outerHTML")) - $editorDiv.find("#selection-marker-1").prop("outerHTML").toString().length;
}
}
},
setCaretOnHide: function(html)
{
this.source.start = this.source.$textarea.get(0).selectionStart;
this.source.end = this.source.$textarea.get(0).selectionEnd;
// if selection starts from end
if (this.source.start > this.source.end && this.source.end > 0)
{
var tempStart = this.source.end;
var tempEnd = this.source.start;
this.source.start = tempStart;
this.source.end = tempEnd;
}
this.source.start = this.source.enlargeOffset(html, this.source.start);
this.source.end = this.source.enlargeOffset(html, this.source.end);
html = html.substr(0, this.source.start) + this.marker.html(1) + html.substr(this.source.start);
if (this.source.end > this.source.start)
{
var markerLength = this.marker.html(1).toString().length;
html = html.substr(0, this.source.end + markerLength) + this.marker.html(2) + html.substr(this.source.end + markerLength);
}
return html;
},
hide: function()
{
this.source.$textarea.removeClass('open').hide();
this.source.$textarea.off('.redactor-source');
var code = this.source.$textarea.val();
// Disable to workaround bug in paragraphize, which will keep adding <p> wrapper for some top level element such as <a>
// code = this.paragraphize.load(code);
code = this.source.setCaretOnHide(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.source.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.source.$textarea.val(code).height(height).addClass('open').show();
this.source.$textarea.on('keyup.redactor-source', $.proxy(function()
{
if (this.opts.type === 'textarea')
{
this.core.textarea().val(this.source.$textarea.val());
}
}, this));
this.marker.remove();
$(window).scrollTop(scroll);
if (this.source.$textarea[0].setSelectionRange)
{
this.source.$textarea[0].setSelectionRange(this.source.start, this.source.end);
}
this.source.$textarea[0].scrollTop = 0;
setTimeout($.proxy(function()
{
this.source.$textarea.focus();
}, this), 0);
},
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);