Upperstage
Upperstage

Reputation: 3757

Intercept CKEditor keystroke

Can I intercept a keystroke in CKEditor (the tab key) and replace the default behavior? I want the tab key to insert a div with margin.

Upvotes: 2

Views: 1757

Answers (3)

TCFDS
TCFDS

Reputation: 622

I am using version 4.4.7. At least here it's possible to change behavior of TAB keystroke just by editing config.js. With this code TAB indents and SHIFT + TAB outdents:

config.keystrokes =
[
    [ 09, 'indent' ],
    [ CKEDITOR.SHIFT + 09, 'outdent' ]
];

Upvotes: 1

lance-java
lance-java

Reputation: 27996

I solved this a slightly different way. Instead of inserting a fixed width span, I wanted the tabs to line up over all lines. So, I insert a tab character (& # 0 9) with 'pre' formatting. I also had difficulties with insertHtml() and had to use a combination of createFromHtml() and insertElement() instead.

Here's my solution:

// my editor's id is 'summary'
CKEDITOR.replace('summary', { ... });

var editor = CKEDITOR.instances.summary; 
editor.on('key', function(ev) {
    if (ev.data.keyCode == 9) { // TAB
        var tabHtml = '<span style="white-space:pre">&#09;</span>';
        var tabElement = CKEDITOR.dom.element.createFromHtml(tabHtml, editor.document);
        editor.insertElement(tabElement);
        ev.cancel();
    }
});

Upvotes: 0

Upperstage
Upperstage

Reputation: 3757

this.editorInstance.on( 'tab', function(evt){

    evt.editor.insertHtml('span style="margin-left: 40px;">&nbsp;</span>');

    evt.cancel();
    return false;
})

Upvotes: 2

Related Questions