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 // Safari on iOS fails to select images in contentEditoble mode on touch.
85 editor.on( 'init', function() {
86 editor.on( 'touchstart', function( event ) {
87 if ( event.target.nodeName === 'IMG' ) {
92 editor.dom.bind( editor.getDoc(), 'touchmove', function( event ) {
93 if ( event.target.nodeName === 'IMG' ) {
98 editor.on( 'touchend', function( event ) {
99 if ( touchOnImage && event.target.nodeName === 'IMG' ) {
100 var node = event.target;
102 touchOnImage = false;
104 window.setTimeout( function() {
105 editor.selection.select( node );
106 editor.nodeChanged();
108 } else if ( toolbar ) {
115 function parseShortcode( content ) {
116 return content.replace( /(?:<p>)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:<\/p>)?/g, function( a, b, c ) {
117 var id, align, classes, caption, img, width;
119 id = b.match( /id=['"]([^'"]*)['"] ?/ );
121 b = b.replace( id[0], '' );
124 align = b.match( /align=['"]([^'"]*)['"] ?/ );
126 b = b.replace( align[0], '' );
129 classes = b.match( /class=['"]([^'"]*)['"] ?/ );
131 b = b.replace( classes[0], '' );
134 width = b.match( /width=['"]([0-9]*)['"] ?/ );
136 b = b.replace( width[0], '' );
140 img = c.match( /((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)([\s\S]*)/i );
142 if ( img && img[2] ) {
143 caption = trim( img[2] );
144 img = trim( img[1] );
146 // old captions shortcode style
147 caption = trim( b ).replace( /caption=['"]/, '' ).replace( /['"]$/, '' );
151 id = ( id && id[1] ) ? id[1].replace( /[<>&]+/g, '' ) : '';
152 align = ( align && align[1] ) ? align[1] : 'alignnone';
153 classes = ( classes && classes[1] ) ? ' ' + classes[1].replace( /[<>&]+/g, '' ) : '';
155 if ( ! width && img ) {
156 width = img.match( /width=['"]([0-9]*)['"]/ );
159 if ( width && width[1] ) {
163 if ( ! width || ! caption ) {
167 width = parseInt( width, 10 );
168 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
172 return '<div class="mceTemp"><dl id="' + id + '" class="wp-caption ' + align + classes + '" style="width: ' + width + 'px">' +
173 '<dt class="wp-caption-dt">'+ img +'</dt><dd class="wp-caption-dd">'+ caption +'</dd></dl></div>';
177 function getShortcode( content ) {
178 return content.replace( /(?:<div [^>]+mceTemp[^>]+>)?\s*(<dl [^>]+wp-caption[^>]+>[\s\S]+?<\/dl>)\s*(?:<\/div>)?/g, function( all, dl ) {
181 if ( dl.indexOf('<img ') === -1 ) {
182 // Broken caption. The user managed to drag the image out?
183 // Try to return the caption text as a paragraph.
184 out = dl.match( /<dd [^>]+>([\s\S]+?)<\/dd>/i );
186 if ( out && out[1] ) {
187 return '<p>' + out[1] + '</p>';
193 out = dl.replace( /\s*<dl ([^>]+)>\s*<dt [^>]+>([\s\S]+?)<\/dt>\s*<dd [^>]+>([\s\S]*?)<\/dd>\s*<\/dl>\s*/gi, function( a, b, c, caption ) {
194 var id, classes, align, width;
196 width = c.match( /width="([0-9]*)"/ );
197 width = ( width && width[1] ) ? width[1] : '';
199 classes = b.match( /class="([^"]*)"/ );
200 classes = ( classes && classes[1] ) ? classes[1] : '';
201 align = classes.match( /align[a-z]+/i ) || 'alignnone';
203 if ( ! width || ! caption ) {
204 if ( 'alignnone' !== align[0] ) {
205 c = c.replace( /><img/, ' class="' + align[0] + '"><img' );
210 id = b.match( /id="([^"]*)"/ );
211 id = ( id && id[1] ) ? id[1] : '';
213 classes = classes.replace( /wp-caption ?|align[a-z]+ ?/gi, '' );
216 classes = ' class="' + classes + '"';
219 caption = caption.replace( /\r\n|\r/g, '\n' ).replace( /<[a-zA-Z0-9]+( [^<>]+)?>/g, function( a ) {
220 // no line breaks inside HTML tags
221 return a.replace( /[\r\n\t]+/, ' ' );
224 // convert remaining line breaks to <br>
225 caption = caption.replace( /\s*\n\s*/g, '<br />' );
227 return '[caption id="' + id + '" align="' + align + '" width="' + width + '"' + classes + ']' + c + ' ' + caption + '[/caption]';
230 if ( out.indexOf('[caption') === -1 ) {
231 // the caption html seems broken, try to find the image that may be wrapped in a link
232 // and may be followed by <p> with the caption text.
233 out = dl.replace( /[\s\S]*?((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)(<p>[\s\S]*<\/p>)?[\s\S]*/gi, '<p>$1</p>$2' );
240 function extractImageData( imageNode ) {
241 var classes, extraClasses, metadata, captionBlock, caption, link, width, height,
242 captionClassName = [],
244 isIntRegExp = /^\d+$/;
246 // default attributes
248 attachment_id: false,
256 linkTargetBlank: false,
261 metadata.url = dom.getAttrib( imageNode, 'src' );
262 metadata.alt = dom.getAttrib( imageNode, 'alt' );
263 metadata.title = dom.getAttrib( imageNode, 'title' );
265 width = dom.getAttrib( imageNode, 'width' );
266 height = dom.getAttrib( imageNode, 'height' );
268 if ( ! isIntRegExp.test( width ) || parseInt( width, 10 ) < 1 ) {
269 width = imageNode.naturalWidth || imageNode.width;
272 if ( ! isIntRegExp.test( height ) || parseInt( height, 10 ) < 1 ) {
273 height = imageNode.naturalHeight || imageNode.height;
276 metadata.customWidth = metadata.width = width;
277 metadata.customHeight = metadata.height = height;
279 classes = tinymce.explode( imageNode.className, ' ' );
282 tinymce.each( classes, function( name ) {
284 if ( /^wp-image/.test( name ) ) {
285 metadata.attachment_id = parseInt( name.replace( 'wp-image-', '' ), 10 );
286 } else if ( /^align/.test( name ) ) {
287 metadata.align = name.replace( 'align', '' );
288 } else if ( /^size/.test( name ) ) {
289 metadata.size = name.replace( 'size-', '' );
291 extraClasses.push( name );
296 metadata.extraClasses = extraClasses.join( ' ' );
299 captionBlock = dom.getParents( imageNode, '.wp-caption' );
301 if ( captionBlock.length ) {
302 captionBlock = captionBlock[0];
304 classes = captionBlock.className.split( ' ' );
305 tinymce.each( classes, function( name ) {
306 if ( /^align/.test( name ) ) {
307 metadata.align = name.replace( 'align', '' );
308 } else if ( name && name !== 'wp-caption' ) {
309 captionClassName.push( name );
313 metadata.captionClassName = captionClassName.join( ' ' );
315 caption = dom.select( 'dd.wp-caption-dd', captionBlock );
316 if ( caption.length ) {
317 caption = caption[0];
319 metadata.caption = editor.serializer.serialize( caption )
320 .replace( /<br[^>]*>/g, '$&\n' ).replace( /^<p>/, '' ).replace( /<\/p>$/, '' );
325 if ( imageNode.parentNode && imageNode.parentNode.nodeName === 'A' ) {
326 link = imageNode.parentNode;
327 metadata.linkUrl = dom.getAttrib( link, 'href' );
328 metadata.linkTargetBlank = dom.getAttrib( link, 'target' ) === '_blank' ? true : false;
329 metadata.linkRel = dom.getAttrib( link, 'rel' );
330 metadata.linkClassName = link.className;
336 function hasTextContent( node ) {
337 return node && !! ( node.textContent || node.innerText );
340 // Verify HTML in captions
341 function verifyHTML( caption ) {
342 if ( ! caption || ( caption.indexOf( '<' ) === -1 && caption.indexOf( '>' ) === -1 ) ) {
346 if ( ! serializer ) {
347 serializer = new tinymce.html.Serializer( {}, editor.schema );
350 return serializer.serialize( editor.parser.parse( caption, { forced_root_block: false } ) );
353 function updateImage( imageNode, imageData ) {
354 var classes, className, node, html, parent, wrap, linkNode,
355 captionNode, dd, dl, id, attrs, linkAttrs, width, height, align,
358 classes = tinymce.explode( imageData.extraClasses, ' ' );
364 if ( ! imageData.caption ) {
365 classes.push( 'align' + imageData.align );
368 if ( imageData.attachment_id ) {
369 classes.push( 'wp-image-' + imageData.attachment_id );
370 if ( imageData.size && imageData.size !== 'custom' ) {
371 classes.push( 'size-' + imageData.size );
375 width = imageData.width;
376 height = imageData.height;
378 if ( imageData.size === 'custom' ) {
379 width = imageData.customWidth;
380 height = imageData.customHeight;
385 width: width || null,
386 height: height || null,
388 title: imageData.title || null,
389 'class': classes.join( ' ' ) || null
392 dom.setAttribs( imageNode, attrs );
395 href: imageData.linkUrl,
396 rel: imageData.linkRel || null,
397 target: imageData.linkTargetBlank ? '_blank': null,
398 'class': imageData.linkClassName || null
401 if ( imageNode.parentNode && imageNode.parentNode.nodeName === 'A' && ! hasTextContent( imageNode.parentNode ) ) {
402 // Update or remove an existing link wrapped around the image
403 if ( imageData.linkUrl ) {
404 dom.setAttribs( imageNode.parentNode, linkAttrs );
406 dom.remove( imageNode.parentNode, true );
408 } else if ( imageData.linkUrl ) {
409 if ( linkNode = dom.getParent( imageNode, 'a' ) ) {
410 // The image is inside a link together with other nodes,
411 // or is nested in another node, move it out
412 dom.insertAfter( imageNode, linkNode );
415 // Add link wrapped around the image
416 linkNode = dom.create( 'a', linkAttrs );
417 imageNode.parentNode.insertBefore( linkNode, imageNode );
418 linkNode.appendChild( imageNode );
421 captionNode = editor.dom.getParent( imageNode, '.mceTemp' );
423 if ( imageNode.parentNode && imageNode.parentNode.nodeName === 'A' && ! hasTextContent( imageNode.parentNode ) ) {
424 node = imageNode.parentNode;
429 if ( imageData.caption ) {
430 imageData.caption = verifyHTML( imageData.caption );
432 id = imageData.attachment_id ? 'attachment_' + imageData.attachment_id : null;
433 align = 'align' + ( imageData.align || 'none' );
434 className = 'wp-caption ' + align;
436 if ( imageData.captionClassName ) {
437 className += ' ' + imageData.captionClassName.replace( /[<>&]+/g, '' );
440 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
441 width = parseInt( width, 10 );
446 dl = dom.select( 'dl.wp-caption', captionNode );
449 dom.setAttribs( dl, {
452 style: 'width: ' + width + 'px'
456 dd = dom.select( '.wp-caption-dd', captionNode );
459 dom.setHTML( dd[0], imageData.caption );
463 id = id ? 'id="'+ id +'" ' : '';
465 // should create a new function for generating the caption markup
466 html = '<dl ' + id + 'class="' + className +'" style="width: '+ width +'px">' +
467 '<dt class="wp-caption-dt"></dt><dd class="wp-caption-dd">'+ imageData.caption +'</dd></dl>';
469 wrap = dom.create( 'div', { 'class': 'mceTemp' }, html );
471 if ( parent = dom.getParent( node, 'p' ) ) {
472 parent.parentNode.insertBefore( wrap, parent );
474 node.parentNode.insertBefore( wrap, node );
477 editor.$( wrap ).find( 'dt.wp-caption-dt' ).append( node );
479 if ( parent && dom.isEmpty( parent ) ) {
480 dom.remove( parent );
483 } else if ( captionNode ) {
484 // Remove the caption wrapper and place the image in new paragraph
485 parent = dom.create( 'p' );
486 captionNode.parentNode.insertBefore( parent, captionNode );
487 parent.appendChild( node );
488 dom.remove( captionNode );
491 if ( wp.media.events ) {
492 wp.media.events.trigger( 'editor:image-update', {
499 editor.nodeChanged();
502 function editImage( img ) {
503 var frame, callback, metadata;
505 if ( typeof wp === 'undefined' || ! wp.media ) {
506 editor.execCommand( 'mceImage' );
510 metadata = extractImageData( img );
512 // Manipulate the metadata by reference that is fed into the PostImage model used in the media modal
513 wp.media.events.trigger( 'editor:image-edit', {
521 state: 'image-details',
525 wp.media.events.trigger( 'editor:frame-create', { frame: frame } );
527 callback = function( imageData ) {
529 editor.undoManager.transact( function() {
530 updateImage( img, imageData );
535 frame.state('image-details').on( 'update', callback );
536 frame.state('replace-image').on( 'replace', callback );
537 frame.on( 'close', function() {
545 function removeImage( node ) {
546 var wrap = editor.dom.getParent( node, 'div.mceTemp' );
548 if ( ! wrap && node.nodeName === 'IMG' ) {
549 wrap = editor.dom.getParent( node, 'a' );
553 if ( wrap.nextSibling ) {
554 editor.selection.select( wrap.nextSibling );
555 } else if ( wrap.previousSibling ) {
556 editor.selection.select( wrap.previousSibling );
558 editor.selection.select( wrap.parentNode );
561 editor.selection.collapse( true );
562 editor.dom.remove( wrap );
564 editor.dom.remove( node );
567 editor.nodeChanged();
568 editor.undoManager.add();
571 editor.on( 'init', function() {
572 var dom = editor.dom,
573 captionClass = editor.getParam( 'wpeditimage_html5_captions' ) ? 'html5-captions' : 'html4-captions';
575 dom.addClass( editor.getBody(), captionClass );
577 // Add caption field to the default image dialog
578 editor.on( 'wpLoadImageForm', function( event ) {
579 if ( editor.getParam( 'wpeditimage_disable_captions' ) ) {
590 label: 'Image caption'
593 event.data.splice( event.data.length - 1, 0, captionField );
596 // Fix caption parent width for images added from URL
597 editor.on( 'wpNewImageRefresh', function( event ) {
598 var parent, captionWidth;
600 if ( parent = dom.getParent( event.node, 'dl.wp-caption' ) ) {
601 if ( ! parent.style.width ) {
602 captionWidth = parseInt( event.node.clientWidth, 10 ) + 10;
603 captionWidth = captionWidth ? captionWidth + 'px' : '50%';
604 dom.setStyle( parent, 'width', captionWidth );
609 editor.on( 'wpImageFormSubmit', function( event ) {
610 var data = event.imgData.data,
611 imgNode = event.imgData.node,
612 caption = event.imgData.caption,
616 wrap, parent, node, html, imgId;
618 // Temp image id so we can find the node later
619 data.id = '__wp-temp-img-id';
620 // Cancel the original callback
621 event.imgData.cancel = true;
623 if ( ! data.style ) {
628 // Delete the image and the caption
630 if ( wrap = dom.getParent( imgNode, 'div.mceTemp' ) ) {
632 } else if ( imgNode.parentNode.nodeName === 'A' ) {
633 dom.remove( imgNode.parentNode );
635 dom.remove( imgNode );
638 editor.nodeChanged();
644 caption = caption.replace( /\r\n|\r/g, '\n' ).replace( /<\/?[a-zA-Z0-9]+( [^<>]+)?>/g, function( a ) {
645 // No line breaks inside HTML tags
646 return a.replace( /[\r\n\t]+/, ' ' );
649 // Convert remaining line breaks to <br>
650 caption = caption.replace( /(<br[^>]*>)\s*\n\s*/g, '$1' ).replace( /\s*\n\s*/g, '<br />' );
651 caption = verifyHTML( caption );
655 // New image inserted
656 html = dom.createHTML( 'img', data );
659 node = editor.selection.getNode();
662 captionWidth = parseInt( data.width, 10 );
664 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
668 captionWidth = ' style="width: ' + captionWidth + 'px"';
671 html = '<dl class="wp-caption alignnone"' + captionWidth + '>' +
672 '<dt class="wp-caption-dt">'+ html +'</dt><dd class="wp-caption-dd">'+ caption +'</dd></dl>';
674 if ( node.nodeName === 'P' ) {
677 parent = dom.getParent( node, 'p' );
680 if ( parent && parent.nodeName === 'P' ) {
681 wrap = dom.create( 'div', { 'class': 'mceTemp' }, html );
682 parent.parentNode.insertBefore( wrap, parent );
683 editor.selection.select( wrap );
684 editor.nodeChanged();
686 if ( dom.isEmpty( parent ) ) {
687 dom.remove( parent );
690 editor.selection.setContent( '<div class="mceTemp">' + html + '</div>' );
693 editor.selection.setContent( html );
696 // Edit existing image
698 // Store the original image id if any
699 imgId = imgNode.id || null;
700 // Update the image node
701 dom.setAttribs( imgNode, data );
702 wrap = dom.getParent( imgNode, 'dl.wp-caption' );
706 if ( parent = dom.select( 'dd.wp-caption-dd', wrap )[0] ) {
707 parent.innerHTML = caption;
710 if ( imgNode.className ) {
711 captionId = imgNode.className.match( /wp-image-([0-9]+)/ );
712 captionAlign = imgNode.className.match( /align(left|right|center|none)/ );
715 if ( captionAlign ) {
716 captionAlign = captionAlign[0];
717 imgNode.className = imgNode.className.replace( /align(left|right|center|none)/g, '' );
719 captionAlign = 'alignnone';
722 captionAlign = ' class="wp-caption ' + captionAlign + '"';
725 captionId = ' id="attachment_' + captionId[1] + '"';
728 captionWidth = data.width || imgNode.clientWidth;
730 if ( captionWidth ) {
731 captionWidth = parseInt( captionWidth, 10 );
733 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
737 captionWidth = ' style="width: '+ captionWidth +'px"';
740 if ( imgNode.parentNode && imgNode.parentNode.nodeName === 'A' ) {
741 node = imgNode.parentNode;
746 html = '<dl ' + captionId + captionAlign + captionWidth + '>' +
747 '<dt class="wp-caption-dt"></dt><dd class="wp-caption-dd">'+ caption +'</dd></dl>';
749 wrap = dom.create( 'div', { 'class': 'mceTemp' }, html );
751 if ( parent = dom.getParent( node, 'p' ) ) {
752 parent.parentNode.insertBefore( wrap, parent );
754 node.parentNode.insertBefore( wrap, node );
757 editor.$( wrap ).find( 'dt.wp-caption-dt' ).append( node );
759 if ( parent && dom.isEmpty( parent ) ) {
760 dom.remove( parent );
765 // Remove the caption wrapper and place the image in new paragraph
766 if ( imgNode.parentNode.nodeName === 'A' ) {
767 html = dom.getOuterHTML( imgNode.parentNode );
769 html = dom.getOuterHTML( imgNode );
772 parent = dom.create( 'p', {}, html );
773 dom.insertAfter( parent, wrap.parentNode );
774 editor.selection.select( parent );
775 editor.nodeChanged();
776 dom.remove( wrap.parentNode );
781 imgNode = dom.get('__wp-temp-img-id');
782 dom.setAttrib( imgNode, 'id', imgId );
783 event.imgData.node = imgNode;
786 editor.on( 'wpLoadImageData', function( event ) {
788 data = event.imgData.data,
789 imgNode = event.imgData.node;
791 if ( parent = dom.getParent( imgNode, 'dl.wp-caption' ) ) {
792 parent = dom.select( 'dd.wp-caption-dd', parent )[0];
795 data.caption = editor.serializer.serialize( parent )
796 .replace( /<br[^>]*>/g, '$&\n' ).replace( /^<p>/, '' ).replace( /<\/p>$/, '' );
801 // Prevent IE11 from making dl.wp-caption resizable
802 if ( tinymce.Env.ie && tinymce.Env.ie > 10 ) {
803 // The 'mscontrolselect' event is supported only in IE11+
804 dom.bind( editor.getBody(), 'mscontrolselect', function( event ) {
805 if ( event.target.nodeName === 'IMG' && dom.getParent( event.target, '.wp-caption' ) ) {
806 // Hide the thick border with resize handles around dl.wp-caption
807 editor.getBody().focus(); // :(
808 } else if ( event.target.nodeName === 'DL' && dom.hasClass( event.target, 'wp-caption' ) ) {
809 // Trigger the thick border with resize handles...
810 // This will make the caption text editable.
811 event.target.focus();
817 editor.on( 'ObjectResized', function( event ) {
818 var node = event.target;
820 if ( node.nodeName === 'IMG' ) {
821 editor.undoManager.transact( function() {
825 node.className = node.className.replace( /\bsize-[^ ]+/, '' );
827 if ( parent = dom.getParent( node, '.wp-caption' ) ) {
828 width = event.width || dom.getAttrib( node, 'width' );
831 width = parseInt( width, 10 );
833 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
837 dom.setStyle( parent, 'width', width + 'px' );
844 editor.on( 'BeforeExecCommand', function( event ) {
845 var node, p, DL, align, replacement,
849 if ( cmd === 'mceInsertContent' ) {
850 // When inserting content, if the caret is inside a caption create new paragraph under
851 // and move the caret there
852 if ( node = dom.getParent( editor.selection.getNode(), 'div.mceTemp' ) ) {
853 p = dom.create( 'p' );
854 dom.insertAfter( p, node );
855 editor.selection.setCursorLocation( p, 0 );
856 editor.nodeChanged();
858 } else if ( cmd === 'JustifyLeft' || cmd === 'JustifyRight' || cmd === 'JustifyCenter' || cmd === 'wpAlignNone' ) {
859 node = editor.selection.getNode();
860 align = 'align' + cmd.slice( 7 ).toLowerCase();
861 DL = editor.dom.getParent( node, '.wp-caption' );
863 if ( node.nodeName !== 'IMG' && ! DL ) {
869 if ( editor.dom.hasClass( node, align ) ) {
870 replacement = ' alignnone';
872 replacement = ' ' + align;
875 node.className = trim( node.className.replace( / ?align(left|center|right|none)/g, '' ) + replacement );
877 editor.nodeChanged();
878 event.preventDefault();
881 toolbar.reposition();
884 editor.fire( 'ExecCommand', {
892 editor.on( 'keydown', function( event ) {
893 var node, wrap, P, spacer,
894 selection = editor.selection,
895 keyCode = event.keyCode,
897 VK = tinymce.util.VK;
899 if ( keyCode === VK.ENTER ) {
900 // When pressing Enter inside a caption move the caret to a new parapraph under it
901 node = selection.getNode();
902 wrap = dom.getParent( node, 'div.mceTemp' );
905 dom.events.cancel( event ); // Doesn't cancel all :(
907 // Remove any extra dt and dd cleated on pressing Enter...
908 tinymce.each( dom.select( 'dt, dd', wrap ), function( element ) {
909 if ( dom.isEmpty( element ) ) {
910 dom.remove( element );
914 spacer = tinymce.Env.ie && tinymce.Env.ie < 11 ? '' : '<br data-mce-bogus="1" />';
915 P = dom.create( 'p', null, spacer );
917 if ( node.nodeName === 'DD' ) {
918 dom.insertAfter( P, wrap );
920 wrap.parentNode.insertBefore( P, wrap );
923 editor.nodeChanged();
924 selection.setCursorLocation( P, 0 );
926 } else if ( keyCode === VK.DELETE || keyCode === VK.BACKSPACE ) {
927 node = selection.getNode();
929 if ( node.nodeName === 'DIV' && dom.hasClass( node, 'mceTemp' ) ) {
931 } else if ( node.nodeName === 'IMG' || node.nodeName === 'DT' || node.nodeName === 'A' ) {
932 wrap = dom.getParent( node, 'div.mceTemp' );
936 dom.events.cancel( event );
943 // After undo/redo FF seems to set the image height very slowly when it is set to 'auto' in the CSS.
944 // This causes image.getBoundingClientRect() to return wrong values and the resize handles are shown in wrong places.
945 // Collapse the selection to remove the resize handles.
946 if ( tinymce.Env.gecko ) {
947 editor.on( 'undo redo', function() {
948 if ( editor.selection.getNode().nodeName === 'IMG' ) {
949 editor.selection.collapse();
954 editor.wpSetImgCaption = function( content ) {
955 return parseShortcode( content );
958 editor.wpGetImgCaption = function( content ) {
959 return getShortcode( content );
962 editor.on( 'BeforeSetContent', function( event ) {
963 if ( event.format !== 'raw' ) {
964 event.content = editor.wpSetImgCaption( event.content );
968 editor.on( 'PostProcess', function( event ) {
970 event.content = editor.wpGetImgCaption( event.content );
977 editor.on( 'dragstart', function() {
978 var node = editor.selection.getNode();
980 if ( node.nodeName === 'IMG' ) {
981 wrap = editor.dom.getParent( node, '.mceTemp' );
983 if ( ! wrap && node.parentNode.nodeName === 'A' && ! hasTextContent( node.parentNode ) ) {
984 wrap = node.parentNode;
989 editor.on( 'drop', function( event ) {
990 var dom = editor.dom,
991 rng = tinymce.dom.RangeUtils.getCaretRangeFromPoint( event.clientX, event.clientY, editor.getDoc() );
993 // Don't allow anything to be dropped in a captioned image.
994 if ( dom.getParent( rng.startContainer, '.mceTemp' ) ) {
995 event.preventDefault();
997 event.preventDefault();
999 editor.undoManager.transact( function() {
1000 editor.selection.setRng( rng );
1001 editor.selection.setNode( wrap );
1011 editor.wp = editor.wp || {};
1012 editor.wp.isPlaceholder = isPlaceholder;
1016 _do_shcode: parseShortcode,
1017 _get_shcode: getShortcode