plugin.js 3.5 KB

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