拦截 CKEditor 击键

发布于 2024-08-31 03:08:58 字数 64 浏览 2 评论 0原文

我可以拦截 CKEditor 中的击键(tab 键)并替换默认行为吗?我想要 Tab 键插入一个带边距的 div。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笔落惊风雨 2024-09-07 03:08:58
this.editorInstance.on( 'tab', function(evt){

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

    evt.cancel();
    return false;
})
this.editorInstance.on( 'tab', function(evt){

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

    evt.cancel();
    return false;
})
[浮城] 2024-09-07 03:08:58

我使用的是4.4.7版本。至少在这里,只需编辑 config.js 就可以更改 TAB 按键的行为。使用此代码 TAB 缩进和 SHIFT + TAB 减少缩进:

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

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' ]
];
你又不是我 2024-09-07 03:08:58

我用稍微不同的方式解决了这个问题。我希望选项卡在所有行上对齐,而不是插入固定宽度的跨度。因此,我插入一个具有“预”格式的制表符(& # 0 9)。我在使用 insertHtml() 时也遇到了困难,不得不使用 createFromHtml() 和 insertElement() 的组合。

这是我的解决方案:

// 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();
    }
});

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();
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文