atd-nonvis-editor-plugin.js
8.92 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* jshint devel: true, onevar: false, smarttabs: true */
/* global AtD, QTags, AtD_l10n_r0ar, edButtons, edButton, switchEditors, AtD_unbind_proofreader_listeners */
/* exported AtD_unbind_proofreader_listeners */
var AtD_qtbutton, autosave;
/* convienence method to restore the text area from the preview div */
function AtD_restore_text_area() {
var content;
/* swap the preview div for the textarea, notice how I have to restore the appropriate class/id/style attributes */
if( jQuery('#atd-content').get(0) ) {
AtD.remove('atd-content');
content = jQuery('#atd-content').html();
} else {
AtD.remove('content');
content = jQuery('#content').html();
}
if ( navigator.appName === 'Microsoft Internet Explorer' ) {
content = content.replace(/<BR.*?class.*?atd_remove_me.*?>/gi, '\n');
}
// Remove emoji replacement images
if ( typeof window.twemoji !== 'undefined' ) {
content = content.replace( /<img [^>]+>/g, function( img ) {
// The images should have class containing 'emoji'
if ( img.indexOf( 'emoji' ) !== -1 ) {
var alt = img.match( /alt="([^"]+)"/ );
if ( alt && alt[1] && window.twemoji.test( alt[1] ) ) {
return alt[1];
}
}
return img;
});
}
// jQuery('#content').replaceWith( AtD.content_canvas );
jQuery('#content').val( content.replace(/\<\;/g, '<').replace(/\>\;/g, '>').replace(/\&/g, '&') )
.height(AtD.height)
.show();
jQuery('#atd-content').remove();
if ( AtD_qtbutton ) {
/* change the link text back to its original label */
jQuery(AtD_qtbutton).val( AtD.getLang('button_proofread', 'proofread') );
jQuery(AtD_qtbutton).css({ 'color' : '#464646' });
/* enable the toolbar buttons */
jQuery( AtD_qtbutton ).siblings('input').andSelf().attr( 'disabled', false );
}
/* restore autosave */
if ( AtD.autosave !== undefined ) {
if ( window.wp && window.wp.autosave && window.wp.autosave.server ) {
window.wp.autosave.local.resume && window.wp.autosave.local.resume();
window.wp.autosave.server.resume && window.wp.autosave.server.resume();
} else {
autosave = AtD.autosave;
}
}
}
// add the AtD button properly to quicktags
if ( typeof(QTags) !== 'undefined' && QTags.addButton ) {
jQuery(document).ready(function(){
QTags.addButton( 'AtD', AtD_l10n_r0ar.button_proofread, AtD_check );
});
} else {
edButtons[edButtons.length] = new edButton('ed_AtD', 'AtD', '', '', '');
jQuery(document).ready(function($){
$('#ed_AtD').replaceWith('<input type="button" id="ed_AtD" accesskey="" class="ed_button" onclick="AtD_check(this);" value="' + AtD_l10n_r0ar.button_proofread + '" />');
});
}
function AtD_restore_if_proofreading() {
if ( AtD_qtbutton && jQuery(AtD_qtbutton).val() === AtD.getLang('button_edit_text', 'edit text') ) {
AtD_restore_text_area();
}
}
/* exported AtD_unbind_proofreader_listeners */
function AtD_unbind_proofreader_listeners() {
jQuery('#save-post, #post-preview, #publish, #edButtonPreview').unbind('focus', AtD_restore_if_proofreading );
jQuery('#add_poll, #add_image, #add_video, #add_audio, #add_media').unbind('click', AtD_restore_if_proofreading );
jQuery('#post').unbind('submit', AtD_restore_if_proofreading );
}
function AtD_bind_proofreader_listeners() {
jQuery('#save-post, #post-preview, #publish, #edButtonPreview').focus( AtD_restore_if_proofreading );
jQuery('#add_poll, #add_image, #add_video, #add_audio, #add_media').click( AtD_restore_if_proofreading );
jQuery('#post').submit( AtD_restore_if_proofreading );
}
/* where the magic happens, checks the spelling or restores the form */
function AtD_check(button) {
var callback, divHeight;
if ( jQuery.isFunction( button ) ) {
callback = button;
if ( !AtD_qtbutton ) {
AtD_qtbutton = jQuery( '#qt_content_AtD, #ed_AtD' ).get( 0 );
}
} else {
if ( !button.id ) {
button = button[0];
}
AtD_qtbutton = button;
}
if ( !jQuery('#content').length ) {
if ( 'undefined' !== typeof callback ) {
callback( 0 );
}
AtD_restore_if_proofreading();
return;
}
/* If the text of the link says edit comment, then restore the textarea so the user can edit the text */
if ( jQuery(AtD_qtbutton).val() === AtD.getLang('button_edit_text', 'edit text') ) {
AtD_restore_text_area();
} else {
// Disable editor expand/scroll
if ( window.editorExpand && jQuery( '#postdivrich' ).hasClass( 'wp-editor-expand' ) ) {
AtD.wpEditorExpand = true;
// window.editorExpand.off && window.editorExpand.off();
} else {
AtD.wpEditorExpand = false;
}
/* initialize some of the stuff related to this plugin */
if ( ! AtD.height ) {
AtD.height = jQuery('#content').height();
AtD_bind_proofreader_listeners();
/* make it so clicking the Visual button works when AtD is active */
jQuery('#edButtonPreview').attr( 'onclick', null ).click( function() {
AtD_restore_if_proofreading();
switchEditors.go( 'content', 'tinymce' );
});
/* saved the textarea as we need to restore the original one for the toolbar to continue to function properly */
AtD.content_canvas = jQuery('#content');
/* store the autosave, we're going to make it empty during spellcheck to prevent auto saved text from being
over written with empty text */
AtD.autosave = autosave;
} else {
// Update the height
AtD.height = jQuery('#content').height();
}
/* set the spell check link to a link that lets the user edit the text */
/* disable the button to prevent a race condition where content is deleted if proofread is clicked with a check
in progress. */
jQuery(AtD_qtbutton).css({ 'color' : 'red' }).val( AtD.getLang('button_edit_text', 'edit text') ).attr('disabled', true);
/* replace the div */
var $replacement,
$textarea = jQuery('#content'),
text = $textarea.val().replace( /\&/g, '&' ).replace( /</g, '<' ).replace( /\>/g, '>' ),
fontFamily = $textarea.css('font-family'),
fontSize = $textarea.css('font-size'),
lineHeight = $textarea.css('line-height');
if ( navigator.appName === 'Microsoft Internet Explorer' ) {
text = text.replace( /[\n\r\f]/gm, '<BR class="atd_remove_me">' );
}
$replacement = jQuery( '<div class="input" id="atd-content">' + text + '</div>' );
$textarea.after( $replacement ).hide();
divHeight = AtD.height;
// AtD disables resizing of the Text editor, normalize the size of the replacement div.
if ( divHeight < 200 ) {
divHeight = 200;
} else if ( divHeight > 1000 ) {
divHeight = 1000;
}
var toolBarHeight = jQuery('#ed_toolbar').height();
$replacement.css( {
overflow: 'auto',
'background-color': 'white',
color: 'black',
'white-space': 'pre-wrap',
padding: '10px',
'font-family': fontFamily || 'Consolas, Monaco, monospace',
'font-size': fontSize || '13px',
'line-height': lineHeight || '1.5',
height: divHeight,
'margin-top': toolBarHeight+7+'px'
} );
/* kill autosave... :) */
if ( window.wp && window.wp.autosave && window.wp.autosave.server ) {
window.wp.autosave.local.suspend && window.wp.autosave.local.suspend();
window.wp.autosave.server.suspend && window.wp.autosave.server.suspend();
} else {
autosave = function() { };
}
/* disable the toolbar buttons */
jQuery( AtD_qtbutton ).siblings('input').andSelf().attr( 'disabled', true ); // using .arrt instead of .prop so it's compat with older WP and jQuery
/* check the writing in the textarea */
AtD.check('atd-content', {
success: function(errorCount) {
if ( Number( errorCount ) === 0 && typeof callback !== 'function' ) {
alert( AtD.getLang('message_no_errors_found', 'No writing errors were found') );
}
AtD_restore_if_proofreading();
},
ready: function(errorCount) {
jQuery(AtD_qtbutton).attr('disabled', false);
if ( typeof callback === 'function' ) {
callback( errorCount );
}
},
error: function() {
jQuery(AtD_qtbutton).attr('disabled', false);
if ( typeof callback === 'function' ) {
callback( -1 );
} else {
alert( AtD.getLang('message_server_error', 'There was a problem communicating with the Proofreading service. Try again in one minute.') );
}
AtD_restore_if_proofreading();
},
editSelection: function(element) {
var text = prompt( AtD.getLang('dialog_replace_selection', 'Replace selection with:'), element.text() );
if ( text != null ) {
element.replaceWith( text );
}
},
explain: function(url) {
var left = (screen.width / 2) - (480 / 2);
var top = (screen.height / 2) - (380 / 2);
window.open( url, '', 'width=480,height=380,toolbar=0,status=0,resizable=0,location=0,menuBar=0,left=' + left + ',top=' + top).focus();
},
ignore: function(word) {
jQuery.ajax({
type : 'GET',
url : AtD.rpc_ignore + encodeURI( word ).replace( /&/g, '%26'),
format : 'raw',
error : function(XHR, status, error) {
if ( AtD.callback_f !== undefined && AtD.callback_f.error !== undefined ) {
AtD.callback_f.error(status + ': ' + error);
}
}
});
}
});
}
}