富文本编辑器CKEditor4简单使用-05(开发自定义插件入门)

1. CKEditor4插件入门

1.1 关于CKEditor4插件的简单安装与使用

1.2 参考

2. 开发自定义插件——当前时间插件

2.1 创建插件文件目录结构

  • 在plugins目录下创建自定义插件文件夹 susutimestamp,以及其目录结构如下:
    在这里插入图片描述
  • 注意:
    • 对于 CKEditor 来说,插件文件夹的名称很重要,并且必须与插件的名称相同,否则编辑器将无法识别它。
    • 注意文件目录结构按照上面的来!

2.2 编写插件原代码

2.2.1 编写代码框架

  • 简单说明:
    • 所有 CKEditor 4 插件都是使用 CKEDITOR.plugins.add() 方法创建的。此方法应包含插件名称 — 'susutimestamp' 以及放置在初始化编辑器实例时调用的 init 函数中的插件逻辑
    • 此外,由于我们将定义一个工具栏按钮,因此设置了 icons 该属性,包括图标文件的名称(重要提示:匹配按钮名称,小写!!!)。
  • 框架代码,如下:
    CKEDITOR.plugins.add( 'susutimestamp', {
         
        icons: 'susutimestamp',
        init: function( editor ) {
         
            //Plugin logic goes here.
        }
    });
    

2.2.2 创建编辑器命令

  • 简单说明:
    • CKEditor 4 插件通常定义一个编辑器命令来执行与其关联的操作。该命令应在 init 函数内部定义,以便在初始化 CKEditor 4 实例时创建
    • 在本例中,我们将使用 CKEDITOR.editor.addCommand() 方法来定义将当前日期和时间插入 CKEditor 4 insertMyTimestamp 的命令
  • 实现代码如下:
    在这里插入图片描述

2.2.3 创建插件工具栏按钮

2.2.3.1 简单说明
  • 简化的时间戳插件应该通过工具栏按钮工作。为此,在函数 init 中 ,我们需要定义一个按钮,该按钮将与我们定义的编辑器命令 insertMyTimestamp 关联。
2.2.3.2 关于命名 + 实现代码
  • 如下图:
    在这里插入图片描述
2.2.3.3 关于工具栏组中的位置
  • 请注意,您可以通过提供可选索引来影响按钮在工具栏组中的位置,例如:
    // This could position the button at the beginning of the "insert" group.
    toolbar: 'insert,0'
    
    // This could position the button at the end of the "insert" group.
    toolbar: 'insert,100'
    

2.2.4 完整插件代码

  • 如下:

    CKEDITOR.plugins.add( 'susutimestamp', {
         
        icons: 'susutimestamp',
        init: function( editor ) {
         
            //Plugin logic goes here.
            // 1.创建编辑器命令
            editor.addCommand( 'insertMyTimestamp', {
         
                exec: function( editor ) {
         
                    var now = new Date();
                    editor.insertHtml( '当前时间为: <em>' + now.toString() + '</em>' );
                }
            });
    
            // 2.创建插件工具栏按钮
            editor.ui.addButton( 'susutimestamp', {
         
                label: 'myTimestamp',
                command: 'insertMyTimestamp',
                toolbar: 'insert'
            });
            
        }
    });
    

2.3 启用自定义插件

  • 如下:
    config.extraPlugins = 'susutimestamp';
    

2.4 测试新插件看效果

  • 如下:
    在这里插入图片描述
    在这里插入图片描述

最近更新

  1. TCP协议是安全的吗?

    2024-02-03 18:54:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-03 18:54:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-03 18:54:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-03 18:54:02       18 阅读

热门阅读

  1. mysql b+搜索的算法次数的计算

    2024-02-03 18:54:02       29 阅读
  2. vscode 突然连接不上服务器了

    2024-02-03 18:54:02       30 阅读
  3. 积分、权益、卡卷 三者的理解

    2024-02-03 18:54:02       30 阅读
  4. 如何用Pycharm在本地调用chatgpt的接口

    2024-02-03 18:54:02       33 阅读
  5. eCos flash模拟EEPROM实现NV系统

    2024-02-03 18:54:02       27 阅读
  6. CPP Weekly --C++17

    2024-02-03 18:54:02       28 阅读