plugin.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or http://ckeditor.com/license
  4. */
  5. ( function() {
  6. CKEDITOR.plugins.add( 'pastefromword', {
  7. requires: 'clipboard',
  8. // jscs:disable maximumLineLength
  9. lang: 'af,ar,bg,bn,bs,ca,cs,cy,da,de,el,en,en-au,en-ca,en-gb,eo,es,et,eu,fa,fi,fo,fr,fr-ca,gl,gu,he,hi,hr,hu,id,is,it,ja,ka,km,ko,ku,lt,lv,mk,mn,ms,nb,nl,no,pl,pt,pt-br,ro,ru,si,sk,sl,sq,sr,sr-latn,sv,th,tr,tt,ug,uk,vi,zh,zh-cn', // %REMOVE_LINE_CORE%
  10. // jscs:enable maximumLineLength
  11. icons: 'pastefromword,pastefromword-rtl', // %REMOVE_LINE_CORE%
  12. hidpi: true, // %REMOVE_LINE_CORE%
  13. init: function( editor ) {
  14. var commandName = 'pastefromword',
  15. // Flag indicate this command is actually been asked instead of a generic pasting.
  16. forceFromWord = 0,
  17. path = this.path;
  18. editor.addCommand( commandName, {
  19. // Snapshots are done manually by editable.insertXXX methods.
  20. canUndo: false,
  21. async: true,
  22. exec: function( editor ) {
  23. var cmd = this;
  24. forceFromWord = 1;
  25. // Force html mode for incomming paste events sequence.
  26. editor.once( 'beforePaste', forceHtmlMode );
  27. editor.getClipboardData( { title: editor.lang.pastefromword.title }, function( data ) {
  28. // Do not use editor#paste, because it would start from beforePaste event.
  29. data && editor.fire( 'paste', {
  30. type: 'html',
  31. dataValue: data.dataValue,
  32. method: 'paste',
  33. dataTransfer: CKEDITOR.plugins.clipboard.initPasteDataTransfer()
  34. } );
  35. editor.fire( 'afterCommandExec', {
  36. name: commandName,
  37. command: cmd,
  38. returnValue: !!data
  39. } );
  40. } );
  41. }
  42. } );
  43. // Register the toolbar button.
  44. editor.ui.addButton && editor.ui.addButton( 'PasteFromWord', {
  45. label: editor.lang.pastefromword.toolbar,
  46. command: commandName,
  47. toolbar: 'clipboard,50'
  48. } );
  49. editor.on( 'pasteState', function( evt ) {
  50. editor.getCommand( commandName ).setState( evt.data );
  51. } );
  52. // Features bring by this command beside the normal process:
  53. // 1. No more bothering of user about the clean-up.
  54. // 2. Perform the clean-up even if content is not from MS-Word.
  55. // (e.g. from a MS-Word similar application.)
  56. // 3. Listen with high priority (3), so clean up is done before content
  57. // type sniffing (priority = 6).
  58. editor.on( 'paste', function( evt ) {
  59. var data = evt.data,
  60. mswordHtml = data.dataValue;
  61. // MS-WORD format sniffing.
  62. if ( mswordHtml && ( forceFromWord || ( /(class=\"?Mso|style=\"[^\"]*\bmso\-|w:WordDocument)/ ).test( mswordHtml ) ) ) {
  63. // Do not apply paste filter to data filtered by the Word filter (#13093).
  64. data.dontFilter = true;
  65. // If filter rules aren't loaded then cancel 'paste' event,
  66. // load them and when they'll get loaded fire new paste event
  67. // for which data will be filtered in second execution of
  68. // this listener.
  69. var isLazyLoad = loadFilterRules( editor, path, function() {
  70. // Event continuation with the original data.
  71. if ( isLazyLoad )
  72. editor.fire( 'paste', data );
  73. else if ( !editor.config.pasteFromWordPromptCleanup || ( forceFromWord || confirm( editor.lang.pastefromword.confirmCleanup ) ) ) // jshint ignore:line
  74. data.dataValue = CKEDITOR.cleanWord( mswordHtml, editor );
  75. // Reset forceFromWord.
  76. forceFromWord = 0;
  77. } );
  78. // The cleanup rules are to be loaded, we should just cancel
  79. // this event.
  80. isLazyLoad && evt.cancel();
  81. }
  82. }, null, null, 3 );
  83. }
  84. } );
  85. function loadFilterRules( editor, path, callback ) {
  86. var isLoaded = CKEDITOR.cleanWord;
  87. if ( isLoaded )
  88. callback();
  89. else {
  90. var filterFilePath = CKEDITOR.getUrl( editor.config.pasteFromWordCleanupFile || ( path + 'filter/default.js' ) );
  91. // Load with busy indicator.
  92. CKEDITOR.scriptLoader.load( filterFilePath, callback, null, true );
  93. }
  94. return !isLoaded;
  95. }
  96. function forceHtmlMode( evt ) {
  97. evt.data.type = 'html';
  98. }
  99. } )();
  100. /**
  101. * Whether to prompt the user about the clean up of content being pasted from MS Word.
  102. *
  103. * config.pasteFromWordPromptCleanup = true;
  104. *
  105. * @since 3.1
  106. * @cfg {Boolean} [pasteFromWordPromptCleanup=false]
  107. * @member CKEDITOR.config
  108. */
  109. /**
  110. * The file that provides the MS Word cleanup function for pasting operations.
  111. *
  112. * **Note:** This is a global configuration shared by all editor instances present
  113. * in the page.
  114. *
  115. * // Load from 'pastefromword' plugin 'filter' sub folder (custom.js file) using path relative to CKEditor installation folder.
  116. * CKEDITOR.config.pasteFromWordCleanupFile = 'plugins/pastefromword/filter/custom.js';
  117. *
  118. * // Load from 'pastefromword' plugin 'filter' sub folder (custom.js file) using full path (including CKEditor installation folder).
  119. * CKEDITOR.config.pasteFromWordCleanupFile = '/ckeditor/plugins/pastefromword/filter/custom.js';
  120. *
  121. * // Load custom.js file from 'customFilerts' folder (located in server's root) using full URL.
  122. * CKEDITOR.config.pasteFromWordCleanupFile = 'http://my.example.com/customFilerts/custom.js';
  123. *
  124. * @since 3.1
  125. * @cfg {String} [pasteFromWordCleanupFile=<plugin path> + 'filter/default.js']
  126. * @member CKEDITOR.config
  127. */