plugin.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * This file is part of the TinyMCE Advanced WordPress plugin and is released under the same license.
  3. * For more information please see tinymce-advanced.php.
  4. *
  5. * Copyright (c) 2007-2019 Andrew Ozz. All rights reserved.
  6. */
  7. ( function( tinymce ) {
  8. tinymce.PluginManager.add( 'wptadv', function( editor ) {
  9. var noAutop = ( ! editor.settings.wpautop && editor.settings.tadv_noautop );
  10. function addLineBreaks( html ) {
  11. var blocklist = 'table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre' +
  12. '|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section' +
  13. '|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary';
  14. html = html.replace( new RegExp( '<(?:' + blocklist + ')(?: [^>]*)?>', 'gi' ), '\n$&' );
  15. html = html.replace( new RegExp( '</(?:' + blocklist + ')>', 'gi' ), '$&\n' );
  16. html = html.replace( /(<br(?: [^>]*)?>)[\r\n\t]*/gi, '$1\n' );
  17. html = html.replace( />\n[\r\n\t]+</g, '>\n<' );
  18. html = html.replace( /^<li/gm, '\t<li' );
  19. html = html.replace( /<td>\u00a0<\/td>/g, '<td>&nbsp;</td>' );
  20. return tinymce.trim( html );
  21. }
  22. editor.addCommand( 'Tadv_Mark', function() {
  23. editor.formatter.toggle('mark');
  24. });
  25. editor.addButton( 'tadv_mark', {
  26. icon: 'backcolor',
  27. tooltip: 'Mark',
  28. cmd: 'Tadv_Mark',
  29. stateSelector: 'mark'
  30. });
  31. editor.on( 'init', function() {
  32. if ( noAutop ) {
  33. editor.on( 'SaveContent', function( event ) {
  34. event.content = event.content.replace( /caption\](\s|<br[^>]*>|<p>&nbsp;<\/p>)*\[caption/g, 'caption] [caption' );
  35. event.content = event.content.replace( /<(object|audio|video)[\s\S]+?<\/\1>/g, function( match ) {
  36. return match.replace( /[\r\n\t ]+/g, ' ' );
  37. });
  38. event.content = event.content.replace( /<pre( [^>]*)?>[\s\S]+?<\/pre>/g, function( match ) {
  39. match = match.replace( /<br ?\/?>(\r\n|\n)?/g, '\n' );
  40. return match.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '\n' );
  41. });
  42. event.content = addLineBreaks( event.content );
  43. });
  44. }
  45. try {
  46. if ( editor.plugins.searchreplace && ! editor.controlManager.buttons.searchreplace ) {
  47. editor.shortcuts.remove( 'meta+f' );
  48. }
  49. } catch ( er ) {}
  50. editor.formatter.register({
  51. mark: { inline: 'mark' }
  52. });
  53. });
  54. editor.on( 'ObjectResizeStart', function( event ) {
  55. var element = event.target;
  56. var table = editor.$( element );
  57. var parentWidth;
  58. var tableWidth;
  59. var width;
  60. if ( table.is( 'table' ) ) {
  61. if ( element.style.width && element.style.width.indexOf( '%' ) !== -1 ) {
  62. return;
  63. }
  64. parentWidth = parseInt( table.parent().css( 'width' ), 10 );
  65. tableWidth = parseInt( event.width, 10 );
  66. if ( parentWidth && tableWidth ) {
  67. if ( Math.abs( parentWidth - tableWidth ) < 3 ) {
  68. table.css({ width: '100%' });
  69. } else {
  70. width = Math.round( ( tableWidth / parentWidth ) * 100 );
  71. if ( width > 10 && width < 200 ) {
  72. table.css({ width: width + '%' });
  73. }
  74. }
  75. }
  76. }
  77. }, true );
  78. editor.addMenuItem( 'tmaresettablesize', {
  79. text: 'Reset table size',
  80. cmd: 'tmaResetTableSize',
  81. icon: 'dashicon dashicons-image-flip-horizontal',
  82. context: 'format',
  83. });
  84. editor.addMenuItem( 'tmaremovetablestyles', {
  85. text: 'Remove table styling',
  86. cmd: 'tmaRemoveTableStyles',
  87. icon: 'dashicon dashicons-editor-table',
  88. context: 'format',
  89. });
  90. editor.addButton( 'tmaresettablesize', {
  91. title: 'Reset table size',
  92. cmd: 'tmaResetTableSize',
  93. icon: 'dashicon dashicons-image-flip-horizontal',
  94. } );
  95. editor.addButton( 'tmaremovetablestyles', {
  96. title: 'Remove table styling',
  97. cmd: 'tmaRemoveTableStyles',
  98. icon: 'dashicon dashicons-editor-table',
  99. } );
  100. editor.addCommand( 'tmaRemoveTableStyles', function() {
  101. var node = editor.selection.getStart();
  102. var table = editor.dom.getParents( node, 'table' );
  103. if ( table ) {
  104. editor.$( table ).attr({
  105. style: null,
  106. width: null,
  107. height: null,
  108. border: null,
  109. cellspacing: null,
  110. cellpadding: null
  111. }).find( 'tr, td' ).each( function( i, element ) {
  112. editor.$( element ).attr({
  113. style: null,
  114. width: null,
  115. height: null
  116. });
  117. } );
  118. }
  119. } );
  120. editor.addCommand( 'tmaResetTableSize', function() {
  121. var node = editor.selection.getStart();
  122. var table = editor.dom.getParents( node, 'table' );
  123. if ( table ) {
  124. removeInlineSizes( table );
  125. editor.$( table ).find( 'tr, td' ).each( function( i, element ) {
  126. removeInlineSizes( element );
  127. } );
  128. }
  129. } );
  130. function removeInlineSizes( node ) {
  131. var element = editor.$( node );
  132. element.css({ width: null, height: null });
  133. if ( ! element.attr( 'style' ) ) {
  134. element.attr({ style: null });
  135. }
  136. }
  137. if ( noAutop ) {
  138. editor.on( 'beforeSetContent', function( event ) {
  139. var autop;
  140. var wp = window.wp;
  141. if ( ! wp ) {
  142. return;
  143. }
  144. autop = wp.editor && wp.editor.autop;
  145. if ( ! autop ) {
  146. autop = wp.oldEditor && wp.oldEditor.autop;
  147. }
  148. if ( event.load && autop && event.content && event.content.indexOf( '\n' ) > -1 && ! /<p>/i.test( event.content ) ) {
  149. event.content = autop( event.content );
  150. }
  151. }, true );
  152. if ( editor.settings.classic_block_editor ) {
  153. editor.on( 'beforeGetContent', function( event ) {
  154. if ( event.format === 'raw' ) {
  155. return;
  156. }
  157. var blocks = tinymce.$( '.block-editor-block-list__block' );
  158. if ( blocks.length === 1 && blocks.attr( 'data-type' ) === 'core/freeform' ) {
  159. // Mark all paragraph tags inside a single freeform block so they are not stripped by the block editor...
  160. editor.$( 'p' ).each( function ( i, node ) {
  161. if ( ! node.hasAttributes() ) {
  162. editor.$( node ).attr( 'data-tadv-p', 'keep' );
  163. }
  164. } );
  165. } else {
  166. // Remove the above ugliness...
  167. editor.$( 'p[data-tadv-p]' ).removeAttr( 'data-tadv-p' );
  168. }
  169. }, true );
  170. }
  171. }
  172. return {
  173. addLineBreaks: addLineBreaks
  174. };
  175. });
  176. }( window.tinymce ));