1 /* global adminpage, wpActiveEditor, quicktagsL10n, wpLink, prompt */
5 * This is the HTML editor in WordPress. It can be attached to any textarea and will
6 * append a toolbar above it. This script is self-contained (does not require external libraries).
8 * Run quicktags(settings) to initialize it, where settings is an object containing up to 3 properties:
10 * id : 'my_id', the HTML ID of the textarea, required
11 * buttons: '' Comma separated list of the names of the default buttons to show. Optional.
12 * Current list of default button names: 'strong,em,link,block,del,ins,img,ul,ol,li,code,more,close';
15 * The settings can also be a string quicktags_id.
17 * quicktags_id string The ID of the textarea that will be the editor canvas
18 * buttons string Comma separated list of the default buttons names that will be shown in that instance.
21 // new edit toolbar used with permission
23 // http://www.alexking.org/
28 /* jshint ignore:start */
33 * Define all former global functions so plugins that hack quicktags.js directly don't cause fatal errors.
35 var edAddTag = function(){},
36 edCheckOpenTags = function(){},
37 edCloseAllTags = function(){},
38 edInsertImage = function(){},
39 edInsertLink = function(){},
40 edInsertTag = function(){},
41 edLink = function(){},
42 edQuickLink = function(){},
43 edRemoveTag = function(){},
44 edShowButton = function(){},
45 edShowLinks = function(){},
46 edSpell = function(){},
47 edToolbar = function(){};
50 * Initialize new instance of the Quicktags editor
52 function quicktags(settings) {
53 return new QTags(settings);
57 * Inserts content at the caret in the active editor (textarea)
59 * Added for back compatibility
60 * @see QTags.insertContent()
62 function edInsertContent(bah, txt) {
63 return QTags.insertContent(txt);
67 * Adds a button to all instances of the editor
69 * Added for back compatibility, use QTags.addButton() as it gives more flexibility like type of button, button placement, etc.
70 * @see QTags.addButton()
72 function edButton(id, display, tagStart, tagEnd, access) {
73 return QTags.addButton( id, display, tagStart, tagEnd, access, '', -1 );
76 /* jshint ignore:end */
79 // private stuff is prefixed with an underscore
80 var _domReady = function(func) {
81 var t, i, DOMContentLoaded, _tryReady;
83 if ( typeof jQuery !== 'undefined' ) {
84 jQuery(document).ready(func);
89 t.ready = function() {
92 for ( i = 0; i < t.funcs.length; i++ ) {
104 if ( ! t.eventAttached ) {
105 if ( document.addEventListener ) {
106 DOMContentLoaded = function(){document.removeEventListener('DOMContentLoaded', DOMContentLoaded, false);t.ready();};
107 document.addEventListener('DOMContentLoaded', DOMContentLoaded, false);
108 window.addEventListener('load', t.ready, false);
109 } else if ( document.attachEvent ) {
110 DOMContentLoaded = function(){if (document.readyState === 'complete'){ document.detachEvent('onreadystatechange', DOMContentLoaded);t.ready();}};
111 document.attachEvent('onreadystatechange', DOMContentLoaded);
112 window.attachEvent('onload', t.ready);
114 _tryReady = function() {
116 document.documentElement.doScroll('left');
118 setTimeout(_tryReady, 50);
127 t.eventAttached = true;
132 _datetime = (function() {
133 var now = new Date(), zeroise;
135 zeroise = function(number) {
136 var str = number.toString();
138 if ( str.length < 2 ) {
145 return now.getUTCFullYear() + '-' +
146 zeroise( now.getUTCMonth() + 1 ) + '-' +
147 zeroise( now.getUTCDate() ) + 'T' +
148 zeroise( now.getUTCHours() ) + ':' +
149 zeroise( now.getUTCMinutes() ) + ':' +
150 zeroise( now.getUTCSeconds() ) +
155 qt = QTags = function(settings) {
156 if ( typeof(settings) === 'string' ) {
157 settings = {id: settings};
158 } else if ( typeof(settings) !== 'object' ) {
164 canvas = document.getElementById(id),
166 tb, onclick, toolbar_id;
168 if ( !id || !canvas ) {
175 t.settings = settings;
177 if ( id === 'content' && typeof(adminpage) === 'string' && ( adminpage === 'post-new-php' || adminpage === 'post-php' ) ) {
178 // back compat hack :-(
180 toolbar_id = 'ed_toolbar';
182 toolbar_id = name + '_toolbar';
185 tb = document.createElement('div');
187 tb.className = 'quicktags-toolbar';
188 tb.onclick = function() {
189 window.wpActiveEditor = id;
192 canvas.parentNode.insertBefore(tb, canvas);
195 // listen for click events
196 onclick = function(e) {
197 e = e || window.event;
198 var target = e.target || e.srcElement, visible = target.clientWidth || target.offsetWidth, i;
200 // don't call the callback on pressing the accesskey when the button is not visible
205 // as long as it has the class ed_button, execute the callback
206 if ( / ed_button /.test(' ' + target.className + ' ') ) {
207 // we have to reassign canvas here
208 t.canvas = canvas = document.getElementById(id);
209 i = target.id.replace(name + '_', '');
211 if ( t.theButtons[i] ) {
212 t.theButtons[i].callback.call(t.theButtons[i], target, canvas, t);
217 if ( tb.addEventListener ) {
218 tb.addEventListener('click', onclick, false);
219 } else if ( tb.attachEvent ) {
220 tb.attachEvent('onclick', onclick);
223 t.getButton = function(id) {
224 return t.theButtons[id];
227 t.getButtonElement = function(id) {
228 return document.getElementById(name + '_' + id);
231 qt.instances[id] = t;
233 if ( !qt.instances[0] ) {
234 qt.instances[0] = qt.instances[id];
235 _domReady( function(){ qt._buttonsInit(); } );
241 qt.getInstance = function(id) {
242 return qt.instances[id];
245 qt._buttonsInit = function() {
246 var t = this, canvas, name, settings, theButtons, html, inst, ed, id, i, use,
247 defaults = ',strong,em,link,block,del,ins,img,ul,ol,li,code,more,close,';
249 for ( inst in t.instances ) {
254 ed = t.instances[inst];
257 settings = ed.settings;
263 if ( settings.buttons ) {
264 use = ','+settings.buttons+',';
267 for ( i in edButtons ) {
268 if ( !edButtons[i] ) {
272 id = edButtons[i].id;
273 if ( use && defaults.indexOf( ',' + id + ',' ) !== -1 && use.indexOf( ',' + id + ',' ) === -1 ) {
277 if ( !edButtons[i].instance || edButtons[i].instance === inst ) {
278 theButtons[id] = edButtons[i];
280 if ( edButtons[i].html ) {
281 html += edButtons[i].html(name + '_');
286 if ( use && use.indexOf(',fullscreen,') !== -1 ) {
287 theButtons.fullscreen = new qt.FullscreenButton();
288 html += theButtons.fullscreen.html(name + '_');
292 if ( 'rtl' === document.getElementsByTagName('html')[0].dir ) {
293 theButtons.textdirection = new qt.TextDirectionButton();
294 html += theButtons.textdirection.html(name + '_');
297 ed.toolbar.innerHTML = html;
298 ed.theButtons = theButtons;
300 t.buttonsInitDone = true;
304 * Main API function for adding a button to Quicktags
306 * Adds qt.Button or qt.TagButton depending on the args. The first three args are always required.
307 * To be able to add button(s) to Quicktags, your script should be enqueued as dependent
308 * on "quicktags" and outputted in the footer. If you are echoing JS directly from PHP,
309 * use add_action( 'admin_print_footer_scripts', 'output_my_js', 100 ) or add_action( 'wp_footer', 'output_my_js', 100 )
311 * Minimum required to add a button that calls an external function:
312 * QTags.addButton( 'my_id', 'my button', my_callback );
313 * function my_callback() { alert('yeah!'); }
315 * Minimum required to add a button that inserts a tag:
316 * QTags.addButton( 'my_id', 'my button', '<span>', '</span>' );
317 * QTags.addButton( 'my_id2', 'my button', '<br />' );
319 * @param string id Required. Button HTML ID
320 * @param string display Required. Button's value="..."
321 * @param string|function arg1 Required. Either a starting tag to be inserted like "<span>" or a callback that is executed when the button is clicked.
322 * @param string arg2 Optional. Ending tag like "</span>"
323 * @param string access_key Optional. Access key for the button.
324 * @param string title Optional. Button's title="..."
325 * @param int priority Optional. Number representing the desired position of the button in the toolbar. 1 - 9 = first, 11 - 19 = second, 21 - 29 = third, etc.
326 * @param string instance Optional. Limit the button to a specifric instance of Quicktags, add to all instances if not present.
327 * @return mixed null or the button object that is needed for back-compat.
329 qt.addButton = function( id, display, arg1, arg2, access_key, title, priority, instance ) {
332 if ( !id || !display ) {
336 priority = priority || 0;
339 if ( typeof(arg1) === 'function' ) {
340 btn = new qt.Button(id, display, access_key, title, instance);
342 } else if ( typeof(arg1) === 'string' ) {
343 btn = new qt.TagButton(id, display, arg1, arg2, access_key, title, instance);
348 if ( priority === -1 ) { // back-compat
352 if ( priority > 0 ) {
353 while ( typeof(edButtons[priority]) !== 'undefined' ) {
357 edButtons[priority] = btn;
359 edButtons[edButtons.length] = btn;
362 if ( this.buttonsInitDone ) {
363 this._buttonsInit(); // add the button HTML to all instances toolbars if addButton() was called too late
367 qt.insertContent = function(content) {
368 var sel, startPos, endPos, scrollTop, text, canvas = document.getElementById(wpActiveEditor);
374 if ( document.selection ) { //IE
376 sel = document.selection.createRange();
379 } else if ( canvas.selectionStart || canvas.selectionStart === 0 ) { // FF, WebKit, Opera
381 startPos = canvas.selectionStart;
382 endPos = canvas.selectionEnd;
383 scrollTop = canvas.scrollTop;
385 canvas.value = text.substring(0, startPos) + content + text.substring(endPos, text.length);
388 canvas.selectionStart = startPos + content.length;
389 canvas.selectionEnd = startPos + content.length;
390 canvas.scrollTop = scrollTop;
392 canvas.value += content;
398 // a plain, dumb button
399 qt.Button = function(id, display, access, title, instance) {
404 t.title = title || '';
405 t.instance = instance || '';
407 qt.Button.prototype.html = function(idPrefix) {
408 var access = this.access ? ' accesskey="' + this.access + '"' : '';
409 if ( this.id === 'fullscreen' ) {
410 return '<button type="button" id="' + idPrefix + this.id + '"' + access + ' class="ed_button qt-fullscreen" title="' + this.title + '"></button>';
412 return '<input type="button" id="' + idPrefix + this.id + '"' + access + ' class="ed_button button button-small" title="' + this.title + '" value="' + this.display + '" />';
414 qt.Button.prototype.callback = function(){};
416 // a button that inserts HTML tag
417 qt.TagButton = function(id, display, tagStart, tagEnd, access, title, instance) {
419 qt.Button.call(t, id, display, access, title, instance);
420 t.tagStart = tagStart;
423 qt.TagButton.prototype = new qt.Button();
424 qt.TagButton.prototype.openTag = function(e, ed) {
427 if ( ! ed.openTags ) {
431 ed.openTags.push(t.id);
432 e.value = '/' + e.value;
435 qt.TagButton.prototype.closeTag = function(e, ed) {
436 var t = this, i = t.isOpen(ed);
439 ed.openTags.splice(i, 1);
444 // whether a tag is open or not. Returns false if not open, or current open depth of the tag
445 qt.TagButton.prototype.isOpen = function (ed) {
446 var t = this, i = 0, ret = false;
448 while ( ret === false && i < ed.openTags.length ) {
449 ret = ed.openTags[i] === t.id ? i : false;
457 qt.TagButton.prototype.callback = function(element, canvas, ed) {
458 var t = this, startPos, endPos, cursorPos, scrollTop, v = canvas.value, l, r, i, sel, endTag = v ? t.tagEnd : '';
460 if ( document.selection ) { // IE
462 sel = document.selection.createRange();
463 if ( sel.text.length > 0 ) {
465 sel.text = sel.text + t.tagStart;
467 sel.text = t.tagStart + sel.text + endTag;
471 sel.text = t.tagStart;
472 } else if ( t.isOpen(ed) === false ) {
473 sel.text = t.tagStart;
474 t.openTag(element, ed);
477 t.closeTag(element, ed);
481 } else if ( canvas.selectionStart || canvas.selectionStart === 0 ) { // FF, WebKit, Opera
482 startPos = canvas.selectionStart;
483 endPos = canvas.selectionEnd;
485 scrollTop = canvas.scrollTop;
486 l = v.substring(0, startPos); // left of the selection
487 r = v.substring(endPos, v.length); // right of the selection
488 i = v.substring(startPos, endPos); // inside the selection
489 if ( startPos !== endPos ) {
491 canvas.value = l + i + t.tagStart + r; // insert self closing tags after the selection
492 cursorPos += t.tagStart.length;
494 canvas.value = l + t.tagStart + i + endTag + r;
495 cursorPos += t.tagStart.length + endTag.length;
499 canvas.value = l + t.tagStart + r;
500 cursorPos = startPos + t.tagStart.length;
501 } else if ( t.isOpen(ed) === false ) {
502 canvas.value = l + t.tagStart + r;
503 t.openTag(element, ed);
504 cursorPos = startPos + t.tagStart.length;
506 canvas.value = l + endTag + r;
507 cursorPos = startPos + endTag.length;
508 t.closeTag(element, ed);
513 canvas.selectionStart = cursorPos;
514 canvas.selectionEnd = cursorPos;
515 canvas.scrollTop = scrollTop;
516 } else { // other browsers?
518 canvas.value += t.tagStart;
519 } else if ( t.isOpen(ed) !== false ) {
520 canvas.value += t.tagStart;
521 t.openTag(element, ed);
523 canvas.value += endTag;
524 t.closeTag(element, ed);
531 qt.SpellButton = function() {};
533 // the close tags button
534 qt.CloseButton = function() {
535 qt.Button.call(this, 'close', quicktagsL10n.closeTags, '', quicktagsL10n.closeAllOpenTags);
538 qt.CloseButton.prototype = new qt.Button();
540 qt._close = function(e, c, ed) {
541 var button, element, tbo = ed.openTags;
544 while ( tbo.length > 0 ) {
545 button = ed.getButton(tbo[tbo.length - 1]);
546 element = document.getElementById(ed.name + '_' + button.id);
549 button.callback.call(button, element, c, ed);
551 button.closeTag(element, ed);
557 qt.CloseButton.prototype.callback = qt._close;
559 qt.closeAllTags = function(editor_id) {
560 var ed = this.getInstance(editor_id);
561 qt._close('', ed.canvas, ed);
565 qt.LinkButton = function() {
566 qt.TagButton.call(this, 'link', 'link', '', '</a>', 'a');
568 qt.LinkButton.prototype = new qt.TagButton();
569 qt.LinkButton.prototype.callback = function(e, c, ed, defaultValue) {
572 if ( typeof wpLink !== 'undefined' ) {
573 wpLink.open( ed.id );
577 if ( ! defaultValue ) {
578 defaultValue = 'http://';
581 if ( t.isOpen(ed) === false ) {
582 URL = prompt(quicktagsL10n.enterURL, defaultValue);
584 t.tagStart = '<a href="' + URL + '">';
585 qt.TagButton.prototype.callback.call(t, e, c, ed);
588 qt.TagButton.prototype.callback.call(t, e, c, ed);
593 qt.ImgButton = function() {
594 qt.TagButton.call(this, 'img', 'img', '', '', 'm');
596 qt.ImgButton.prototype = new qt.TagButton();
597 qt.ImgButton.prototype.callback = function(e, c, ed, defaultValue) {
598 if ( ! defaultValue ) {
599 defaultValue = 'http://';
601 var src = prompt(quicktagsL10n.enterImageURL, defaultValue), alt;
603 alt = prompt(quicktagsL10n.enterImageDescription, '');
604 this.tagStart = '<img src="' + src + '" alt="' + alt + '" />';
605 qt.TagButton.prototype.callback.call(this, e, c, ed);
609 qt.FullscreenButton = function() {
610 qt.Button.call(this, 'fullscreen', quicktagsL10n.fullscreen, 'f', quicktagsL10n.toggleFullscreen);
612 qt.FullscreenButton.prototype = new qt.Button();
613 qt.FullscreenButton.prototype.callback = function(e, c) {
614 if ( ! c.id || typeof wp === 'undefined' || ! wp.editor || ! wp.editor.fullscreen ) {
618 wp.editor.fullscreen.on();
621 qt.TextDirectionButton = function() {
622 qt.Button.call(this, 'textdirection', quicktagsL10n.textdirection, '', quicktagsL10n.toggleTextdirection);
624 qt.TextDirectionButton.prototype = new qt.Button();
625 qt.TextDirectionButton.prototype.callback = function(e, c) {
626 var isRTL = ( 'rtl' === document.getElementsByTagName('html')[0].dir ),
627 currentDirection = c.style.direction;
629 if ( ! currentDirection ) {
630 currentDirection = ( isRTL ) ? 'rtl' : 'ltr';
633 c.style.direction = ( 'rtl' === currentDirection ) ? 'ltr' : 'rtl';
637 // ensure backward compatibility
638 edButtons[10] = new qt.TagButton('strong','b','<strong>','</strong>','b');
639 edButtons[20] = new qt.TagButton('em','i','<em>','</em>','i'),
640 edButtons[30] = new qt.LinkButton(), // special case
641 edButtons[40] = new qt.TagButton('block','b-quote','\n\n<blockquote>','</blockquote>\n\n','q'),
642 edButtons[50] = new qt.TagButton('del','del','<del datetime="' + _datetime + '">','</del>','d'),
643 edButtons[60] = new qt.TagButton('ins','ins','<ins datetime="' + _datetime + '">','</ins>','s'),
644 edButtons[70] = new qt.ImgButton(), // special case
645 edButtons[80] = new qt.TagButton('ul','ul','<ul>\n','</ul>\n\n','u'),
646 edButtons[90] = new qt.TagButton('ol','ol','<ol>\n','</ol>\n\n','o'),
647 edButtons[100] = new qt.TagButton('li','li','\t<li>','</li>\n','l'),
648 edButtons[110] = new qt.TagButton('code','code','<code>','</code>','c'),
649 edButtons[120] = new qt.TagButton('more','more','<!--more-->\n\n','','t'),
650 edButtons[140] = new qt.CloseButton();