[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-includes/js/tinymce/plugins/wpeditimage/ -> plugin.js (source)

   1  /* global tinymce */
   2  tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
   3      var toolbar, serializer, touchOnImage, pasteInCaption,
   4          each = tinymce.each,
   5          trim = tinymce.trim,
   6          iOS = tinymce.Env.iOS;
   7  
   8  	function isPlaceholder( node ) {
   9          return !! ( editor.dom.getAttrib( node, 'data-mce-placeholder' ) || editor.dom.getAttrib( node, 'data-mce-object' ) );
  10      }
  11  
  12      editor.addButton( 'wp_img_remove', {
  13          tooltip: 'Remove',
  14          icon: 'dashicon dashicons-no',
  15          onclick: function() {
  16              removeImage( editor.selection.getNode() );
  17          }
  18      } );
  19  
  20      editor.addButton( 'wp_img_edit', {
  21          tooltip: 'Edit|button', // '|button' is not displayed, only used for context
  22          icon: 'dashicon dashicons-edit',
  23          onclick: function() {
  24              editImage( editor.selection.getNode() );
  25          }
  26      } );
  27  
  28      each( {
  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 );
  35  
  36          editor.addButton( 'wp_img_' + name, {
  37              tooltip: tooltip,
  38              icon: 'dashicon dashicons-align-' + direction,
  39              cmd: 'alignnone' === name ? 'wpAlignNone' : 'Justify' + direction.slice( 0, 1 ).toUpperCase() + direction.slice( 1 ),
  40              onPostRender: function() {
  41                  var self = this;
  42  
  43                  editor.on( 'NodeChange', function( event ) {
  44                      var node;
  45  
  46                      // Don't bother.
  47                      if ( event.element.nodeName !== 'IMG' ) {
  48                          return;
  49                      }
  50  
  51                      node = editor.dom.getParent( event.element, '.wp-caption' ) || event.element;
  52  
  53                      if ( 'alignnone' === name ) {
  54                          self.active( ! /\balign(left|center|right)\b/.test( node.className ) );
  55                      } else {
  56                          self.active( editor.dom.hasClass( node, name ) );
  57                      }
  58                  } );
  59              }
  60          } );
  61      } );
  62  
  63      editor.once( 'preinit', function() {
  64          if ( editor.wp && editor.wp._createToolbar ) {
  65              toolbar = editor.wp._createToolbar( [
  66                  'wp_img_alignleft',
  67                  'wp_img_aligncenter',
  68                  'wp_img_alignright',
  69                  'wp_img_alignnone',
  70                  'wp_img_edit',
  71                  'wp_img_remove'
  72              ] );
  73          }
  74      } );
  75  
  76      editor.on( 'wptoolbar', function( event ) {
  77          if ( event.element.nodeName === 'IMG' && ! isPlaceholder( event.element ) ) {
  78              event.toolbar = toolbar;
  79          }
  80      } );
  81  
  82  	function isNonEditable( node ) {
  83          var parent = editor.$( node ).parents( '[contenteditable]' );
  84          return parent && parent.attr( 'contenteditable' ) === 'false';
  85      }
  86  
  87      // Safari on iOS fails to select images in contentEditoble mode on touch.
  88      // Select them again.
  89      if ( iOS ) {
  90          editor.on( 'init', function() {
  91              editor.on( 'touchstart', function( event ) {
  92                  if ( event.target.nodeName === 'IMG' && ! isNonEditable( event.target ) ) {
  93                      touchOnImage = true;
  94                  }
  95              });
  96  
  97              editor.dom.bind( editor.getDoc(), 'touchmove', function() {
  98                  touchOnImage = false;
  99              });
 100  
 101              editor.on( 'touchend', function( event ) {
 102                  if ( touchOnImage && event.target.nodeName === 'IMG' && ! isNonEditable( event.target ) ) {
 103                      var node = event.target;
 104  
 105                      touchOnImage = false;
 106  
 107                      window.setTimeout( function() {
 108                          editor.selection.select( node );
 109                          editor.nodeChanged();
 110                      }, 100 );
 111                  } else if ( toolbar ) {
 112                      toolbar.hide();
 113                  }
 114              });
 115          });
 116      }
 117  
 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;
 121  
 122              id = b.match( /id=['"]([^'"]*)['"] ?/ );
 123              if ( id ) {
 124                  b = b.replace( id[0], '' );
 125              }
 126  
 127              align = b.match( /align=['"]([^'"]*)['"] ?/ );
 128              if ( align ) {
 129                  b = b.replace( align[0], '' );
 130              }
 131  
 132              classes = b.match( /class=['"]([^'"]*)['"] ?/ );
 133              if ( classes ) {
 134                  b = b.replace( classes[0], '' );
 135              }
 136  
 137              width = b.match( /width=['"]([0-9]*)['"] ?/ );
 138              if ( width ) {
 139                  b = b.replace( width[0], '' );
 140              }
 141  
 142              c = trim( c );
 143              img = c.match( /((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)([\s\S]*)/i );
 144  
 145              if ( img && img[2] ) {
 146                  caption = trim( img[2] );
 147                  img = trim( img[1] );
 148              } else {
 149                  // old captions shortcode style
 150                  caption = trim( b ).replace( /caption=['"]/, '' ).replace( /['"]$/, '' );
 151                  img = c;
 152              }
 153  
 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,  '' ) : '';
 157  
 158              if ( ! width && img ) {
 159                  width = img.match( /width=['"]([0-9]*)['"]/ );
 160              }
 161  
 162              if ( width && width[1] ) {
 163                  width = width[1];
 164              }
 165  
 166              if ( ! width || ! caption ) {
 167                  return c;
 168              }
 169  
 170              width = parseInt( width, 10 );
 171              if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
 172                  width += 10;
 173              }
 174  
 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>';
 177          });
 178      }
 179  
 180  	function getShortcode( content ) {
 181          return content.replace( /(?:<div [^>]+mceTemp[^>]+>)?\s*(<dl [^>]+wp-caption[^>]+>[\s\S]+?<\/dl>)\s*(?:<\/div>)?/g, function( all, dl ) {
 182              var out = '';
 183  
 184              if ( dl.indexOf('<img ') === -1 || dl.indexOf('</p>') !== -1 ) {
 185                  // Broken caption. The user managed to drag the image out or type in the wrapper div?
 186                  // Remove the <dl>, <dd> and <dt> and return the remaining text.
 187                  return dl.replace( /<d[ldt]( [^>]+)?>/g, '' ).replace( /<\/d[ldt]>/g, '' );
 188              }
 189  
 190              out = dl.replace( /\s*<dl ([^>]+)>\s*<dt [^>]+>([\s\S]+?)<\/dt>\s*<dd [^>]+>([\s\S]*?)<\/dd>\s*<\/dl>\s*/gi, function( a, b, c, caption ) {
 191                  var id, classes, align, width;
 192  
 193                  width = c.match( /width="([0-9]*)"/ );
 194                  width = ( width && width[1] ) ? width[1] : '';
 195  
 196                  classes = b.match( /class="([^"]*)"/ );
 197                  classes = ( classes && classes[1] ) ? classes[1] : '';
 198                  align = classes.match( /align[a-z]+/i ) || 'alignnone';
 199  
 200                  if ( ! width || ! caption ) {
 201                      if ( 'alignnone' !== align[0] ) {
 202                          c = c.replace( /><img/, ' class="' + align[0] + '"><img' );
 203                      }
 204                      return c;
 205                  }
 206  
 207                  id = b.match( /id="([^"]*)"/ );
 208                  id = ( id && id[1] ) ? id[1] : '';
 209  
 210                  classes = classes.replace( /wp-caption ?|align[a-z]+ ?/gi, '' );
 211  
 212                  if ( classes ) {
 213                      classes = ' class="' + classes + '"';
 214                  }
 215  
 216                  caption = caption.replace( /\r\n|\r/g, '\n' ).replace( /<[a-zA-Z0-9]+( [^<>]+)?>/g, function( a ) {
 217                      // no line breaks inside HTML tags
 218                      return a.replace( /[\r\n\t]+/, ' ' );
 219                  });
 220  
 221                  // convert remaining line breaks to <br>
 222                  caption = caption.replace( /\s*\n\s*/g, '<br />' );
 223  
 224                  return '[caption id="' + id + '" align="' + align + '" width="' + width + '"' + classes + ']' + c + ' ' + caption + '[/caption]';
 225              });
 226  
 227              if ( out.indexOf('[caption') === -1 ) {
 228                  // the caption html seems broken, try to find the image that may be wrapped in a link
 229                  // and may be followed by <p> with the caption text.
 230                  out = dl.replace( /[\s\S]*?((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)(<p>[\s\S]*<\/p>)?[\s\S]*/gi, '<p>$1</p>$2' );
 231              }
 232  
 233              return out;
 234          });
 235      }
 236  
 237  	function extractImageData( imageNode ) {
 238          var classes, extraClasses, metadata, captionBlock, caption, link, width, height,
 239              captionClassName = [],
 240              dom = editor.dom,
 241              isIntRegExp = /^\d+$/;
 242  
 243          // default attributes
 244          metadata = {
 245              attachment_id: false,
 246              size: 'custom',
 247              caption: '',
 248              align: 'none',
 249              extraClasses: '',
 250              link: false,
 251              linkUrl: '',
 252              linkClassName: '',
 253              linkTargetBlank: false,
 254              linkRel: '',
 255              title: ''
 256          };
 257  
 258          metadata.url = dom.getAttrib( imageNode, 'src' );
 259          metadata.alt = dom.getAttrib( imageNode, 'alt' );
 260          metadata.title = dom.getAttrib( imageNode, 'title' );
 261  
 262          width = dom.getAttrib( imageNode, 'width' );
 263          height = dom.getAttrib( imageNode, 'height' );
 264  
 265          if ( ! isIntRegExp.test( width ) || parseInt( width, 10 ) < 1 ) {
 266              width = imageNode.naturalWidth || imageNode.width;
 267          }
 268  
 269          if ( ! isIntRegExp.test( height ) || parseInt( height, 10 ) < 1 ) {
 270              height = imageNode.naturalHeight || imageNode.height;
 271          }
 272  
 273          metadata.customWidth = metadata.width = width;
 274          metadata.customHeight = metadata.height = height;
 275  
 276          classes = tinymce.explode( imageNode.className, ' ' );
 277          extraClasses = [];
 278  
 279          tinymce.each( classes, function( name ) {
 280  
 281              if ( /^wp-image/.test( name ) ) {
 282                  metadata.attachment_id = parseInt( name.replace( 'wp-image-', '' ), 10 );
 283              } else if ( /^align/.test( name ) ) {
 284                  metadata.align = name.replace( 'align', '' );
 285              } else if ( /^size/.test( name ) ) {
 286                  metadata.size = name.replace( 'size-', '' );
 287              } else {
 288                  extraClasses.push( name );
 289              }
 290  
 291          } );
 292  
 293          metadata.extraClasses = extraClasses.join( ' ' );
 294  
 295          // Extract caption
 296          captionBlock = dom.getParents( imageNode, '.wp-caption' );
 297  
 298          if ( captionBlock.length ) {
 299              captionBlock = captionBlock[0];
 300  
 301              classes = captionBlock.className.split( ' ' );
 302              tinymce.each( classes, function( name ) {
 303                  if ( /^align/.test( name ) ) {
 304                      metadata.align = name.replace( 'align', '' );
 305                  } else if ( name && name !== 'wp-caption' ) {
 306                      captionClassName.push( name );
 307                  }
 308              } );
 309  
 310              metadata.captionClassName = captionClassName.join( ' ' );
 311  
 312              caption = dom.select( 'dd.wp-caption-dd', captionBlock );
 313              if ( caption.length ) {
 314                  caption = caption[0];
 315  
 316                  metadata.caption = editor.serializer.serialize( caption )
 317                      .replace( /<br[^>]*>/g, '$&\n' ).replace( /^<p>/, '' ).replace( /<\/p>$/, '' );
 318              }
 319          }
 320  
 321          // Extract linkTo
 322          if ( imageNode.parentNode && imageNode.parentNode.nodeName === 'A' ) {
 323              link = imageNode.parentNode;
 324              metadata.linkUrl = dom.getAttrib( link, 'href' );
 325              metadata.linkTargetBlank = dom.getAttrib( link, 'target' ) === '_blank' ? true : false;
 326              metadata.linkRel = dom.getAttrib( link, 'rel' );
 327              metadata.linkClassName = link.className;
 328          }
 329  
 330          return metadata;
 331      }
 332  
 333  	function hasTextContent( node ) {
 334          return node && !! ( node.textContent || node.innerText ).replace( /\ufeff/g, '' );
 335      }
 336  
 337      // Verify HTML in captions
 338  	function verifyHTML( caption ) {
 339          if ( ! caption || ( caption.indexOf( '<' ) === -1 && caption.indexOf( '>' ) === -1 ) ) {
 340              return caption;
 341          }
 342  
 343          if ( ! serializer ) {
 344              serializer = new tinymce.html.Serializer( {}, editor.schema );
 345          }
 346  
 347          return serializer.serialize( editor.parser.parse( caption, { forced_root_block: false } ) );
 348      }
 349  
 350  	function updateImage( $imageNode, imageData ) {
 351          var classes, className, node, html, parent, wrap, linkNode, imageNode,
 352              captionNode, dd, dl, id, attrs, linkAttrs, width, height, align,
 353              $imageNode, srcset, src,
 354              dom = editor.dom;
 355  
 356          if ( ! $imageNode || ! $imageNode.length ) {
 357              return;
 358          }
 359  
 360          imageNode = $imageNode[0];
 361          classes = tinymce.explode( imageData.extraClasses, ' ' );
 362  
 363          if ( ! classes ) {
 364              classes = [];
 365          }
 366  
 367          if ( ! imageData.caption ) {
 368              classes.push( 'align' + imageData.align );
 369          }
 370  
 371          if ( imageData.attachment_id ) {
 372              classes.push( 'wp-image-' + imageData.attachment_id );
 373              if ( imageData.size && imageData.size !== 'custom' ) {
 374                  classes.push( 'size-' + imageData.size );
 375              }
 376          }
 377  
 378          width = imageData.width;
 379          height = imageData.height;
 380  
 381          if ( imageData.size === 'custom' ) {
 382              width = imageData.customWidth;
 383              height = imageData.customHeight;
 384          }
 385  
 386          attrs = {
 387              src: imageData.url,
 388              width: width || null,
 389              height: height || null,
 390              title: imageData.title || null,
 391              'class': classes.join( ' ' ) || null
 392          };
 393  
 394          dom.setAttribs( imageNode, attrs );
 395  
 396          // Preserve empty alt attributes.
 397          $imageNode.attr( 'alt', imageData.alt || '' );
 398  
 399          linkAttrs = {
 400              href: imageData.linkUrl,
 401              rel: imageData.linkRel || null,
 402              target: imageData.linkTargetBlank ? '_blank': null,
 403              'class': imageData.linkClassName || null
 404          };
 405  
 406          if ( imageNode.parentNode && imageNode.parentNode.nodeName === 'A' && ! hasTextContent( imageNode.parentNode ) ) {
 407              // Update or remove an existing link wrapped around the image
 408              if ( imageData.linkUrl ) {
 409                  dom.setAttribs( imageNode.parentNode, linkAttrs );
 410              } else {
 411                  dom.remove( imageNode.parentNode, true );
 412              }
 413          } else if ( imageData.linkUrl ) {
 414              if ( linkNode = dom.getParent( imageNode, 'a' ) ) {
 415                  // The image is inside a link together with other nodes,
 416                  // or is nested in another node, move it out
 417                  dom.insertAfter( imageNode, linkNode );
 418              }
 419  
 420              // Add link wrapped around the image
 421              linkNode = dom.create( 'a', linkAttrs );
 422              imageNode.parentNode.insertBefore( linkNode, imageNode );
 423              linkNode.appendChild( imageNode );
 424          }
 425  
 426          captionNode = editor.dom.getParent( imageNode, '.mceTemp' );
 427  
 428          if ( imageNode.parentNode && imageNode.parentNode.nodeName === 'A' && ! hasTextContent( imageNode.parentNode ) ) {
 429              node = imageNode.parentNode;
 430          } else {
 431              node = imageNode;
 432          }
 433  
 434          if ( imageData.caption ) {
 435              imageData.caption = verifyHTML( imageData.caption );
 436  
 437              id = imageData.attachment_id ? 'attachment_' + imageData.attachment_id : null;
 438              align = 'align' + ( imageData.align || 'none' );
 439              className = 'wp-caption ' + align;
 440  
 441              if ( imageData.captionClassName ) {
 442                  className += ' ' + imageData.captionClassName.replace( /[<>&]+/g,  '' );
 443              }
 444  
 445              if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
 446                  width = parseInt( width, 10 );
 447                  width += 10;
 448              }
 449  
 450              if ( captionNode ) {
 451                  dl = dom.select( 'dl.wp-caption', captionNode );
 452  
 453                  if ( dl.length ) {
 454                      dom.setAttribs( dl, {
 455                          id: id,
 456                          'class': className,
 457                          style: 'width: ' + width + 'px'
 458                      } );
 459                  }
 460  
 461                  dd = dom.select( '.wp-caption-dd', captionNode );
 462  
 463                  if ( dd.length ) {
 464                      dom.setHTML( dd[0], imageData.caption );
 465                  }
 466  
 467              } else {
 468                  id = id ? 'id="'+ id +'" ' : '';
 469  
 470                  // should create a new function for generating the caption markup
 471                  html =  '<dl ' + id + 'class="' + className +'" style="width: '+ width +'px">' +
 472                      '<dt class="wp-caption-dt"></dt><dd class="wp-caption-dd">'+ imageData.caption +'</dd></dl>';
 473  
 474                  wrap = dom.create( 'div', { 'class': 'mceTemp' }, html );
 475  
 476                  if ( parent = dom.getParent( node, 'p' ) ) {
 477                      parent.parentNode.insertBefore( wrap, parent );
 478                  } else {
 479                      node.parentNode.insertBefore( wrap, node );
 480                  }
 481  
 482                  editor.$( wrap ).find( 'dt.wp-caption-dt' ).append( node );
 483  
 484                  if ( parent && dom.isEmpty( parent ) ) {
 485                      dom.remove( parent );
 486                  }
 487              }
 488          } else if ( captionNode ) {
 489              // Remove the caption wrapper and place the image in new paragraph
 490              parent = dom.create( 'p' );
 491              captionNode.parentNode.insertBefore( parent, captionNode );
 492              parent.appendChild( node );
 493              dom.remove( captionNode );
 494          }
 495  
 496          $imageNode = editor.$( imageNode );
 497          srcset = $imageNode.attr( 'srcset' );
 498          src = $imageNode.attr( 'src' );
 499  
 500          // Remove srcset and sizes if the image file was edited or the image was replaced.
 501          if ( srcset && src ) {
 502              src = src.replace( /[?#].*/, '' );
 503  
 504              if ( srcset.indexOf( src ) === -1 ) {
 505                  $imageNode.attr( 'srcset', null ).attr( 'sizes', null );
 506              }
 507          }
 508  
 509          if ( wp.media.events ) {
 510              wp.media.events.trigger( 'editor:image-update', {
 511                  editor: editor,
 512                  metadata: imageData,
 513                  image: imageNode
 514              } );
 515          }
 516  
 517          editor.nodeChanged();
 518      }
 519  
 520  	function editImage( img ) {
 521          var frame, callback, metadata, imageNode;
 522  
 523          if ( typeof wp === 'undefined' || ! wp.media ) {
 524              editor.execCommand( 'mceImage' );
 525              return;
 526          }
 527  
 528          metadata = extractImageData( img );
 529  
 530          // Mark the image node so we can select it later.
 531          editor.$( img ).attr( 'data-wp-editing', 1 );
 532  
 533          // Manipulate the metadata by reference that is fed into the PostImage model used in the media modal
 534          wp.media.events.trigger( 'editor:image-edit', {
 535              editor: editor,
 536              metadata: metadata,
 537              image: img
 538          } );
 539  
 540          frame = wp.media({
 541              frame: 'image',
 542              state: 'image-details',
 543              metadata: metadata
 544          } );
 545  
 546          wp.media.events.trigger( 'editor:frame-create', { frame: frame } );
 547  
 548          callback = function( imageData ) {
 549              editor.undoManager.transact( function() {
 550                  updateImage( imageNode, imageData );
 551              } );
 552              frame.detach();
 553          };
 554  
 555          frame.state('image-details').on( 'update', callback );
 556          frame.state('replace-image').on( 'replace', callback );
 557          frame.on( 'close', function() {
 558              editor.focus();
 559              frame.detach();
 560  
 561              // `close` fires first...
 562              // To be able to update the image node, we need to find it here,
 563              // and use it in the callback.
 564              imageNode = editor.$( 'img[data-wp-editing]' )
 565              imageNode.removeAttr( 'data-wp-editing' );
 566          });
 567  
 568          frame.open();
 569      }
 570  
 571  	function removeImage( node ) {
 572          var wrap = editor.dom.getParent( node, 'div.mceTemp' );
 573  
 574          if ( ! wrap && node.nodeName === 'IMG' ) {
 575              wrap = editor.dom.getParent( node, 'a' );
 576          }
 577  
 578          if ( wrap ) {
 579              if ( wrap.nextSibling ) {
 580                  editor.selection.select( wrap.nextSibling );
 581              } else if ( wrap.previousSibling ) {
 582                  editor.selection.select( wrap.previousSibling );
 583              } else {
 584                  editor.selection.select( wrap.parentNode );
 585              }
 586  
 587              editor.selection.collapse( true );
 588              editor.dom.remove( wrap );
 589          } else {
 590              editor.dom.remove( node );
 591          }
 592  
 593          editor.nodeChanged();
 594          editor.undoManager.add();
 595      }
 596  
 597      editor.on( 'init', function() {
 598          var dom = editor.dom,
 599              captionClass = editor.getParam( 'wpeditimage_html5_captions' ) ? 'html5-captions' : 'html4-captions';
 600  
 601          dom.addClass( editor.getBody(), captionClass );
 602  
 603          // Prevent IE11 from making dl.wp-caption resizable
 604          if ( tinymce.Env.ie && tinymce.Env.ie > 10 ) {
 605              // The 'mscontrolselect' event is supported only in IE11+
 606              dom.bind( editor.getBody(), 'mscontrolselect', function( event ) {
 607                  if ( event.target.nodeName === 'IMG' && dom.getParent( event.target, '.wp-caption' ) ) {
 608                      // Hide the thick border with resize handles around dl.wp-caption
 609                      editor.getBody().focus(); // :(
 610                  } else if ( event.target.nodeName === 'DL' && dom.hasClass( event.target, 'wp-caption' ) ) {
 611                      // Trigger the thick border with resize handles...
 612                      // This will make the caption text editable.
 613                      event.target.focus();
 614                  }
 615              });
 616          }
 617      });
 618  
 619      editor.on( 'ObjectResized', function( event ) {
 620          var node = event.target;
 621  
 622          if ( node.nodeName === 'IMG' ) {
 623              editor.undoManager.transact( function() {
 624                  var parent, width,
 625                      dom = editor.dom;
 626  
 627                  node.className = node.className.replace( /\bsize-[^ ]+/, '' );
 628  
 629                  if ( parent = dom.getParent( node, '.wp-caption' ) ) {
 630                      width = event.width || dom.getAttrib( node, 'width' );
 631  
 632                      if ( width ) {
 633                          width = parseInt( width, 10 );
 634  
 635                          if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
 636                              width += 10;
 637                          }
 638  
 639                          dom.setStyle( parent, 'width', width + 'px' );
 640                      }
 641                  }
 642              });
 643          }
 644      });
 645  
 646      editor.on( 'pastePostProcess', function( event ) {
 647          // Pasting in a caption node.
 648          if ( editor.dom.getParent( editor.selection.getNode(), 'dd.wp-caption-dd' ) ) {
 649              // Remove "non-block" elements that should not be in captions.
 650              editor.$( 'img, audio, video, object, embed, iframe, script, style', event.node ).remove();
 651  
 652              editor.$( '*', event.node ).each( function( i, node ) {
 653                  if ( editor.dom.isBlock( node ) ) {
 654                      // Insert <br> where the blocks used to be. Makes it look better after pasting in the caption.
 655                      if ( tinymce.trim( node.textContent || node.innerText ) ) {
 656                          editor.dom.insertAfter( editor.dom.create( 'br' ), node );
 657                          editor.dom.remove( node, true );
 658                      } else {
 659                          editor.dom.remove( node );
 660                      }
 661                  }
 662              });
 663  
 664              // Trim <br> tags.
 665              editor.$( 'br',  event.node ).each( function( i, node ) {
 666                  if ( ! node.nextSibling || node.nextSibling.nodeName === 'BR' ||
 667                      ! node.previousSibling || node.previousSibling.nodeName === 'BR' ) {
 668  
 669                      editor.dom.remove( node );
 670                  }
 671              } );
 672  
 673              // Pasted HTML is cleaned up for inserting in the caption.
 674              pasteInCaption = true;
 675          }
 676      });
 677  
 678      editor.on( 'BeforeExecCommand', function( event ) {
 679          var node, p, DL, align, replacement, captionParent,
 680              cmd = event.command,
 681              dom = editor.dom;
 682  
 683          if ( cmd === 'mceInsertContent' || cmd === 'Indent' || cmd === 'Outdent' ) {
 684              node = editor.selection.getNode();
 685              captionParent = dom.getParent( node, 'div.mceTemp' );
 686  
 687              if ( captionParent ) {
 688                  if ( cmd === 'mceInsertContent' ) {
 689                      if ( pasteInCaption ) {
 690                          pasteInCaption = false;
 691                          // We are in the caption element, and in 'paste' context,
 692                          // and the pasted HTML was cleaned up on 'pastePostProcess' above.
 693                          // Let it be pasted in the caption.
 694                          return;
 695                      }
 696  
 697                      // The paste is somewhere else in the caption DL element.
 698                      // Prevent pasting in there as it will break the caption.
 699                      // Make new paragraph under the caption DL and move the caret there.
 700                      p = dom.create( 'p' );
 701                      dom.insertAfter( p, captionParent );
 702                      editor.selection.setCursorLocation( p, 0 );
 703  
 704                      // If the image is selected and the user pastes "over" it,
 705                      // replace both the image and the caption elements with the pasted content.
 706                      // This matches the behavior when pasting over non-caption images.
 707                      if ( node.nodeName === 'IMG' ) {
 708                          editor.$( captionParent ).remove();
 709                      }
 710  
 711                      editor.nodeChanged();
 712                  } else {
 713                      // Clicking Indent or Outdent while an image with a caption is selected breaks the caption.
 714                      // See #38313.
 715                      event.preventDefault();
 716                      event.stopImmediatePropagation();
 717                      return false;
 718                  }
 719              }
 720          } else if ( cmd === 'JustifyLeft' || cmd === 'JustifyRight' || cmd === 'JustifyCenter' || cmd === 'wpAlignNone' ) {
 721              node = editor.selection.getNode();
 722              align = 'align' + cmd.slice( 7 ).toLowerCase();
 723              DL = editor.dom.getParent( node, '.wp-caption' );
 724  
 725              if ( node.nodeName !== 'IMG' && ! DL ) {
 726                  return;
 727              }
 728  
 729              node = DL || node;
 730  
 731              if ( editor.dom.hasClass( node, align ) ) {
 732                  replacement = ' alignnone';
 733              } else {
 734                  replacement = ' ' + align;
 735              }
 736  
 737              node.className = trim( node.className.replace( / ?align(left|center|right|none)/g, '' ) + replacement );
 738  
 739              editor.nodeChanged();
 740              event.preventDefault();
 741  
 742              if ( toolbar ) {
 743                  toolbar.reposition();
 744              }
 745  
 746              editor.fire( 'ExecCommand', {
 747                  command: cmd,
 748                  ui: event.ui,
 749                  value: event.value
 750              } );
 751          }
 752      });
 753  
 754      editor.on( 'keydown', function( event ) {
 755          var node, wrap, P, spacer,
 756              selection = editor.selection,
 757              keyCode = event.keyCode,
 758              dom = editor.dom,
 759              VK = tinymce.util.VK;
 760  
 761          if ( keyCode === VK.ENTER ) {
 762              // When pressing Enter inside a caption move the caret to a new parapraph under it
 763              node = selection.getNode();
 764              wrap = dom.getParent( node, 'div.mceTemp' );
 765  
 766              if ( wrap ) {
 767                  dom.events.cancel( event ); // Doesn't cancel all :(
 768  
 769                  // Remove any extra dt and dd cleated on pressing Enter...
 770                  tinymce.each( dom.select( 'dt, dd', wrap ), function( element ) {
 771                      if ( dom.isEmpty( element ) ) {
 772                          dom.remove( element );
 773                      }
 774                  });
 775  
 776                  spacer = tinymce.Env.ie && tinymce.Env.ie < 11 ? '' : '<br data-mce-bogus="1" />';
 777                  P = dom.create( 'p', null, spacer );
 778  
 779                  if ( node.nodeName === 'DD' ) {
 780                      dom.insertAfter( P, wrap );
 781                  } else {
 782                      wrap.parentNode.insertBefore( P, wrap );
 783                  }
 784  
 785                  editor.nodeChanged();
 786                  selection.setCursorLocation( P, 0 );
 787              }
 788          } else if ( keyCode === VK.DELETE || keyCode === VK.BACKSPACE ) {
 789              node = selection.getNode();
 790  
 791              if ( node.nodeName === 'DIV' && dom.hasClass( node, 'mceTemp' ) ) {
 792                  wrap = node;
 793              } else if ( node.nodeName === 'IMG' || node.nodeName === 'DT' || node.nodeName === 'A' ) {
 794                  wrap = dom.getParent( node, 'div.mceTemp' );
 795              }
 796  
 797              if ( wrap ) {
 798                  dom.events.cancel( event );
 799                  removeImage( node );
 800                  return false;
 801              }
 802          }
 803      });
 804  
 805      // After undo/redo FF seems to set the image height very slowly when it is set to 'auto' in the CSS.
 806      // This causes image.getBoundingClientRect() to return wrong values and the resize handles are shown in wrong places.
 807      // Collapse the selection to remove the resize handles.
 808      if ( tinymce.Env.gecko ) {
 809          editor.on( 'undo redo', function() {
 810              if ( editor.selection.getNode().nodeName === 'IMG' ) {
 811                  editor.selection.collapse();
 812              }
 813          });
 814      }
 815  
 816      editor.wpSetImgCaption = function( content ) {
 817          return parseShortcode( content );
 818      };
 819  
 820      editor.wpGetImgCaption = function( content ) {
 821          return getShortcode( content );
 822      };
 823  
 824      editor.on( 'beforeGetContent', function( event ) {
 825          if ( event.format !== 'raw' ) {
 826              editor.$( 'img[id="__wp-temp-img-id"]' ).removeAttr( 'id' );
 827          }
 828      });
 829  
 830      editor.on( 'BeforeSetContent', function( event ) {
 831          if ( event.format !== 'raw' ) {
 832              event.content = editor.wpSetImgCaption( event.content );
 833          }
 834      });
 835  
 836      editor.on( 'PostProcess', function( event ) {
 837          if ( event.get ) {
 838              event.content = editor.wpGetImgCaption( event.content );
 839          }
 840      });
 841  
 842      ( function() {
 843          var wrap;
 844  
 845          editor.on( 'dragstart', function() {
 846              var node = editor.selection.getNode();
 847  
 848              if ( node.nodeName === 'IMG' ) {
 849                  wrap = editor.dom.getParent( node, '.mceTemp' );
 850  
 851                  if ( ! wrap && node.parentNode.nodeName === 'A' && ! hasTextContent( node.parentNode ) ) {
 852                      wrap = node.parentNode;
 853                  }
 854              }
 855          } );
 856  
 857          editor.on( 'drop', function( event ) {
 858              var dom = editor.dom,
 859                  rng = tinymce.dom.RangeUtils.getCaretRangeFromPoint( event.clientX, event.clientY, editor.getDoc() );
 860  
 861              // Don't allow anything to be dropped in a captioned image.
 862              if ( rng && dom.getParent( rng.startContainer, '.mceTemp' ) ) {
 863                  event.preventDefault();
 864              } else if ( wrap ) {
 865                  event.preventDefault();
 866  
 867                  editor.undoManager.transact( function() {
 868                      if ( rng ) {
 869                          editor.selection.setRng( rng );
 870                      }
 871  
 872                      editor.selection.setNode( wrap );
 873                      dom.remove( wrap );
 874                  } );
 875              }
 876  
 877              wrap = null;
 878          } );
 879      } )();
 880  
 881      // Add to editor.wp
 882      editor.wp = editor.wp || {};
 883      editor.wp.isPlaceholder = isPlaceholder;
 884  
 885      // Back-compat.
 886      return {
 887          _do_shcode: parseShortcode,
 888          _get_shcode: getShortcode
 889      };
 890  });


Generated: Fri Oct 25 08:20:01 2019 Cross-referenced by PHPXref 0.7