[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  ( function( tinymce ) {
   2      tinymce.PluginManager.add( 'wpemoji', function( editor ) {
   3          var typing,
   4              wp = window.wp,
   5              settings = window._wpemojiSettings,
   6              env = tinymce.Env,
   7              ua = window.navigator.userAgent,
   8              isWin = ua.indexOf( 'Windows' ) > -1,
   9              isWin8 = ( function() {
  10                  var match = ua.match( /Windows NT 6\.(\d)/ );
  11  
  12                  if ( match && match[1] > 1 ) {
  13                      return true;
  14                  }
  15  
  16                  return false;
  17              }());
  18  
  19          if ( ! wp || ! wp.emoji || settings.supports.everything ) {
  20              return;
  21          }
  22  
  23  		function setImgAttr( image ) {
  24              image.className = 'emoji';
  25              image.setAttribute( 'data-mce-resize', 'false' );
  26              image.setAttribute( 'data-mce-placeholder', '1' );
  27              image.setAttribute( 'data-wp-emoji', '1' );
  28          }
  29  
  30  		function replaceEmoji( node ) {
  31              var imgAttr = {
  32                  'data-mce-resize': 'false',
  33                  'data-mce-placeholder': '1',
  34                  'data-wp-emoji': '1'
  35              };
  36  
  37              wp.emoji.parse( node, { imgAttr: imgAttr } );
  38          }
  39  
  40          // Test if the node text contains emoji char(s) and replace.
  41  		function parseNode( node ) {
  42              var selection, bookmark;
  43  
  44              if ( node && window.twemoji && window.twemoji.test( node.textContent || node.innerText ) ) {
  45                  if ( env.webkit ) {
  46                      selection = editor.selection;
  47                      bookmark = selection.getBookmark();
  48                  }
  49  
  50                  replaceEmoji( node );
  51  
  52                  if ( env.webkit ) {
  53                      selection.moveToBookmark( bookmark );
  54                  }
  55              }
  56          }
  57  
  58          if ( isWin8 ) {
  59              // Windows 8+ emoji can be "typed" with the onscreen keyboard.
  60              // That triggers the normal keyboard events, but not the 'input' event.
  61              // Thankfully it sets keyCode 231 when the onscreen keyboard inserts any emoji.
  62              editor.on( 'keyup', function( event ) {
  63                  if ( event.keyCode === 231 ) {
  64                      parseNode( editor.selection.getNode() );
  65                  }
  66              } );
  67          } else if ( ! isWin ) {
  68              // In MacOS inserting emoji doesn't trigger the stanradr keyboard events.
  69              // Thankfully it triggers the 'input' event.
  70              // This works in Android and iOS as well.
  71              editor.on( 'keydown keyup', function( event ) {
  72                  typing = ( event.type === 'keydown' );
  73              } );
  74  
  75              editor.on( 'input', function() {
  76                  if ( typing ) {
  77                      return;
  78                  }
  79  
  80                  parseNode( editor.selection.getNode() );
  81              });
  82          }
  83  
  84          editor.on( 'setcontent', function( event ) {
  85              var selection = editor.selection,
  86                  node = selection.getNode();
  87  
  88              if ( window.twemoji && window.twemoji.test( node.textContent || node.innerText ) ) {
  89                  replaceEmoji( node );
  90  
  91                  // In IE all content in the editor is left selected after wp.emoji.parse()...
  92                  // Collapse the selection to the beginning.
  93                  if ( env.ie && env.ie < 9 && event.load && node && node.nodeName === 'BODY' ) {
  94                      selection.collapse( true );
  95                  }
  96              }
  97          } );
  98  
  99          // Convert Twemoji compatible pasted emoji replacement images into our format.
 100          editor.on( 'PastePostProcess', function( event ) {
 101              if ( window.twemoji ) {
 102                  tinymce.each( editor.dom.$( 'img.emoji', event.node ), function( image ) {
 103                      if ( image.alt && window.twemoji.test( image.alt ) ) {
 104                          setImgAttr( image );
 105                      }
 106                  });
 107              }
 108          });
 109  
 110          editor.on( 'postprocess', function( event ) {
 111              if ( event.content ) {
 112                  event.content = event.content.replace( /<img[^>]+data-wp-emoji="[^>]+>/g, function( img ) {
 113                      var alt = img.match( /alt="([^"]+)"/ );
 114  
 115                      if ( alt && alt[1] ) {
 116                          return alt[1];
 117                      }
 118  
 119                      return img;
 120                  });
 121              }
 122          } );
 123  
 124          editor.on( 'resolvename', function( event ) {
 125              if ( event.target.nodeName === 'IMG' && editor.dom.getAttrib( event.target, 'data-wp-emoji' ) ) {
 126                  event.preventDefault();
 127              }
 128          } );
 129      } );
 130  } )( window.tinymce );


Generated: Tue Sep 17 01:00:03 2019 Cross-referenced by PHPXref 0.7.1