Melissa
Melissa

Reputation: 33

CKEDITOR - Open paste as plain text window by default

I am looking for a way to ensure that I always get plain text if the user pastes text using ctrl+v.

I have tried adding config.forcePasteAsPlainText = true; to my CKEDITOR.editorConfig function and I still get a large amount of nbsp; etc. The only way I seem to be able to get true plain text is using the paste as plain text button on the toolbar. Is there a way to open that window if the user uses ctrl+v to paste and have them paste there. I know it could be done with fckeditor, but have not been able to figure out how to make it work in ckeditor.

Any help would be greatly appreciated! Thanks

Upvotes: 3

Views: 5242

Answers (2)

Dragon Warrior
Dragon Warrior

Reputation: 327

This would paste as plain text even from MS Word

var editor = CKEDITOR.instances.YourInputControlName;
editor.on('paste', function(evt) {
    evt.editor.setData($('<div/>').html(evt.data.dataValue).text());
}, editor.element.$);

Upvotes: 0

Reinmar
Reinmar

Reputation: 22023

I checked the latest SVN version (should differ a lot from 3.6.3) and when I set forcePasteAsPlainText : true (note - you have to do this before initializing editor or in editor.replace( el, config ) - doing this later won't work.

I'm pasting (by ctrl+v) copied part of a website and text is being pasted correctly. No styling, no superfluous and non-plain-text elements.

However, the &nbsp; entities are fully correct in plain text - they replace multiple spaces and you'll have the same result for content pasted by pastetext dialog.

If you still want to open pastetext dialog on ctrl+v you can use CKEDITOR.config.keystrokes. There're couple of defaults in this array, so you shouldn't override them, but you can add your own keystroke (before initializing editor):

CKEDITOR.config.keystrokes.push(
    [ CKEDITOR.CTRL + 86, 'pastetext' ]
);

If you want to add this keystroke only for one editor (because editing global config affects all) then you'll have to use custom config file (see http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Setting_Configurations#Using_the_config.js_File) and add your keystroke this way:

CKEDITOR.editorConfig = function( config )
{
    config.keystrokes = [
        // copy here all keystrokes from _source/plugins/keystrokes/plugin.js
        [ CKEDITOR.CTRL + 86, 'pastetext' ]
    ];
};

This way OFC isn't convenient, but this will be fixed in the next major release of CKEditor.

Upvotes: 6

Related Questions