How to force paste from word in CKEditor 4

This post deals with forcing CKEditor 4 into pasting from word on every CTRL+V directly in the editor’s textarea.

For this fix to work, one has to get a bit into CKEditor’s code and modify the pastefromword plugin.

What we are going to do is add a forcePasteFromWord configuration option to the default configuration options you might set in config.js (as in config.forcePasteFromWord = true;)

So here are the steps:

1- Import CKEditor’s project code from github at https://github.com/ckeditor/ckeditor-dev (preferably import the stable branch).

2- Open plugins/pastefromword/plugin.js

3- Modify plugin.js (basically my change is around line 51 – under the editor.ui.addButton code) add the following:

if(editor.config.forcePasteFromWord && !editor.config.forcePasteAsPlainText){
forceFromWord = 1;
}

Please note that the above code checks if forcePasteAsPlainText has NOT been set, if it has the forcePasteAsPlainText takes over.

In order to force paste from word regardless of the state of forcePasteAsPlainText just modify the if statement to only include the check on forcePasteFromWord as such:

if(editor.config.forcePasteFromWord){
forceFromWord = 1;
}

What the above basically does is to check if the config.forcePasteFromWord has been set in your config.js (the one under ckeditor’s root directory), and modifies the forceFromWord variable to be true (1 is true in boolean) and then when the paste event occurs

editor.on( 'paste', function( evt ) {

the following check is true and the ms-word filtering is applied:

// MS-WORD format sniffing.
if ( mswordHtml && ( forceFromWord || ( /(class=\"?Mso|style=\"[^\"]*\bmso\-|w:WordDocument)/ ).test( mswordHtml ) ) ) 

4-Now we just need to re-build ckeditor’s source code and generate a new ckeditor.js that holds the modified code.

For this you’ll have to follow the following official documentation article:http://docs.ckeditor.com/#!/guide/dev_build

5- After building, you’ll have to configure ckeditor’s config.js to forcePasteFromWord as such:
CKEDITOR.editorConfig = function( config ) {
// Define changes to default configuration here. For example:
// config.language = 'fr';
// config.uiColor = '#AADC6E';
// %REMOVE_START%
config.forcePasteFromWord=true;
};

Hope this was helpful.
Regards

Advertisements

2 thoughts on “How to force paste from word in CKEditor 4

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s