CKEditor: Change value from config.js at runtime?

Refresh

April 2019

Views

2.5k time

1

I would like to have a switch for CKEditor's feature "forcePasteAsPlainText" that you can enable in config.js:

CKEDITOR.editorConfig = function( config ) {
  config.forcePasteAsPlainText = true;
}


From my website's js-file I now try to change the value for forcePasteAsPlainText with jquery:

 if(typeof(CKEDITOR) !== 'undefined') { 
    $('#filterTextButton').click(function () {
        CKEDITOR.config.forcePasteAsPlainText = !CKEDITOR.config.forcePasteAsPlainText;
        if(CKEDITOR.config.forcePasteAsPlainText) {
            $("#filterSwitch").html("OFF");
        }
        else {
            $("#filterSwitch").html("ON");
        }
    });
}

Problem is that I cannot access CKEDITOR.config.forcePasteAsPlainText. I get an undefined.

Converting the object CKEDITOR.config to a string, I see that there is an object named CKEDITOR.config.plugins holding a parameter called "pastetext" (belonging to forcePasteAsPlainText). But I don't know how to read or set this anew.


I read that one possibility to change the config value is to reinitialize the entire editor by replacing it at runtime... but there must be another solution!?

PS: I read here that you could use the following but this did not work for me:

CKEDITOR.on('instanceReady', function(ev) {
   ev.editor._.commands.paste = ev.editor._.commands.pastetext;
});

1 answers

1

Unfortunately, it isn't possible to modify forcePasteAsPlaintext without reinitializing editor. You can check here http://dev.ckeditor.com/browser/CKEditor/trunk/_source/plugins/pastetext/plugin.js#L56 that this config setting is used only once - at startup.

Alternative solution is to switch off/on forcing paste as plaintext manually. E.g. like this:

// Set to false to switch forcing off.
var force = true;

editor.on( 'beforeCommandExec', function ( evt )
{
     var mode = evt.data.commandData;
     if ( force && evt.data.name == 'paste' && mode != 'html' )
     {
         editor.execCommand( 'pastetext' );
         evt.cancel();
     }
}, null, null, 0 );

editor.on( 'beforePaste', function( evt )
{
    if ( force )
        evt.data.mode = 'text';
});