CKEditor 如何关闭回车键功能

4
我正在尝试使用ckeditor编辑单行文本,因此我不希望用户能够使用回车键。有人知道如何禁用回车键吗?
我尝试了$("#text").keypress(function(event){ alert(event.keyCode); });,但它没有注册成功。谢谢任何帮助。
6个回答

4

以下是我的操作步骤。我创建了一个名为“doNothing”的虚拟插件。只需在“plugins”下创建一个名为“doNothing”的文件夹,然后将以下代码粘贴到插件.js文件中。

(function()
{
    var doNothingCmd =
    {
        exec : function( editor )
        {
            return;
        }
    };
    var pluginName = 'doNothing';
    CKEDITOR.plugins.add( pluginName,
    {
        init : function( editor )
        {
            editor.addCommand( pluginName, doNothingCmd );
        }
    });
})();

将插件添加到config.js文件中:config.extraPlugins = 'doNothing';,然后放入。
config.keystrokes =
[
    [ 13 /*Enter*/, 'doNothing'],
    [ CKEDITOR.ALT + 121 /*F10*/, 'toolbarFocus' ],
    [ CKEDITOR.ALT + 122 /*F11*/, 'elementsPathFocus' ],

    [ CKEDITOR.SHIFT + 121 /*F10*/, 'contextMenu' ],

    [ CKEDITOR.CTRL + 90 /*Z*/, 'undo' ],
    [ CKEDITOR.CTRL + 89 /*Y*/, 'redo' ],
    [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 90 /*Z*/, 'redo' ],

    [ CKEDITOR.CTRL + 76 /*L*/, 'link' ],

    [ CKEDITOR.CTRL + 66 /*B*/, 'bold' ],
    [ CKEDITOR.CTRL + 73 /*I*/, 'italic' ],
    [ CKEDITOR.CTRL + 85 /*U*/, 'underline' ],

    [ CKEDITOR.ALT + 109 /*-*/, 'toolbarCollapse' ]
];

大功告成!现在回车键已经完全不起作用了。你也可以把“doNothing”赋给任何其他你想禁用的按键,虽然我自己还没有尝试过其他的按键。


4

CKEditor 4有一个可用于config对象的 blockedKeystrokes 属性。

以下是禁止回车和shift+回车的示例:

CKEDITOR.inline('someEditorElementId', {
    blockedKeystrokes: [13, CKEDITOR.SHIFT + 13]
});

blockedKeystrokes 在 ckeditor 4.7.3 上对我不起作用,除非我做错了什么。 - Drakkin

2
这有点类似于“技巧”,但我让它正常工作的方法是更改按键配置上发生的事件,我将其设置为“模糊”。
keystrokes:
   [
   [ 13, 'blur'],
   [ CKEDITOR.SHIFT + 13, 'blur' ],
   [ CKEDITOR.CTRL + 90, 'undo' ],
   [ CKEDITOR.CTRL + 89, 'redo' ],
   [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 90, 'redo' ],
   [ CKEDITOR.CTRL + 66, 'bold' ],
   [ CKEDITOR.CTRL + 73, 'italic' ],
   [ CKEDITOR.CTRL + 85 , 'underline' ],
   [ CKEDITOR.ALT + 109, 'toolbarCollapse' ]
   ]

此外,在数据发布之前,我会删除所有段落标签。

0

我无法让插件或被阻止的按键起作用,所以我通过捕获按键事件来处理这个问题。

注意:受到此评论的启发。

editor.on('key', function(e) {
    if (e.data.keyCode === 13 /*enter*/ ||
        e.data.keyCode === 2228237 /*shift + enter*/) {
            e.cancel();
    }
}

0

这也不是一个优雅的解决方案(正则表达式可能太简单了,但我很幸运,因为我不允许源模式)。如果您从键事件中返回false,它只拒绝密钥将是很好的。

    $('#text').ckeditorGet().on('afterCommandExec', function (e) {
    var text = $('#text').ckeditorGet().getData();
    $('#text').ckeditorGet().setData(text.replace(/<\/*p>/i, ''));
    $('#text').ckeditorGet().focus();
});

需要与config.shiftEnterMode结合使用,以CKEDITOR.ENTER_P为例,否则您还需要处理br。聚焦也不完美,因为它会重置光标位置。使用afterCommandExec似乎是在按下回车键后触发的事件,在缺少keyup的情况下(对于类似keyup的东西,请参见{{link1:the onchange plugin}})


0

你面临的问题之一是ckeditor在编辑部分使用了iframe,而事件(如按键事件)不会像普通元素那样从iframe中冒泡出来。

我解决这个问题的方式是连接到ckeditor的按键事件并取消它的回车操作。就像这样:

CKEDITOR.replace($('#myText')[0], //can't be jQuery obj
{ on: {'key': ckeditorKeyPress} }
);

function ckeditorKeyPress(event){
    switch(event.data.keyCode){
      case 13: //enter key
        event.cancel();
        event.stop();
      break;
    }
    //trigger an imitation key event here and it lets you catch the enter key
    //outside the ckeditor
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接