/gwtckeditor/src/main/resources/com/axeiya/gwtckeditor/public/ckeditor/_source/plugins/pastefromword/plugin.js

http://gwt-ckeditor.googlecode.com/ · JavaScript · 141 lines · 83 code · 19 blank · 39 comment · 14 complexity · 5d99a6f0c94d1ab26b819660aa3b9965 MD5 · raw file

  1. /*
  2. Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
  3. For licensing, see LICENSE.html or http://ckeditor.com/license
  4. */
  5. (function()
  6. {
  7. function forceHtmlMode( evt ) { evt.data.mode = 'html'; }
  8. CKEDITOR.plugins.add( 'pastefromword',
  9. {
  10. init : function( editor )
  11. {
  12. // Flag indicate this command is actually been asked instead of a generic
  13. // pasting.
  14. var forceFromWord = 0;
  15. var resetFromWord = function( evt )
  16. {
  17. evt && evt.removeListener();
  18. editor.removeListener( 'beforePaste', forceHtmlMode );
  19. forceFromWord && setTimeout( function() { forceFromWord = 0; }, 0 );
  20. };
  21. // Features bring by this command beside the normal process:
  22. // 1. No more bothering of user about the clean-up.
  23. // 2. Perform the clean-up even if content is not from MS-Word.
  24. // (e.g. from a MS-Word similar application.)
  25. editor.addCommand( 'pastefromword',
  26. {
  27. canUndo : false,
  28. exec : function()
  29. {
  30. // Ensure the received data format is HTML and apply content filtering. (#6718)
  31. forceFromWord = 1;
  32. editor.on( 'beforePaste', forceHtmlMode );
  33. if ( editor.execCommand( 'paste', 'html' ) === false )
  34. {
  35. editor.on( 'dialogShow', function ( evt )
  36. {
  37. evt.removeListener();
  38. evt.data.on( 'cancel', resetFromWord );
  39. });
  40. editor.on( 'dialogHide', function( evt )
  41. {
  42. evt.data.removeListener( 'cancel', resetFromWord );
  43. } );
  44. }
  45. editor.on( 'afterPaste', resetFromWord );
  46. }
  47. });
  48. // Register the toolbar button.
  49. editor.ui.addButton( 'PasteFromWord',
  50. {
  51. label : editor.lang.pastefromword.toolbar,
  52. command : 'pastefromword'
  53. });
  54. editor.on( 'pasteState', function( evt )
  55. {
  56. editor.getCommand( 'pastefromword' ).setState( evt.data );
  57. });
  58. editor.on( 'paste', function( evt )
  59. {
  60. var data = evt.data,
  61. mswordHtml;
  62. // MS-WORD format sniffing.
  63. if ( ( mswordHtml = data[ 'html' ] )
  64. && ( forceFromWord || ( /(class=\"?Mso|style=\"[^\"]*\bmso\-|w:WordDocument)/ ).test( mswordHtml ) ) )
  65. {
  66. var isLazyLoad = this.loadFilterRules( function()
  67. {
  68. // Event continuation with the original data.
  69. if ( isLazyLoad )
  70. editor.fire( 'paste', data );
  71. else if ( !editor.config.pasteFromWordPromptCleanup
  72. || ( forceFromWord || confirm( editor.lang.pastefromword.confirmCleanup ) ) )
  73. {
  74. data[ 'html' ] = CKEDITOR.cleanWord( mswordHtml, editor );
  75. }
  76. });
  77. // The cleanup rules are to be loaded, we should just cancel
  78. // this event.
  79. isLazyLoad && evt.cancel();
  80. }
  81. }, this );
  82. },
  83. loadFilterRules : function( callback )
  84. {
  85. var isLoaded = CKEDITOR.cleanWord;
  86. if ( isLoaded )
  87. callback();
  88. else
  89. {
  90. var filterFilePath = CKEDITOR.getUrl(
  91. CKEDITOR.config.pasteFromWordCleanupFile
  92. || ( this.path + 'filter/default.js' ) );
  93. // Load with busy indicator.
  94. CKEDITOR.scriptLoader.load( filterFilePath, callback, null, true );
  95. }
  96. return !isLoaded;
  97. },
  98. requires : [ 'clipboard' ]
  99. });
  100. })();
  101. /**
  102. * Whether to prompt the user about the clean up of content being pasted from
  103. * MS Word.
  104. * @name CKEDITOR.config.pasteFromWordPromptCleanup
  105. * @since 3.1
  106. * @type Boolean
  107. * @default undefined
  108. * @example
  109. * config.pasteFromWordPromptCleanup = true;
  110. */
  111. /**
  112. * The file that provides the MS Word cleanup function for pasting operations.
  113. * Note: This is a global configuration shared by all editor instances present
  114. * in the page.
  115. * @name CKEDITOR.config.pasteFromWordCleanupFile
  116. * @since 3.1
  117. * @type String
  118. * @default 'default'
  119. * @example
  120. * // Load from 'pastefromword' plugin 'filter' sub folder (custom.js file).
  121. * CKEDITOR.config.pasteFromWordCleanupFile = 'custom';
  122. */