2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
7 function forceHtmlMode( evt
) { evt
.data
.mode
= 'html'; }
9 CKEDITOR
.plugins
.add( 'pastefromword',
11 init : function( editor
)
14 // Flag indicate this command is actually been asked instead of a generic
16 var forceFromWord
= 0;
17 var resetFromWord = function( evt
)
19 evt
&& evt
.removeListener();
20 editor
.removeListener( 'beforePaste', forceHtmlMode
);
21 forceFromWord
&& setTimeout( function() { forceFromWord
= 0; }, 0 );
24 // Features bring by this command beside the normal process:
25 // 1. No more bothering of user about the clean-up.
26 // 2. Perform the clean-up even if content is not from MS-Word.
27 // (e.g. from a MS-Word similar application.)
28 editor
.addCommand( 'pastefromword',
33 // Ensure the received data format is HTML and apply content filtering. (#6718)
35 editor
.on( 'beforePaste', forceHtmlMode
);
37 if ( editor
.execCommand( 'paste', 'html' ) === false )
39 editor
.on( 'dialogShow', function ( evt
)
42 evt
.data
.on( 'cancel', resetFromWord
);
45 editor
.on( 'dialogHide', function( evt
)
47 evt
.data
.removeListener( 'cancel', resetFromWord
);
51 editor
.on( 'afterPaste', resetFromWord
);
55 // Register the toolbar button.
56 editor
.ui
.addButton( 'PasteFromWord',
58 label
: editor
.lang
.pastefromword
.toolbar
,
59 command
: 'pastefromword'
62 editor
.on( 'pasteState', function( evt
)
64 editor
.getCommand( 'pastefromword' ).setState( evt
.data
);
67 editor
.on( 'paste', function( evt
)
72 // MS-WORD format sniffing.
73 if ( ( mswordHtml
= data
[ 'html' ] )
74 && ( forceFromWord
|| ( /(class=\"?Mso|style=\"[^\"]*\bmso\-|w:WordDocument)/ ).test( mswordHtml
) ) )
76 var isLazyLoad
= this.loadFilterRules( function()
78 // Event continuation with the original data.
80 editor
.fire( 'paste', data
);
81 else if ( !editor
.config
.pasteFromWordPromptCleanup
82 || ( forceFromWord
|| confirm( editor
.lang
.pastefromword
.confirmCleanup
) ) )
84 data
[ 'html' ] = CKEDITOR
.cleanWord( mswordHtml
, editor
);
88 // The cleanup rules are to be loaded, we should just cancel
90 isLazyLoad
&& evt
.cancel();
95 loadFilterRules : function( callback
)
98 var isLoaded
= CKEDITOR
.cleanWord
;
104 var filterFilePath
= CKEDITOR
.getUrl(
105 CKEDITOR
.config
.pasteFromWordCleanupFile
106 || ( this.path
+ 'filter/default.js' ) );
108 // Load with busy indicator.
109 CKEDITOR
.scriptLoader
.load( filterFilePath
, callback
, null, true );
115 requires
: [ 'clipboard' ]
120 * Whether to prompt the user about the clean up of content being pasted from
122 * @name CKEDITOR.config.pasteFromWordPromptCleanup
127 * config.pasteFromWordPromptCleanup = true;
131 * The file that provides the MS Word cleanup function for pasting operations.
132 * Note: This is a global configuration shared by all editor instances present
134 * @name CKEDITOR.config.pasteFromWordCleanupFile
139 * // Load from 'pastefromword' plugin 'filter' sub folder (custom.js file).
140 * CKEDITOR.config.pasteFromWordCleanupFile = 'custom';