Reputation: 3757
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
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
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">	</span>';
var tabElement = CKEDITOR.dom.element.createFromHtml(tabHtml, editor.document);
editor.insertElement(tabElement);
ev.cancel();
}
});
Upvotes: 0
Reputation: 3757
this.editorInstance.on( 'tab', function(evt){
evt.editor.insertHtml('span style="margin-left: 40px;"> </span>');
evt.cancel();
return false;
})
Upvotes: 2