2 tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
3 var toolbar, serializer, touchOnImage,
8 function isPlaceholder( node ) {
9 return !! ( editor.dom.getAttrib( node, 'data-mce-placeholder' ) || editor.dom.getAttrib( node, 'data-mce-object' ) );
12 editor.addButton( 'wp_img_remove', {
14 icon: 'dashicon dashicons-no',
16 removeImage( editor.selection.getNode() );
20 editor.addButton( 'wp_img_edit', {
21 tooltip: 'Edit ', // trailing space is needed, used for context
22 icon: 'dashicon dashicons-edit',
24 editImage( editor.selection.getNode() );
29 alignleft: 'Align left',
30 aligncenter: 'Align center',
31 alignright: 'Align right',
32 alignnone: 'No alignment'
33 }, function( tooltip, name ) {
34 var direction = name.slice( 5 );
36 editor.addButton( 'wp_img_' + name, {
38 icon: 'dashicon dashicons-align-' + direction,
39 cmd: 'alignnone' === name ? 'wpAlignNone' : 'Justify' + direction.slice( 0, 1 ).toUpperCase() + direction.slice( 1 ),
40 onPostRender: function() {
43 editor.on( 'NodeChange', function( event ) {
47 if ( event.element.nodeName !== 'IMG' ) {
51 node = editor.dom.getParent( event.element, '.wp-caption' ) || event.element;
53 if ( 'alignnone' === name ) {
54 self.active( ! /\balign(left|center|right)\b/.test( node.className ) );
56 self.active( editor.dom.hasClass( node, name ) );
63 editor.once( 'preinit', function() {
64 if ( editor.wp && editor.wp._createToolbar ) {
65 toolbar = editor.wp._createToolbar( [
76 editor.on( 'wptoolbar', function( event ) {
77 if ( event.element.nodeName === 'IMG' && ! isPlaceholder( event.element ) ) {
78 event.toolbar = toolbar;
82 function isNonEditable( node ) {
83 var parent = editor.$( node ).parents( '[contenteditable]' );
84 return parent && parent.attr( 'contenteditable' ) === 'false';
87 // Safari on iOS fails to select images in contentEditoble mode on touch.
90 editor.on( 'init', function() {
91 editor.on( 'touchstart', function( event ) {
92 if ( event.target.nodeName === 'IMG' && ! isNonEditable( event.target ) ) {
97 editor.dom.bind( editor.getDoc(), 'touchmove', function() {
101 editor.on( 'touchend', function( event ) {
102 if ( touchOnImage && event.target.nodeName === 'IMG' && ! isNonEditable( event.target ) ) {
103 var node = event.target;
105 touchOnImage = false;
107 window.setTimeout( function() {
108 editor.selection.select( node );
109 editor.nodeChanged();
111 } else if ( toolbar ) {
118 function parseShortcode( content ) {
119 return content.replace( /(?:<p>)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:<\/p>)?/g, function( a, b, c ) {
120 var id, align, classes, caption, img, width;
122 id = b.match( /id=['"]([^'"]*)['"] ?/ );
124 b = b.replace( id[0], '' );
127 align = b.match( /align=['"]([^'"]*)['"] ?/ );
129 b = b.replace( align[0], '' );
132 classes = b.match( /class=['"]([^'"]*)['"] ?/ );
134 b = b.replace( classes[0], '' );
137 width = b.match( /width=['"]([0-9]*)['"] ?/ );
139 b = b.replace( width[0], '' );
143 img = c.match( /((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)([\s\S]*)/i );
145 if ( img && img[2] ) {
146 caption = trim( img[2] );
147 img = trim( img[1] );
149 // old captions shortcode style
150 caption = trim( b ).replace( /caption=['"]/, '' ).replace( /['"]$/, '' );
154 id = ( id && id[1] ) ? id[1].replace( /[<>&]+/g, '' ) : '';
155 align = ( align && align[1] ) ? align[1] : 'alignnone';
156 classes = ( classes && classes[1] ) ? ' ' + classes[1].replace( /[<>&]+/g, '' ) : '';
158 if ( ! width && img ) {
159 width = img.match( /width=['"]([0-9]*)['"]/ );
162 if ( width && width[1] ) {
166 if ( ! width || ! caption ) {
170 width = parseInt( width, 10 );
171 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
175 return '<div class="mceTemp"><dl id="' + id + '" class="wp-caption ' + align + classes + '" style="width: ' + width + 'px">' +
176 '<dt class="wp-caption-dt">'+ img +'</dt><dd class="wp-caption-dd">'+ caption +'</dd></dl></div>';
180 function getShortcode( content ) {
181 return content.replace( /(?:<div [^>]+mceTemp[^>]+>)?\s*(<dl [^>]+wp-caption[^>]+>[\s\S]+?<\/dl>)\s*(?:<\/div>)?/g, function( all, dl ) {
184 if ( dl.indexOf('<img ') === -1 ) {
185 // Broken caption. The user managed to drag the image out?
186 // Try to return the caption text as a paragraph.
187 out = dl.match( /<dd [^>]+>([\s\S]+?)<\/dd>/i );
189 if ( out && out[1] ) {
190 return '<p>' + out[1] + '</p>';
196 out = dl.replace( /\s*<dl ([^>]+)>\s*<dt [^>]+>([\s\S]+?)<\/dt>\s*<dd [^>]+>([\s\S]*?)<\/dd>\s*<\/dl>\s*/gi, function( a, b, c, caption ) {
197 var id, classes, align, width;
199 width = c.match( /width="([0-9]*)"/ );
200 width = ( width && width[1] ) ? width[1] : '';
202 classes = b.match( /class="([^"]*)"/ );
203 classes = ( classes && classes[1] ) ? classes[1] : '';
204 align = classes.match( /align[a-z]+/i ) || 'alignnone';
206 if ( ! width || ! caption ) {
207 if ( 'alignnone' !== align[0] ) {
208 c = c.replace( /><img/, ' class="' + align[0] + '"><img' );
213 id = b.match( /id="([^"]*)"/ );
214 id = ( id && id[1] ) ? id[1] : '';
216 classes = classes.replace( /wp-caption ?|align[a-z]+ ?/gi, '' );
219 classes = ' class="' + classes + '"';
222 caption = caption.replace( /\r\n|\r/g, '\n' ).replace( /<[a-zA-Z0-9]+( [^<>]+)?>/g, function( a ) {
223 // no line breaks inside HTML tags
224 return a.replace( /[\r\n\t]+/, ' ' );
227 // convert remaining line breaks to <br>
228 caption = caption.replace( /\s*\n\s*/g, '<br />' );
230 return '[caption id="' + id + '" align="' + align + '" width="' + width + '"' + classes + ']' + c + ' ' + caption + '[/caption]';
233 if ( out.indexOf('[caption') === -1 ) {
234 // the caption html seems broken, try to find the image that may be wrapped in a link
235 // and may be followed by <p> with the caption text.
236 out = dl.replace( /[\s\S]*?((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)(<p>[\s\S]*<\/p>)?[\s\S]*/gi, '<p>$1</p>$2' );
243 function extractImageData( imageNode ) {
244 var classes, extraClasses, metadata, captionBlock, caption, link, width, height,
245 captionClassName = [],
247 isIntRegExp = /^\d+$/;
249 // default attributes
251 attachment_id: false,
259 linkTargetBlank: false,
264 metadata.url = dom.getAttrib( imageNode, 'src' );
265 metadata.alt = dom.getAttrib( imageNode, 'alt' );
266 metadata.title = dom.getAttrib( imageNode, 'title' );
268 width = dom.getAttrib( imageNode, 'width' );
269 height = dom.getAttrib( imageNode, 'height' );
271 if ( ! isIntRegExp.test( width ) || parseInt( width, 10 ) < 1 ) {
272 width = imageNode.naturalWidth || imageNode.width;
275 if ( ! isIntRegExp.test( height ) || parseInt( height, 10 ) < 1 ) {
276 height = imageNode.naturalHeight || imageNode.height;
279 metadata.customWidth = metadata.width = width;
280 metadata.customHeight = metadata.height = height;
282 classes = tinymce.explode( imageNode.className, ' ' );
285 tinymce.each( classes, function( name ) {
287 if ( /^wp-image/.test( name ) ) {
288 metadata.attachment_id = parseInt( name.replace( 'wp-image-', '' ), 10 );
289 } else if ( /^align/.test( name ) ) {
290 metadata.align = name.replace( 'align', '' );
291 } else if ( /^size/.test( name ) ) {
292 metadata.size = name.replace( 'size-', '' );
294 extraClasses.push( name );
299 metadata.extraClasses = extraClasses.join( ' ' );
302 captionBlock = dom.getParents( imageNode, '.wp-caption' );
304 if ( captionBlock.length ) {
305 captionBlock = captionBlock[0];
307 classes = captionBlock.className.split( ' ' );
308 tinymce.each( classes, function( name ) {
309 if ( /^align/.test( name ) ) {
310 metadata.align = name.replace( 'align', '' );
311 } else if ( name && name !== 'wp-caption' ) {
312 captionClassName.push( name );
316 metadata.captionClassName = captionClassName.join( ' ' );
318 caption = dom.select( 'dd.wp-caption-dd', captionBlock );
319 if ( caption.length ) {
320 caption = caption[0];
322 metadata.caption = editor.serializer.serialize( caption )
323 .replace( /<br[^>]*>/g, '$&\n' ).replace( /^<p>/, '' ).replace( /<\/p>$/, '' );
328 if ( imageNode.parentNode && imageNode.parentNode.nodeName === 'A' ) {
329 link = imageNode.parentNode;
330 metadata.linkUrl = dom.getAttrib( link, 'href' );
331 metadata.linkTargetBlank = dom.getAttrib( link, 'target' ) === '_blank' ? true : false;
332 metadata.linkRel = dom.getAttrib( link, 'rel' );
333 metadata.linkClassName = link.className;
339 function hasTextContent( node ) {
340 return node && !! ( node.textContent || node.innerText );
343 // Verify HTML in captions
344 function verifyHTML( caption ) {
345 if ( ! caption || ( caption.indexOf( '<' ) === -1 && caption.indexOf( '>' ) === -1 ) ) {
349 if ( ! serializer ) {
350 serializer = new tinymce.html.Serializer( {}, editor.schema );
353 return serializer.serialize( editor.parser.parse( caption, { forced_root_block: false } ) );
356 function updateImage( imageNode, imageData ) {
357 var classes, className, node, html, parent, wrap, linkNode,
358 captionNode, dd, dl, id, attrs, linkAttrs, width, height, align,
359 $imageNode, srcset, src,
362 classes = tinymce.explode( imageData.extraClasses, ' ' );
368 if ( ! imageData.caption ) {
369 classes.push( 'align' + imageData.align );
372 if ( imageData.attachment_id ) {
373 classes.push( 'wp-image-' + imageData.attachment_id );
374 if ( imageData.size && imageData.size !== 'custom' ) {
375 classes.push( 'size-' + imageData.size );
379 width = imageData.width;
380 height = imageData.height;
382 if ( imageData.size === 'custom' ) {
383 width = imageData.customWidth;
384 height = imageData.customHeight;
389 width: width || null,
390 height: height || null,
392 title: imageData.title || null,
393 'class': classes.join( ' ' ) || null
396 dom.setAttribs( imageNode, attrs );
399 href: imageData.linkUrl,
400 rel: imageData.linkRel || null,
401 target: imageData.linkTargetBlank ? '_blank': null,
402 'class': imageData.linkClassName || null
405 if ( imageNode.parentNode && imageNode.parentNode.nodeName === 'A' && ! hasTextContent( imageNode.parentNode ) ) {
406 // Update or remove an existing link wrapped around the image
407 if ( imageData.linkUrl ) {
408 dom.setAttribs( imageNode.parentNode, linkAttrs );
410 dom.remove( imageNode.parentNode, true );
412 } else if ( imageData.linkUrl ) {
413 if ( linkNode = dom.getParent( imageNode, 'a' ) ) {
414 // The image is inside a link together with other nodes,
415 // or is nested in another node, move it out
416 dom.insertAfter( imageNode, linkNode );
419 // Add link wrapped around the image
420 linkNode = dom.create( 'a', linkAttrs );
421 imageNode.parentNode.insertBefore( linkNode, imageNode );
422 linkNode.appendChild( imageNode );
425 captionNode = editor.dom.getParent( imageNode, '.mceTemp' );
427 if ( imageNode.parentNode && imageNode.parentNode.nodeName === 'A' && ! hasTextContent( imageNode.parentNode ) ) {
428 node = imageNode.parentNode;
433 if ( imageData.caption ) {
434 imageData.caption = verifyHTML( imageData.caption );
436 id = imageData.attachment_id ? 'attachment_' + imageData.attachment_id : null;
437 align = 'align' + ( imageData.align || 'none' );
438 className = 'wp-caption ' + align;
440 if ( imageData.captionClassName ) {
441 className += ' ' + imageData.captionClassName.replace( /[<>&]+/g, '' );
444 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
445 width = parseInt( width, 10 );
450 dl = dom.select( 'dl.wp-caption', captionNode );
453 dom.setAttribs( dl, {
456 style: 'width: ' + width + 'px'
460 dd = dom.select( '.wp-caption-dd', captionNode );
463 dom.setHTML( dd[0], imageData.caption );
467 id = id ? 'id="'+ id +'" ' : '';
469 // should create a new function for generating the caption markup
470 html = '<dl ' + id + 'class="' + className +'" style="width: '+ width +'px">' +
471 '<dt class="wp-caption-dt"></dt><dd class="wp-caption-dd">'+ imageData.caption +'</dd></dl>';
473 wrap = dom.create( 'div', { 'class': 'mceTemp' }, html );
475 if ( parent = dom.getParent( node, 'p' ) ) {
476 parent.parentNode.insertBefore( wrap, parent );
478 node.parentNode.insertBefore( wrap, node );
481 editor.$( wrap ).find( 'dt.wp-caption-dt' ).append( node );
483 if ( parent && dom.isEmpty( parent ) ) {
484 dom.remove( parent );
487 } else if ( captionNode ) {
488 // Remove the caption wrapper and place the image in new paragraph
489 parent = dom.create( 'p' );
490 captionNode.parentNode.insertBefore( parent, captionNode );
491 parent.appendChild( node );
492 dom.remove( captionNode );
495 $imageNode = editor.$( imageNode );
496 srcset = $imageNode.attr( 'srcset' );
497 src = $imageNode.attr( 'src' );
499 // Remove srcset and sizes if the image file was edited or the image was replaced.
500 if ( srcset && src ) {
501 src = src.replace( /[?#].*/, '' );
503 if ( srcset.indexOf( src ) === -1 ) {
504 $imageNode.attr( 'srcset', null ).attr( 'sizes', null );
508 if ( wp.media.events ) {
509 wp.media.events.trigger( 'editor:image-update', {
516 editor.nodeChanged();
519 function editImage( img ) {
520 var frame, callback, metadata;
522 if ( typeof wp === 'undefined' || ! wp.media ) {
523 editor.execCommand( 'mceImage' );
527 metadata = extractImageData( img );
529 // Manipulate the metadata by reference that is fed into the PostImage model used in the media modal
530 wp.media.events.trigger( 'editor:image-edit', {
538 state: 'image-details',
542 wp.media.events.trigger( 'editor:frame-create', { frame: frame } );
544 callback = function( imageData ) {
546 editor.undoManager.transact( function() {
547 updateImage( img, imageData );
552 frame.state('image-details').on( 'update', callback );
553 frame.state('replace-image').on( 'replace', callback );
554 frame.on( 'close', function() {
562 function removeImage( node ) {
563 var wrap = editor.dom.getParent( node, 'div.mceTemp' );
565 if ( ! wrap && node.nodeName === 'IMG' ) {
566 wrap = editor.dom.getParent( node, 'a' );
570 if ( wrap.nextSibling ) {
571 editor.selection.select( wrap.nextSibling );
572 } else if ( wrap.previousSibling ) {
573 editor.selection.select( wrap.previousSibling );
575 editor.selection.select( wrap.parentNode );
578 editor.selection.collapse( true );
579 editor.dom.remove( wrap );
581 editor.dom.remove( node );
584 editor.nodeChanged();
585 editor.undoManager.add();
588 editor.on( 'init', function() {
589 var dom = editor.dom,
590 captionClass = editor.getParam( 'wpeditimage_html5_captions' ) ? 'html5-captions' : 'html4-captions';
592 dom.addClass( editor.getBody(), captionClass );
594 // Add caption field to the default image dialog
595 editor.on( 'wpLoadImageForm', function( event ) {
596 if ( editor.getParam( 'wpeditimage_disable_captions' ) ) {
607 label: 'Image caption'
610 event.data.splice( event.data.length - 1, 0, captionField );
613 // Fix caption parent width for images added from URL
614 editor.on( 'wpNewImageRefresh', function( event ) {
615 var parent, captionWidth;
617 if ( parent = dom.getParent( event.node, 'dl.wp-caption' ) ) {
618 if ( ! parent.style.width ) {
619 captionWidth = parseInt( event.node.clientWidth, 10 ) + 10;
620 captionWidth = captionWidth ? captionWidth + 'px' : '50%';
621 dom.setStyle( parent, 'width', captionWidth );
626 editor.on( 'wpImageFormSubmit', function( event ) {
627 var data = event.imgData.data,
628 imgNode = event.imgData.node,
629 caption = event.imgData.wpcaption,
633 wrap, parent, node, html, imgId;
635 // Temp image id so we can find the node later
636 data.id = '__wp-temp-img-id';
637 // Cancel the original callback
638 event.imgData.cancel = true;
640 if ( ! data.style ) {
645 // Delete the image and the caption
647 if ( wrap = dom.getParent( imgNode, 'div.mceTemp' ) ) {
649 } else if ( imgNode.parentNode.nodeName === 'A' ) {
650 dom.remove( imgNode.parentNode );
652 dom.remove( imgNode );
655 editor.nodeChanged();
661 caption = caption.replace( /\r\n|\r/g, '\n' ).replace( /<\/?[a-zA-Z0-9]+( [^<>]+)?>/g, function( a ) {
662 // No line breaks inside HTML tags
663 return a.replace( /[\r\n\t]+/, ' ' );
666 // Convert remaining line breaks to <br>
667 caption = caption.replace( /(<br[^>]*>)\s*\n\s*/g, '$1' ).replace( /\s*\n\s*/g, '<br />' );
668 caption = verifyHTML( caption );
672 // New image inserted
673 html = dom.createHTML( 'img', data );
676 node = editor.selection.getNode();
679 captionWidth = parseInt( data.width, 10 );
681 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
685 captionWidth = ' style="width: ' + captionWidth + 'px"';
688 html = '<dl class="wp-caption alignnone"' + captionWidth + '>' +
689 '<dt class="wp-caption-dt">'+ html +'</dt><dd class="wp-caption-dd">'+ caption +'</dd></dl>';
691 if ( node.nodeName === 'P' ) {
694 parent = dom.getParent( node, 'p' );
697 if ( parent && parent.nodeName === 'P' ) {
698 wrap = dom.create( 'div', { 'class': 'mceTemp' }, html );
699 parent.parentNode.insertBefore( wrap, parent );
700 editor.selection.select( wrap );
701 editor.nodeChanged();
703 if ( dom.isEmpty( parent ) ) {
704 dom.remove( parent );
707 editor.selection.setContent( '<div class="mceTemp">' + html + '</div>' );
710 editor.selection.setContent( html );
713 // Edit existing image
715 // Store the original image id if any
716 imgId = imgNode.id || null;
717 // Update the image node
718 dom.setAttribs( imgNode, data );
719 wrap = dom.getParent( imgNode, 'dl.wp-caption' );
723 if ( parent = dom.select( 'dd.wp-caption-dd', wrap )[0] ) {
724 parent.innerHTML = caption;
727 if ( imgNode.className ) {
728 captionId = imgNode.className.match( /wp-image-([0-9]+)/ );
729 captionAlign = imgNode.className.match( /align(left|right|center|none)/ );
732 if ( captionAlign ) {
733 captionAlign = captionAlign[0];
734 imgNode.className = imgNode.className.replace( /align(left|right|center|none)/g, '' );
736 captionAlign = 'alignnone';
739 captionAlign = ' class="wp-caption ' + captionAlign + '"';
742 captionId = ' id="attachment_' + captionId[1] + '"';
745 captionWidth = data.width || imgNode.clientWidth;
747 if ( captionWidth ) {
748 captionWidth = parseInt( captionWidth, 10 );
750 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
754 captionWidth = ' style="width: '+ captionWidth +'px"';
757 if ( imgNode.parentNode && imgNode.parentNode.nodeName === 'A' ) {
758 node = imgNode.parentNode;
763 html = '<dl ' + captionId + captionAlign + captionWidth + '>' +
764 '<dt class="wp-caption-dt"></dt><dd class="wp-caption-dd">'+ caption +'</dd></dl>';
766 wrap = dom.create( 'div', { 'class': 'mceTemp' }, html );
768 if ( parent = dom.getParent( node, 'p' ) ) {
769 parent.parentNode.insertBefore( wrap, parent );
771 node.parentNode.insertBefore( wrap, node );
774 editor.$( wrap ).find( 'dt.wp-caption-dt' ).append( node );
776 if ( parent && dom.isEmpty( parent ) ) {
777 dom.remove( parent );
782 // Remove the caption wrapper and place the image in new paragraph
783 if ( imgNode.parentNode.nodeName === 'A' ) {
784 html = dom.getOuterHTML( imgNode.parentNode );
786 html = dom.getOuterHTML( imgNode );
789 parent = dom.create( 'p', {}, html );
790 dom.insertAfter( parent, wrap.parentNode );
791 editor.selection.select( parent );
792 editor.nodeChanged();
793 dom.remove( wrap.parentNode );
798 imgNode = dom.get('__wp-temp-img-id');
799 dom.setAttrib( imgNode, 'id', imgId );
800 event.imgData.node = imgNode;
803 editor.on( 'wpLoadImageData', function( event ) {
805 data = event.imgData.data,
806 imgNode = event.imgData.node;
808 if ( parent = dom.getParent( imgNode, 'dl.wp-caption' ) ) {
809 parent = dom.select( 'dd.wp-caption-dd', parent )[0];
812 data.wpcaption = editor.serializer.serialize( parent )
813 .replace( /<br[^>]*>/g, '$&\n' ).replace( /^<p>/, '' ).replace( /<\/p>$/, '' );
818 // Prevent IE11 from making dl.wp-caption resizable
819 if ( tinymce.Env.ie && tinymce.Env.ie > 10 ) {
820 // The 'mscontrolselect' event is supported only in IE11+
821 dom.bind( editor.getBody(), 'mscontrolselect', function( event ) {
822 if ( event.target.nodeName === 'IMG' && dom.getParent( event.target, '.wp-caption' ) ) {
823 // Hide the thick border with resize handles around dl.wp-caption
824 editor.getBody().focus(); // :(
825 } else if ( event.target.nodeName === 'DL' && dom.hasClass( event.target, 'wp-caption' ) ) {
826 // Trigger the thick border with resize handles...
827 // This will make the caption text editable.
828 event.target.focus();
834 editor.on( 'ObjectResized', function( event ) {
835 var node = event.target;
837 if ( node.nodeName === 'IMG' ) {
838 editor.undoManager.transact( function() {
842 node.className = node.className.replace( /\bsize-[^ ]+/, '' );
844 if ( parent = dom.getParent( node, '.wp-caption' ) ) {
845 width = event.width || dom.getAttrib( node, 'width' );
848 width = parseInt( width, 10 );
850 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
854 dom.setStyle( parent, 'width', width + 'px' );
861 editor.on( 'BeforeExecCommand', function( event ) {
862 var node, p, DL, align, replacement,
866 if ( cmd === 'mceInsertContent' ) {
867 // When inserting content, if the caret is inside a caption create new paragraph under
868 // and move the caret there
869 if ( node = dom.getParent( editor.selection.getNode(), 'div.mceTemp' ) ) {
870 p = dom.create( 'p' );
871 dom.insertAfter( p, node );
872 editor.selection.setCursorLocation( p, 0 );
873 editor.nodeChanged();
875 } else if ( cmd === 'JustifyLeft' || cmd === 'JustifyRight' || cmd === 'JustifyCenter' || cmd === 'wpAlignNone' ) {
876 node = editor.selection.getNode();
877 align = 'align' + cmd.slice( 7 ).toLowerCase();
878 DL = editor.dom.getParent( node, '.wp-caption' );
880 if ( node.nodeName !== 'IMG' && ! DL ) {
886 if ( editor.dom.hasClass( node, align ) ) {
887 replacement = ' alignnone';
889 replacement = ' ' + align;
892 node.className = trim( node.className.replace( / ?align(left|center|right|none)/g, '' ) + replacement );
894 editor.nodeChanged();
895 event.preventDefault();
898 toolbar.reposition();
901 editor.fire( 'ExecCommand', {
909 editor.on( 'keydown', function( event ) {
910 var node, wrap, P, spacer,
911 selection = editor.selection,
912 keyCode = event.keyCode,
914 VK = tinymce.util.VK;
916 if ( keyCode === VK.ENTER ) {
917 // When pressing Enter inside a caption move the caret to a new parapraph under it
918 node = selection.getNode();
919 wrap = dom.getParent( node, 'div.mceTemp' );
922 dom.events.cancel( event ); // Doesn't cancel all :(
924 // Remove any extra dt and dd cleated on pressing Enter...
925 tinymce.each( dom.select( 'dt, dd', wrap ), function( element ) {
926 if ( dom.isEmpty( element ) ) {
927 dom.remove( element );
931 spacer = tinymce.Env.ie && tinymce.Env.ie < 11 ? '' : '<br data-mce-bogus="1" />';
932 P = dom.create( 'p', null, spacer );
934 if ( node.nodeName === 'DD' ) {
935 dom.insertAfter( P, wrap );
937 wrap.parentNode.insertBefore( P, wrap );
940 editor.nodeChanged();
941 selection.setCursorLocation( P, 0 );
943 } else if ( keyCode === VK.DELETE || keyCode === VK.BACKSPACE ) {
944 node = selection.getNode();
946 if ( node.nodeName === 'DIV' && dom.hasClass( node, 'mceTemp' ) ) {
948 } else if ( node.nodeName === 'IMG' || node.nodeName === 'DT' || node.nodeName === 'A' ) {
949 wrap = dom.getParent( node, 'div.mceTemp' );
953 dom.events.cancel( event );
960 // After undo/redo FF seems to set the image height very slowly when it is set to 'auto' in the CSS.
961 // This causes image.getBoundingClientRect() to return wrong values and the resize handles are shown in wrong places.
962 // Collapse the selection to remove the resize handles.
963 if ( tinymce.Env.gecko ) {
964 editor.on( 'undo redo', function() {
965 if ( editor.selection.getNode().nodeName === 'IMG' ) {
966 editor.selection.collapse();
971 editor.wpSetImgCaption = function( content ) {
972 return parseShortcode( content );
975 editor.wpGetImgCaption = function( content ) {
976 return getShortcode( content );
979 editor.on( 'BeforeSetContent', function( event ) {
980 if ( event.format !== 'raw' ) {
981 event.content = editor.wpSetImgCaption( event.content );
985 editor.on( 'PostProcess', function( event ) {
987 event.content = editor.wpGetImgCaption( event.content );
994 editor.on( 'dragstart', function() {
995 var node = editor.selection.getNode();
997 if ( node.nodeName === 'IMG' ) {
998 wrap = editor.dom.getParent( node, '.mceTemp' );
1000 if ( ! wrap && node.parentNode.nodeName === 'A' && ! hasTextContent( node.parentNode ) ) {
1001 wrap = node.parentNode;
1006 editor.on( 'drop', function( event ) {
1007 var dom = editor.dom,
1008 rng = tinymce.dom.RangeUtils.getCaretRangeFromPoint( event.clientX, event.clientY, editor.getDoc() );
1010 // Don't allow anything to be dropped in a captioned image.
1011 if ( rng && dom.getParent( rng.startContainer, '.mceTemp' ) ) {
1012 event.preventDefault();
1013 } else if ( wrap ) {
1014 event.preventDefault();
1016 editor.undoManager.transact( function() {
1018 editor.selection.setRng( rng );
1021 editor.selection.setNode( wrap );
1031 editor.wp = editor.wp || {};
1032 editor.wp.isPlaceholder = isPlaceholder;
1036 _do_shcode: parseShortcode,
1037 _get_shcode: getShortcode