在ckeditor wysiwyg中,我们如何更改块引用按钮的操作

时间:2012-11-26 06:06:13

标签: javascript ckeditor wysiwyg

我最终要做的是实现一些简单的代码荧光笔(/ Syntax Highlighter),因为ckEditor中没有可用的东西。出于这个原因,我想通过以下列方式单击所选文本的更改来添加新按钮(或修改其中一个现有按钮): 1-获取单声道空格字体以保留源代码中的缩进(例如“Courier New”) 2-字体颜色变为蓝色

由于我不知道如何添加新按钮,我想使用其中一个现有按钮,比如块引用按钮来完成这项工作。

补充说明: 顺便提一下,我刚注意到ckEditor 4也出来了:ckeditor 4 插件也可以自定义:ckeditor 4 builder

4 个答案:

答案 0 :(得分:1)

WYSIWYG编辑器在后面生成一个HTML标记或编码的HTML来存储您的格式,比如它可能在后面生成此代码的引用:

<blockquote> … </blockquote>

(但这会因编辑器而异)

最简单的方法是找出它在后台生成的标记,并根据需要应用CSS样式。

//.CSS
blockquote{
//Styles here…
}

希望它有所帮助。

答案 1 :(得分:1)

不确定你的完整用例是什么,但这是我的,最后是custom plugin

  1. 我希望能够使用对话框窗口将代码片段插入CKEditor(不确定为什么更喜欢blockquote元素)
  2. 我想在CKEditor中以某种方式在视觉上标记这些代码片段,我不关心在CKEditor中使用真正的语法高亮显示器
  3. 最后,在保存内容之后,我想对插入的元素应用真正的语法格式,而不需要花费太多的工作。我使用了prettify
  4. 随意检查插件并根据需要进行修改。

答案 2 :(得分:1)

您可以考虑我从头开始创建的以下插件(这不是一个完整的解决方案,但它显示了正确的方向):

(function() {
    'use strict';

    CKEDITOR.plugins.add( 'prettify', {
        icons: 'prettify',
        init: function( editor ) {
            editor.on( 'mode', function( event ) {
                if( editor.mode == 'wysiwyg' )
                    insertResources( editor );
            });

            editor.addCommand( 'prettify', {
                exec: function( editor ) {
                    var selection = editor.getSelection(),
                        startElement = selection.getStartElement();

                    if ( startElement.hasClass( 'prettyprint' ) )
                        return;

                    var contents = startElement.getText();

                    // Naive pre-processing.
                    contents = contents.replace( /\n/g ,'<br>' );
                    contents = contents.replace( /\t/g ,'&nbsp;&nbsp;&nbsp;&nbsp;' );
                    contents = contents.replace( / /g ,'&nbsp;' );

                    startElement.setHtml( prettyPrintOne( contents, 'js', 0 ) );
                    startElement.addClass( 'prettyprint' );
                }
            });

            editor.ui.addButton( 'Prettify', {
                label: 'Prettify',
                command: 'prettify',
                toolbar: 'insert'
            });
        }
    });

    function insertResources( editor ) {
        var outerHead = CKEDITOR.document.getHead(),
            innerHead = editor.document.getHead(),
            path = CKEDITOR.plugins.getPath( 'prettify' );

        outerHead.append( CKEDITOR.document.createElement( 'script', {
            attributes: {
                type: 'text/javascript',
                async: 'true',
                src: path + 'lib/prettify.js'
            }
        }));

        innerHead.append( CKEDITOR.document.createElement( 'link', {
            attributes: {
                rel: 'stylesheet',
                href: path + 'lib/prettify.css'
            }
        }));
    }
})();

要运行它,请将Google Prettify库解压缩到lib插件的prettify目录中。然后尝试以下HTML并使用工具栏中的按钮来美化pre

<textarea cols="80" id="editor1" name="editor1" rows="10">
<pre>function insertResources( editor ) {
    var outerHead = CKEDITOR.document.getHead(),
        innerHead = editor.document.getHead(),
        path = CKEDITOR.plugins.getPath( 'prettify' );
</pre>

<p>Foo:</p>

<pre>outerHead.append( CKEDITOR.document.createElement( 'script', {
        attributes: {
            type: 'text/javascript',
            async: 'true',
            src: path + 'lib/prettify.js'
        }
    }));</pre>

<p>Bar:</p>

<pre>innerHead.append( CKEDITOR.document.createElement( 'link', {
        attributes: {
            rel: 'stylesheet',
            href: path + 'lib/prettify.css'
        }
    }));
}</pre>
</textarea>
<script>

    CKEDITOR.replace( 'editor1', {
        extraPlugins: 'prettify',
        toolbar: [
            [ 'Source', '-', 'NewPage', 'Preview', '-', 'Templates' ],
            [ 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo' ],
            [ 'Bold', 'Italic' ],
            '/',
            [ 'Prettify']
        ]
    } );

</script>

答案 3 :(得分:-1)

我刚刚找到了这个超级简单的解决方案:

  1. 打开ckeditor根目录中的contents.css

  2. 添加以下内容:

    blockquote {         颜色:蓝色;         font-family:“Courier New”,                         信使,                         等宽;     }

相关问题