2 tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
3 var floatingToolbar, serializer,
5 settings = editor.settings,
6 Factory = tinymce.ui.Factory,
9 toolbarIsHidden = true,
10 editorWrapParent = tinymce.$( '#postdivrich' );
12 function isPlaceholder( node ) {
13 return !! ( editor.dom.getAttrib( node, 'data-mce-placeholder' ) || editor.dom.getAttrib( node, 'data-mce-object' ) );
16 editor.addButton( 'wp_img_remove', {
18 icon: 'dashicon dashicons-no',
20 removeImage( editor.selection.getNode() );
24 editor.addButton( 'wp_img_edit', {
25 tooltip: 'Edit ', // trailing space is needed, used for context
26 icon: 'dashicon dashicons-edit',
28 editImage( editor.selection.getNode() );
33 alignleft: 'Align left',
34 aligncenter: 'Align center',
35 alignright: 'Align right',
36 alignnone: 'No alignment'
37 }, function( tooltip, name ) {
38 var direction = name.slice( 5 );
40 editor.addButton( 'wp_img_' + name, {
42 icon: 'dashicon dashicons-align-' + direction,
43 cmd: 'alignnone' === name ? 'wpAlignNone' : 'Justify' + direction.slice( 0, 1 ).toUpperCase() + direction.slice( 1 ),
44 onPostRender: function() {
47 editor.on( 'NodeChange', function( event ) {
51 if ( event.element.nodeName !== 'IMG' ) {
55 node = editor.dom.getParent( event.element, '.wp-caption' ) || event.element;
57 if ( 'alignnone' === name ) {
58 self.active( ! /\balign(left|center|right)\b/.test( node.className ) );
60 self.active( editor.dom.hasClass( node, name ) );
67 function toolbarConfig() {
68 var toolbarItems = [],
71 each( [ 'wp_img_alignleft', 'wp_img_aligncenter', 'wp_img_alignright', 'wp_img_alignnone', 'wp_img_edit', 'wp_img_remove' ], function( item ) {
74 function bindSelectorChanged() {
75 var selection = editor.selection;
77 if ( item.settings.stateSelector ) {
78 selection.selectorChanged( item.settings.stateSelector, function( state ) {
83 if ( item.settings.disabledStateSelector ) {
84 selection.selectorChanged( item.settings.disabledStateSelector, function( state ) {
85 item.disabled( state );
93 if ( Factory.has( item ) ) {
98 if ( settings.toolbar_items_size ) {
99 item.size = settings.toolbar_items_size;
102 toolbarItems.push( item );
106 if ( ! buttonGroup ) {
112 toolbarItems.push( buttonGroup );
115 if ( editor.buttons[ item ] ) {
117 item = editor.buttons[ itemName ];
119 if ( typeof item === 'function' ) {
123 item.type = item.type || 'button';
125 if ( settings.toolbar_items_size ) {
126 item.size = settings.toolbar_items_size;
129 item = Factory.create( item );
130 buttonGroup.items.push( item );
132 if ( editor.initialized ) {
133 bindSelectorChanged();
135 editor.on( 'init', bindSelectorChanged );
145 classes: 'toolbar-grp inline-toolbar-grp wp-image-toolbar',
158 floatingToolbar = Factory.create( toolbarConfig() ).renderTo( document.body ).hide();
160 floatingToolbar.reposition = function() {
161 var top, left, minTop, className,
162 windowPos, adminbar, mceToolbar, boundary,
163 boundaryMiddle, boundaryVerticalMiddle, spaceTop,
164 spaceBottom, windowWidth, toolbarWidth, toolbarHalf,
165 iframe, iframePos, iframeWidth, iframeHeigth,
166 toolbarNodeHeight, verticalSpaceNeeded,
167 toolbarNode = this.getEl(),
171 imageNode = editor.selection.getNode();
173 if ( ! imageNode || imageNode.nodeName !== 'IMG' ) {
177 windowPos = window.pageYOffset || document.documentElement.scrollTop;
178 adminbar = tinymce.$( '#wpadminbar' )[0];
179 mceToolbar = tinymce.$( '.mce-tinymce .mce-toolbar-grp' )[0];
180 boundary = imageNode.getBoundingClientRect();
181 boundaryMiddle = ( boundary.left + boundary.right ) / 2;
182 boundaryVerticalMiddle = ( boundary.top + boundary.bottom ) / 2;
183 spaceTop = boundary.top;
184 spaceBottom = iframeHeigth - boundary.bottom;
185 windowWidth = window.innerWidth;
186 toolbarWidth = toolbarNode.offsetWidth;
187 toolbarHalf = toolbarWidth / 2;
188 iframe = document.getElementById( editor.id + '_ifr' );
189 iframePos = DOM.getPos( iframe );
190 iframeWidth = iframe.offsetWidth;
191 iframeHeigth = iframe.offsetHeight;
192 toolbarNodeHeight = toolbarNode.offsetHeight;
193 verticalSpaceNeeded = toolbarNodeHeight + margin + buffer;
196 top = boundary.top + iframePos.y + margin;
198 if ( spaceTop >= verticalSpaceNeeded ) {
199 className = ' mce-arrow-down';
200 top = boundary.top + iframePos.y - toolbarNodeHeight - margin;
201 } else if ( spaceBottom >= verticalSpaceNeeded ) {
202 className = ' mce-arrow-up';
203 top = boundary.bottom + iframePos.y;
207 if ( boundaryVerticalMiddle >= verticalSpaceNeeded ) {
208 className = ' mce-arrow-down';
210 className = ' mce-arrow-up';
215 // Make sure the image toolbar is below the main toolbar.
217 minTop = DOM.getPos( mceToolbar ).y + mceToolbar.clientHeight;
219 minTop = iframePos.y;
222 // Make sure the image toolbar is below the adminbar (if visible) or below the top of the window.
224 if ( adminbar && adminbar.getBoundingClientRect().top === 0 ) {
225 adminbarHeight = adminbar.clientHeight;
228 if ( windowPos + adminbarHeight > minTop ) {
229 minTop = windowPos + adminbarHeight;
233 if ( top && minTop && ( minTop + buffer > top ) ) {
234 top = minTop + buffer;
238 left = boundaryMiddle - toolbarHalf;
241 if ( boundary.left < 0 || boundary.right > iframeWidth ) {
242 left = iframePos.x + ( iframeWidth - toolbarWidth ) / 2;
243 } else if ( toolbarWidth >= windowWidth ) {
244 className += ' mce-arrow-full';
246 } else if ( ( left < 0 && boundary.left + toolbarWidth > windowWidth ) ||
247 ( left + toolbarWidth > windowWidth && boundary.right - toolbarWidth < 0 ) ) {
249 left = ( windowWidth - toolbarWidth ) / 2;
250 } else if ( left < iframePos.x ) {
251 className += ' mce-arrow-left';
252 left = boundary.left + iframePos.x;
253 } else if ( left + toolbarWidth > iframeWidth + iframePos.x ) {
254 className += ' mce-arrow-right';
255 left = boundary.right - toolbarWidth + iframePos.x;
259 toolbarNode.className = toolbarNode.className.replace( / ?mce-arrow-[\w]+/g, '' );
260 toolbarNode.className += className;
263 DOM.setStyles( toolbarNode, { 'left': left, 'top': top } );
269 // Safari on iOS fails to select image nodes in contentEditoble mode on touch/click.
270 // Select them again.
271 editor.on( 'click', function( event ) {
272 if ( event.target.nodeName === 'IMG' ) {
273 var node = event.target;
275 window.setTimeout( function() {
276 editor.selection.select( node );
279 floatingToolbar.hide();
284 editor.on( 'nodechange', function( event ) {
285 var delay = iOS ? 350 : 100;
287 if ( event.element.nodeName !== 'IMG' || isPlaceholder( event.element ) ) {
288 floatingToolbar.hide();
292 setTimeout( function() {
293 var element = editor.selection.getNode();
295 if ( element.nodeName === 'IMG' && ! isPlaceholder( element ) ) {
296 if ( floatingToolbar._visible ) {
297 floatingToolbar.reposition();
299 floatingToolbar.show();
302 floatingToolbar.hide();
308 if ( ! toolbarIsHidden ) {
309 floatingToolbar.hide();
313 floatingToolbar.on( 'show', function() {
314 toolbarIsHidden = false;
316 if ( this._visible ) {
318 DOM.addClass( this.getEl(), 'mce-inline-toolbar-grp-active' );
322 floatingToolbar.on( 'hide', function() {
323 toolbarIsHidden = true;
324 DOM.removeClass( this.getEl(), 'mce-inline-toolbar-grp-active' );
327 floatingToolbar.on( 'keydown', function( event ) {
328 if ( event.keyCode === 27 ) {
334 DOM.bind( window, 'resize scroll', function() {
335 if ( ! toolbarIsHidden && editorWrapParent.hasClass( 'wp-editor-expand' ) ) {
340 editor.on( 'init', function() {
341 editor.dom.bind( editor.getWin(), 'scroll', hide );
344 editor.on( 'blur hide', hide );
347 editor.shortcuts.add( 'Alt+119', '', function() {
348 var node = floatingToolbar.find( 'toolbar' )[0];
355 function parseShortcode( content ) {
356 return content.replace( /(?:<p>)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:<\/p>)?/g, function( a, b, c ) {
357 var id, align, classes, caption, img, width,
360 id = b.match( /id=['"]([^'"]*)['"] ?/ );
362 b = b.replace( id[0], '' );
365 align = b.match( /align=['"]([^'"]*)['"] ?/ );
367 b = b.replace( align[0], '' );
370 classes = b.match( /class=['"]([^'"]*)['"] ?/ );
372 b = b.replace( classes[0], '' );
375 width = b.match( /width=['"]([0-9]*)['"] ?/ );
377 b = b.replace( width[0], '' );
381 img = c.match( /((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)([\s\S]*)/i );
383 if ( img && img[2] ) {
384 caption = trim( img[2] );
385 img = trim( img[1] );
387 // old captions shortcode style
388 caption = trim( b ).replace( /caption=['"]/, '' ).replace( /['"]$/, '' );
392 id = ( id && id[1] ) ? id[1].replace( /[<>&]+/g, '' ) : '';
393 align = ( align && align[1] ) ? align[1] : 'alignnone';
394 classes = ( classes && classes[1] ) ? ' ' + classes[1].replace( /[<>&]+/g, '' ) : '';
396 if ( ! width && img ) {
397 width = img.match( /width=['"]([0-9]*)['"]/ );
400 if ( width && width[1] ) {
404 if ( ! width || ! caption ) {
408 width = parseInt( width, 10 );
409 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
413 return '<div class="mceTemp"><dl id="' + id + '" class="wp-caption ' + align + classes + '" style="width: ' + width + 'px">' +
414 '<dt class="wp-caption-dt">'+ img +'</dt><dd class="wp-caption-dd">'+ caption +'</dd></dl></div>';
418 function getShortcode( content ) {
419 return content.replace( /<div (?:id="attachment_|class="mceTemp)[^>]*>([\s\S]+?)<\/div>/g, function( a, b ) {
422 if ( b.indexOf('<img ') === -1 ) {
423 // Broken caption. The user managed to drag the image out?
424 // Try to return the caption text as a paragraph.
425 out = b.match( /<dd [^>]+>([\s\S]+?)<\/dd>/i );
427 if ( out && out[1] ) {
428 return '<p>' + out[1] + '</p>';
434 out = b.replace( /\s*<dl ([^>]+)>\s*<dt [^>]+>([\s\S]+?)<\/dt>\s*<dd [^>]+>([\s\S]*?)<\/dd>\s*<\/dl>\s*/gi, function( a, b, c, caption ) {
435 var id, classes, align, width;
437 width = c.match( /width="([0-9]*)"/ );
438 width = ( width && width[1] ) ? width[1] : '';
440 if ( ! width || ! caption ) {
444 id = b.match( /id="([^"]*)"/ );
445 id = ( id && id[1] ) ? id[1] : '';
447 classes = b.match( /class="([^"]*)"/ );
448 classes = ( classes && classes[1] ) ? classes[1] : '';
450 align = classes.match( /align[a-z]+/i ) || 'alignnone';
451 classes = classes.replace( /wp-caption ?|align[a-z]+ ?/gi, '' );
454 classes = ' class="' + classes + '"';
457 caption = caption.replace( /\r\n|\r/g, '\n' ).replace( /<[a-zA-Z0-9]+( [^<>]+)?>/g, function( a ) {
458 // no line breaks inside HTML tags
459 return a.replace( /[\r\n\t]+/, ' ' );
462 // convert remaining line breaks to <br>
463 caption = caption.replace( /\s*\n\s*/g, '<br />' );
465 return '[caption id="' + id + '" align="' + align + '" width="' + width + '"' + classes + ']' + c + ' ' + caption + '[/caption]';
468 if ( out.indexOf('[caption') === -1 ) {
469 // the caption html seems broken, try to find the image that may be wrapped in a link
470 // and may be followed by <p> with the caption text.
471 out = b.replace( /[\s\S]*?((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)(<p>[\s\S]*<\/p>)?[\s\S]*/gi, '<p>$1</p>$2' );
478 function extractImageData( imageNode ) {
479 var classes, extraClasses, metadata, captionBlock, caption, link, width, height,
480 captionClassName = [],
482 isIntRegExp = /^\d+$/;
484 // default attributes
486 attachment_id: false,
494 linkTargetBlank: false,
499 metadata.url = dom.getAttrib( imageNode, 'src' );
500 metadata.alt = dom.getAttrib( imageNode, 'alt' );
501 metadata.title = dom.getAttrib( imageNode, 'title' );
503 width = dom.getAttrib( imageNode, 'width' );
504 height = dom.getAttrib( imageNode, 'height' );
506 if ( ! isIntRegExp.test( width ) || parseInt( width, 10 ) < 1 ) {
507 width = imageNode.naturalWidth || imageNode.width;
510 if ( ! isIntRegExp.test( height ) || parseInt( height, 10 ) < 1 ) {
511 height = imageNode.naturalHeight || imageNode.height;
514 metadata.customWidth = metadata.width = width;
515 metadata.customHeight = metadata.height = height;
517 classes = tinymce.explode( imageNode.className, ' ' );
520 tinymce.each( classes, function( name ) {
522 if ( /^wp-image/.test( name ) ) {
523 metadata.attachment_id = parseInt( name.replace( 'wp-image-', '' ), 10 );
524 } else if ( /^align/.test( name ) ) {
525 metadata.align = name.replace( 'align', '' );
526 } else if ( /^size/.test( name ) ) {
527 metadata.size = name.replace( 'size-', '' );
529 extraClasses.push( name );
534 metadata.extraClasses = extraClasses.join( ' ' );
537 captionBlock = dom.getParents( imageNode, '.wp-caption' );
539 if ( captionBlock.length ) {
540 captionBlock = captionBlock[0];
542 classes = captionBlock.className.split( ' ' );
543 tinymce.each( classes, function( name ) {
544 if ( /^align/.test( name ) ) {
545 metadata.align = name.replace( 'align', '' );
546 } else if ( name && name !== 'wp-caption' ) {
547 captionClassName.push( name );
551 metadata.captionClassName = captionClassName.join( ' ' );
553 caption = dom.select( 'dd.wp-caption-dd', captionBlock );
554 if ( caption.length ) {
555 caption = caption[0];
557 metadata.caption = editor.serializer.serialize( caption )
558 .replace( /<br[^>]*>/g, '$&\n' ).replace( /^<p>/, '' ).replace( /<\/p>$/, '' );
563 if ( imageNode.parentNode && imageNode.parentNode.nodeName === 'A' ) {
564 link = imageNode.parentNode;
565 metadata.linkUrl = dom.getAttrib( link, 'href' );
566 metadata.linkTargetBlank = dom.getAttrib( link, 'target' ) === '_blank' ? true : false;
567 metadata.linkRel = dom.getAttrib( link, 'rel' );
568 metadata.linkClassName = link.className;
574 function hasTextContent( node ) {
575 return node && !! ( node.textContent || node.innerText );
578 // Verify HTML in captions
579 function verifyHTML( caption ) {
580 if ( ! caption || ( caption.indexOf( '<' ) === -1 && caption.indexOf( '>' ) === -1 ) ) {
584 if ( ! serializer ) {
585 serializer = new tinymce.html.Serializer( {}, editor.schema );
588 return serializer.serialize( editor.parser.parse( caption, { forced_root_block: false } ) );
591 function updateImage( imageNode, imageData ) {
592 var classes, className, node, html, parent, wrap, linkNode,
593 captionNode, dd, dl, id, attrs, linkAttrs, width, height, align,
596 classes = tinymce.explode( imageData.extraClasses, ' ' );
602 if ( ! imageData.caption ) {
603 classes.push( 'align' + imageData.align );
606 if ( imageData.attachment_id ) {
607 classes.push( 'wp-image-' + imageData.attachment_id );
608 if ( imageData.size && imageData.size !== 'custom' ) {
609 classes.push( 'size-' + imageData.size );
613 width = imageData.width;
614 height = imageData.height;
616 if ( imageData.size === 'custom' ) {
617 width = imageData.customWidth;
618 height = imageData.customHeight;
623 width: width || null,
624 height: height || null,
626 title: imageData.title || null,
627 'class': classes.join( ' ' ) || null
630 dom.setAttribs( imageNode, attrs );
633 href: imageData.linkUrl,
634 rel: imageData.linkRel || null,
635 target: imageData.linkTargetBlank ? '_blank': null,
636 'class': imageData.linkClassName || null
639 if ( imageNode.parentNode && imageNode.parentNode.nodeName === 'A' && ! hasTextContent( imageNode.parentNode ) ) {
640 // Update or remove an existing link wrapped around the image
641 if ( imageData.linkUrl ) {
642 dom.setAttribs( imageNode.parentNode, linkAttrs );
644 dom.remove( imageNode.parentNode, true );
646 } else if ( imageData.linkUrl ) {
647 if ( linkNode = dom.getParent( imageNode, 'a' ) ) {
648 // The image is inside a link together with other nodes,
649 // or is nested in another node, move it out
650 dom.insertAfter( imageNode, linkNode );
653 // Add link wrapped around the image
654 linkNode = dom.create( 'a', linkAttrs );
655 imageNode.parentNode.insertBefore( linkNode, imageNode );
656 linkNode.appendChild( imageNode );
659 captionNode = editor.dom.getParent( imageNode, '.mceTemp' );
661 if ( imageNode.parentNode && imageNode.parentNode.nodeName === 'A' && ! hasTextContent( imageNode.parentNode ) ) {
662 node = imageNode.parentNode;
667 if ( imageData.caption ) {
668 imageData.caption = verifyHTML( imageData.caption );
670 id = imageData.attachment_id ? 'attachment_' + imageData.attachment_id : null;
671 align = 'align' + ( imageData.align || 'none' );
672 className = 'wp-caption ' + align;
674 if ( imageData.captionClassName ) {
675 className += ' ' + imageData.captionClassName.replace( /[<>&]+/g, '' );
678 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
679 width = parseInt( width, 10 );
684 dl = dom.select( 'dl.wp-caption', captionNode );
687 dom.setAttribs( dl, {
690 style: 'width: ' + width + 'px'
694 dd = dom.select( '.wp-caption-dd', captionNode );
697 dom.setHTML( dd[0], imageData.caption );
701 id = id ? 'id="'+ id +'" ' : '';
703 // should create a new function for generating the caption markup
704 html = '<dl ' + id + 'class="' + className +'" style="width: '+ width +'px">' +
705 '<dt class="wp-caption-dt"></dt><dd class="wp-caption-dd">'+ imageData.caption +'</dd></dl>';
707 wrap = dom.create( 'div', { 'class': 'mceTemp' }, html );
709 if ( parent = dom.getParent( node, 'p' ) ) {
710 parent.parentNode.insertBefore( wrap, parent );
712 if ( dom.isEmpty( parent ) ) {
713 dom.remove( parent );
716 node.parentNode.insertBefore( wrap, node );
719 editor.$( wrap ).find( 'dt.wp-caption-dt' ).append( node );
721 } else if ( captionNode ) {
722 // Remove the caption wrapper and place the image in new paragraph
723 parent = dom.create( 'p' );
724 captionNode.parentNode.insertBefore( parent, captionNode );
725 parent.appendChild( node );
726 dom.remove( captionNode );
729 if ( wp.media.events ) {
730 wp.media.events.trigger( 'editor:image-update', {
737 editor.nodeChanged();
740 function editImage( img ) {
741 var frame, callback, metadata;
743 if ( typeof wp === 'undefined' || ! wp.media ) {
744 editor.execCommand( 'mceImage' );
748 metadata = extractImageData( img );
750 // Manipulate the metadata by reference that is fed into the PostImage model used in the media modal
751 wp.media.events.trigger( 'editor:image-edit', {
759 state: 'image-details',
763 wp.media.events.trigger( 'editor:frame-create', { frame: frame } );
765 callback = function( imageData ) {
767 editor.undoManager.transact( function() {
768 updateImage( img, imageData );
773 frame.state('image-details').on( 'update', callback );
774 frame.state('replace-image').on( 'replace', callback );
775 frame.on( 'close', function() {
783 function removeImage( node ) {
786 if ( node.nodeName === 'DIV' && editor.dom.hasClass( node, 'mceTemp' ) ) {
788 } else if ( node.nodeName === 'IMG' || node.nodeName === 'DT' || node.nodeName === 'A' ) {
789 wrap = editor.dom.getParent( node, 'div.mceTemp' );
793 if ( wrap.nextSibling ) {
794 editor.selection.select( wrap.nextSibling );
795 } else if ( wrap.previousSibling ) {
796 editor.selection.select( wrap.previousSibling );
798 editor.selection.select( wrap.parentNode );
801 editor.selection.collapse( true );
802 editor.dom.remove( wrap );
804 editor.dom.remove( node );
807 editor.nodeChanged();
808 editor.undoManager.add();
811 editor.on( 'init', function() {
812 var dom = editor.dom,
813 captionClass = editor.getParam( 'wpeditimage_html5_captions' ) ? 'html5-captions' : 'html4-captions';
815 dom.addClass( editor.getBody(), captionClass );
817 // Add caption field to the default image dialog
818 editor.on( 'wpLoadImageForm', function( event ) {
819 if ( editor.getParam( 'wpeditimage_disable_captions' ) ) {
830 label: 'Image caption'
833 event.data.splice( event.data.length - 1, 0, captionField );
836 // Fix caption parent width for images added from URL
837 editor.on( 'wpNewImageRefresh', function( event ) {
838 var parent, captionWidth;
840 if ( parent = dom.getParent( event.node, 'dl.wp-caption' ) ) {
841 if ( ! parent.style.width ) {
842 captionWidth = parseInt( event.node.clientWidth, 10 ) + 10;
843 captionWidth = captionWidth ? captionWidth + 'px' : '50%';
844 dom.setStyle( parent, 'width', captionWidth );
849 editor.on( 'wpImageFormSubmit', function( event ) {
850 var data = event.imgData.data,
851 imgNode = event.imgData.node,
852 caption = event.imgData.caption,
856 wrap, parent, node, html, imgId;
858 // Temp image id so we can find the node later
859 data.id = '__wp-temp-img-id';
860 // Cancel the original callback
861 event.imgData.cancel = true;
863 if ( ! data.style ) {
868 // Delete the image and the caption
870 if ( wrap = dom.getParent( imgNode, 'div.mceTemp' ) ) {
872 } else if ( imgNode.parentNode.nodeName === 'A' ) {
873 dom.remove( imgNode.parentNode );
875 dom.remove( imgNode );
878 editor.nodeChanged();
884 caption = caption.replace( /\r\n|\r/g, '\n' ).replace( /<\/?[a-zA-Z0-9]+( [^<>]+)?>/g, function( a ) {
885 // No line breaks inside HTML tags
886 return a.replace( /[\r\n\t]+/, ' ' );
889 // Convert remaining line breaks to <br>
890 caption = caption.replace( /(<br[^>]*>)\s*\n\s*/g, '$1' ).replace( /\s*\n\s*/g, '<br />' );
891 caption = verifyHTML( caption );
895 // New image inserted
896 html = dom.createHTML( 'img', data );
899 node = editor.selection.getNode();
902 captionWidth = parseInt( data.width, 10 );
904 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
908 captionWidth = ' style="width: ' + captionWidth + 'px"';
911 html = '<dl class="wp-caption alignnone"' + captionWidth + '>' +
912 '<dt class="wp-caption-dt">'+ html +'</dt><dd class="wp-caption-dd">'+ caption +'</dd></dl>';
914 if ( node.nodeName === 'P' ) {
917 parent = dom.getParent( node, 'p' );
920 if ( parent && parent.nodeName === 'P' ) {
921 wrap = dom.create( 'div', { 'class': 'mceTemp' }, html );
922 parent.parentNode.insertBefore( wrap, parent );
923 editor.selection.select( wrap );
924 editor.nodeChanged();
926 if ( dom.isEmpty( parent ) ) {
927 dom.remove( parent );
930 editor.selection.setContent( '<div class="mceTemp">' + html + '</div>' );
933 editor.selection.setContent( html );
936 // Edit existing image
938 // Store the original image id if any
939 imgId = imgNode.id || null;
940 // Update the image node
941 dom.setAttribs( imgNode, data );
942 wrap = dom.getParent( imgNode, 'dl.wp-caption' );
946 if ( parent = dom.select( 'dd.wp-caption-dd', wrap )[0] ) {
947 parent.innerHTML = caption;
950 if ( imgNode.className ) {
951 captionId = imgNode.className.match( /wp-image-([0-9]+)/ );
952 captionAlign = imgNode.className.match( /align(left|right|center|none)/ );
955 if ( captionAlign ) {
956 captionAlign = captionAlign[0];
957 imgNode.className = imgNode.className.replace( /align(left|right|center|none)/g, '' );
959 captionAlign = 'alignnone';
962 captionAlign = ' class="wp-caption ' + captionAlign + '"';
965 captionId = ' id="attachment_' + captionId[1] + '"';
968 captionWidth = data.width || imgNode.clientWidth;
970 if ( captionWidth ) {
971 captionWidth = parseInt( captionWidth, 10 );
973 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
977 captionWidth = ' style="width: '+ captionWidth +'px"';
980 if ( imgNode.parentNode && imgNode.parentNode.nodeName === 'A' ) {
981 node = imgNode.parentNode;
986 html = '<dl ' + captionId + captionAlign + captionWidth + '>' +
987 '<dt class="wp-caption-dt"></dt><dd class="wp-caption-dd">'+ caption +'</dd></dl>';
989 wrap = dom.create( 'div', { 'class': 'mceTemp' }, html );
991 if ( parent = dom.getParent( node, 'p' ) ) {
992 parent.parentNode.insertBefore( wrap, parent );
994 if ( dom.isEmpty( parent ) ) {
995 dom.remove( parent );
998 node.parentNode.insertBefore( wrap, node );
1001 editor.$( wrap ).find( 'dt.wp-caption-dt' ).append( node );
1005 // Remove the caption wrapper and place the image in new paragraph
1006 if ( imgNode.parentNode.nodeName === 'A' ) {
1007 html = dom.getOuterHTML( imgNode.parentNode );
1009 html = dom.getOuterHTML( imgNode );
1012 parent = dom.create( 'p', {}, html );
1013 dom.insertAfter( parent, wrap.parentNode );
1014 editor.selection.select( parent );
1015 editor.nodeChanged();
1016 dom.remove( wrap.parentNode );
1021 imgNode = dom.get('__wp-temp-img-id');
1022 dom.setAttrib( imgNode, 'id', imgId );
1023 event.imgData.node = imgNode;
1026 editor.on( 'wpLoadImageData', function( event ) {
1028 data = event.imgData.data,
1029 imgNode = event.imgData.node;
1031 if ( parent = dom.getParent( imgNode, 'dl.wp-caption' ) ) {
1032 parent = dom.select( 'dd.wp-caption-dd', parent )[0];
1035 data.caption = editor.serializer.serialize( parent )
1036 .replace( /<br[^>]*>/g, '$&\n' ).replace( /^<p>/, '' ).replace( /<\/p>$/, '' );
1041 dom.bind( editor.getDoc(), 'dragstart', function( event ) {
1042 var node = editor.selection.getNode();
1044 // Prevent dragging images out of the caption elements
1045 if ( node.nodeName === 'IMG' && dom.getParent( node, '.wp-caption' ) ) {
1046 event.preventDefault();
1050 // Prevent IE11 from making dl.wp-caption resizable
1051 if ( tinymce.Env.ie && tinymce.Env.ie > 10 ) {
1052 // The 'mscontrolselect' event is supported only in IE11+
1053 dom.bind( editor.getBody(), 'mscontrolselect', function( event ) {
1054 if ( event.target.nodeName === 'IMG' && dom.getParent( event.target, '.wp-caption' ) ) {
1055 // Hide the thick border with resize handles around dl.wp-caption
1056 editor.getBody().focus(); // :(
1057 } else if ( event.target.nodeName === 'DL' && dom.hasClass( event.target, 'wp-caption' ) ) {
1058 // Trigger the thick border with resize handles...
1059 // This will make the caption text editable.
1060 event.target.focus();
1066 editor.on( 'ObjectResized', function( event ) {
1067 var node = event.target;
1069 if ( node.nodeName === 'IMG' ) {
1070 editor.undoManager.transact( function() {
1074 node.className = node.className.replace( /\bsize-[^ ]+/, '' );
1076 if ( parent = dom.getParent( node, '.wp-caption' ) ) {
1077 width = event.width || dom.getAttrib( node, 'width' );
1080 width = parseInt( width, 10 );
1082 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
1086 dom.setStyle( parent, 'width', width + 'px' );
1093 editor.on( 'BeforeExecCommand', function( event ) {
1094 var node, p, DL, align, replacement,
1095 cmd = event.command,
1098 if ( cmd === 'mceInsertContent' ) {
1099 // When inserting content, if the caret is inside a caption create new paragraph under
1100 // and move the caret there
1101 if ( node = dom.getParent( editor.selection.getNode(), 'div.mceTemp' ) ) {
1102 p = dom.create( 'p' );
1103 dom.insertAfter( p, node );
1104 editor.selection.setCursorLocation( p, 0 );
1105 editor.nodeChanged();
1107 } else if ( cmd === 'JustifyLeft' || cmd === 'JustifyRight' || cmd === 'JustifyCenter' || cmd === 'wpAlignNone' ) {
1108 node = editor.selection.getNode();
1109 align = 'align' + cmd.slice( 7 ).toLowerCase();
1110 DL = editor.dom.getParent( node, '.wp-caption' );
1112 if ( node.nodeName !== 'IMG' && ! DL ) {
1118 if ( editor.dom.hasClass( node, align ) ) {
1119 replacement = ' alignnone';
1121 replacement = ' ' + align;
1124 node.className = node.className.replace( / ?align(left|center|right|none)/g, '' ) + replacement;
1126 editor.nodeChanged();
1127 event.preventDefault();
1129 if ( floatingToolbar ) {
1130 floatingToolbar.reposition();
1133 editor.fire( 'ExecCommand', {
1141 editor.on( 'keydown', function( event ) {
1142 var node, wrap, P, spacer,
1143 selection = editor.selection,
1144 keyCode = event.keyCode,
1146 VK = tinymce.util.VK;
1148 if ( keyCode === VK.ENTER ) {
1149 // When pressing Enter inside a caption move the caret to a new parapraph under it
1150 node = selection.getNode();
1151 wrap = dom.getParent( node, 'div.mceTemp' );
1154 dom.events.cancel( event ); // Doesn't cancel all :(
1156 // Remove any extra dt and dd cleated on pressing Enter...
1157 tinymce.each( dom.select( 'dt, dd', wrap ), function( element ) {
1158 if ( dom.isEmpty( element ) ) {
1159 dom.remove( element );
1163 spacer = tinymce.Env.ie && tinymce.Env.ie < 11 ? '' : '<br data-mce-bogus="1" />';
1164 P = dom.create( 'p', null, spacer );
1166 if ( node.nodeName === 'DD' ) {
1167 dom.insertAfter( P, wrap );
1169 wrap.parentNode.insertBefore( P, wrap );
1172 editor.nodeChanged();
1173 selection.setCursorLocation( P, 0 );
1175 } else if ( keyCode === VK.DELETE || keyCode === VK.BACKSPACE ) {
1176 node = selection.getNode();
1178 if ( node.nodeName === 'DIV' && dom.hasClass( node, 'mceTemp' ) ) {
1180 } else if ( node.nodeName === 'IMG' || node.nodeName === 'DT' || node.nodeName === 'A' ) {
1181 wrap = dom.getParent( node, 'div.mceTemp' );
1185 dom.events.cancel( event );
1186 removeImage( node );
1192 // After undo/redo FF seems to set the image height very slowly when it is set to 'auto' in the CSS.
1193 // This causes image.getBoundingClientRect() to return wrong values and the resize handles are shown in wrong places.
1194 // Collapse the selection to remove the resize handles.
1195 if ( tinymce.Env.gecko ) {
1196 editor.on( 'undo redo', function() {
1197 if ( editor.selection.getNode().nodeName === 'IMG' ) {
1198 editor.selection.collapse();
1203 editor.wpSetImgCaption = function( content ) {
1204 return parseShortcode( content );
1207 editor.wpGetImgCaption = function( content ) {
1208 return getShortcode( content );
1211 editor.on( 'BeforeSetContent', function( event ) {
1212 if ( event.format !== 'raw' ) {
1213 event.content = editor.wpSetImgCaption( event.content );
1217 editor.on( 'PostProcess', function( event ) {
1219 event.content = editor.wpGetImgCaption( event.content );
1224 _do_shcode: parseShortcode,
1225 _get_shcode: getShortcode