如何在ckeditor 4.x中添加自定义快捷方式?

时间:2017-06-08 10:34:28

标签: ckeditor shortcut-key

在Ckeditor中我想创建自定义快捷键来改变字体类型(按快捷方式键可以改变字体)  需要你的帮助

1 个答案:

答案 0 :(得分:1)

为此它需要一个自定义插件,我已经创建了这样的自定义插件 通过创建插件名称(keystorke)添加此代码并将其添加到插件文件夹

CKEDITOR.plugins.add( 'keystrokes', {
        init: function( editor ) {
            editor.addCommand( 'h1', {
                exec: function( editor ) {
                    var format = { element: 'h1' };
                    var style = new CKEDITOR.style(format);
                    style.apply(editor.document);
                    var selectedText = editor.getSelection().getNative();
                }
            } );
            editor.addCommand( 'h2', {
                exec: function( editor ) {
                    var format = { element: 'h2' };
                    var style = new CKEDITOR.style(format);
                    style.apply(editor.document);
                }
            } );
            editor.addCommand( 'h3', {
                exec: function( editor ) {
                    var format = { element: 'h3' };
                    var style = new CKEDITOR.style(format);
                    style.apply(editor.document);
                }
            } );
            editor.addCommand( 'h4', {
                exec: function( editor ) {
                    var format = { element: 'h4' };
                    var style = new CKEDITOR.style(format);
                    style.apply(editor.document);
                }
            } );
            editor.addCommand( 'h5', {
                exec: function( editor ) {
                    var format = { element: 'h5' };
                    var style = new CKEDITOR.style(format);
                    style.apply(editor.document);
                }
            } );
            editor.addCommand( 'h6', {
                exec: function( editor ) {
                    var format = { element: 'h6' };
                    var style = new CKEDITOR.style(format);
                    style.apply(editor.document);
                }
            } );
            editor.addCommand( 'p', {
                exec: function( editor ) {
                    var format = { element: 'p' };
                    var style = new CKEDITOR.style(format);
                    style.apply(editor.document);
                }
            } );



            editor.setKeystroke( CKEDITOR.ALT + 49 , 'h1' ); // ALT + 1
            editor.setKeystroke( CKEDITOR.ALT + 50 , 'h2' ); // ALT + 2
            editor.setKeystroke( CKEDITOR.ALT + 51 , 'h3' ); // ALT + 3
            editor.setKeystroke( CKEDITOR.ALT + 52 , 'h4' ); // ALT + 4
            editor.setKeystroke( CKEDITOR.ALT + 53 , 'h5' ); // ALT + 5
            editor.setKeystroke( CKEDITOR.ALT + 54 , 'h6' ); // ALT + 6
            editor.setKeystroke( CKEDITOR.ALT + 55 , 'p' ); // ALT + 7




        }
    });

2现在您需要在config.js文件中添加一行,如下所示   config.extraPlugins ='keystrokes';

3现在它将起作用