Symfony 5 & FOSCKEDITOR : 创建一个简单的自定义插件,这个插件的图标不会出现在 Ckeditor 工具栏中

马修·洛佩兹

从 Symfony 5 网站,我安装了有用的包 fosckeditor(CKEDITOR 版本 4)。

一切正常,我的页面上有 CKEDITOR 字段。现在我想创建一个新的简单插件。

我严格按照这个官方指南,并在<symfony_root_dir>/public/bundle/fosckeditor/plugins/名为“时间戳”的一些文件中创建了一个新插件

在此处输入图片说明

在 中plugin.js,我添加了以下代码:

CKEDITOR.plugins.add( 'timestamp', {
    icons: 'timestamp',
    init: function( editor ) {
        alert('hello test ?'); // this alert appears when I load the page containing the CKEDITOR
        editor.addCommand('insertTimestamp', {
            exec: function (editor) {
                var now = new Date();
                editor.insertHtml('The current date and time is: <em>' + now.toString() + '</em>');
            }
        });
        editor.ui.addButton('timestamp', {
            label: 'Insert Timestamp',
            command: 'insertTimestamp',
            toolbar: 'insert'
        })
    }
});

而且,在 中<symfony_root_dir>/public/bundle/fosckeditor/config.js,我补充说:

CKEDITOR.editorConfig = function( config ) {
    config.extraPlugins = ['timestamp'];
    // same result if instead I add the custom plugin via a string : config.extraPlugins = 'timestamp';
};

对于这个简单的例子,我从另一个插件复制/粘贴了一个图标,这是时间戳图标文件:

在此处输入图片说明

最后,我重新加载我的页面(重新加载 + 清除缓存)。但是Ckeditor工具栏没有变化,自定义插件无处可见。

在此处输入图片说明

我尝试在fos_ckeditor.yaml文件中添加按钮,如下所示:

# ...
fos_ck_editor:
    # ...
    default_config: main_config
    configs:
        main_config:
            # ...
            toolbar:
                - {
                    items:
                      ['timestamp']
                }
    styles:
        # ...

但是我的自定义插件的按钮在 CKEditor 工具栏中一直丢失。我在浏览器控制台中没有 javascript 错误,我不明白我在哪里犯了错误。

阿泽姆

试试这个配置:

应用程序/模板/ckeditor.html.twig

{% extends "base.html.twig" %}

{% block body %}

    <div class="ckeditor">

        {{ form_start(form) }}
            {{ form_row( form.content ) }}
        {{ form_end(form) }}

    </div>

{% endblock %}

app/src/Controller/TestController.php

<?php

namespace App\Controller;

use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use FOS\CKEditorBundle\Form\Type\CKEditorType;

class TestController extends AbstractController
{
    public function index()
    {
        $form = $this->createFormBuilder()
                    ->add('content', CKEditorType::class)
                    ->getForm();

        return $this->render('ckeditor.html.twig', [
            'form' => $form->createView()
        ]);
    }
}

app/public/bundles/fosckeditor/plugins/timestamp/plugin.js

CKEDITOR.plugins.add( 'timestamp', {
    init: function(editor) {
        editor.addCommand('insertTimestamp', {
            exec: function (editor) {
                var now = new Date();
                editor.insertHtml('The current date and time is: <em>' + now.toString() + '</em>');
            }
        });

        editor.ui.addButton('timestamp', {
            label: 'Insert Timestamp',
            command: 'insertTimestamp',
            toolbar: 'mode,0',                 // toolbar group and index
            icon: this.path + 'timestamp.png'  // icon file (PNG)
        });
    }
})

app/config/packages/fos_ckeditor.yaml

twig:
    form_themes:
        - '@FOSCKEditor/Form/ckeditor_widget.html.twig'

fos_ck_editor:
    default_config: test_config

    configs:
        test_config:
            extraPlugins: ["timestamp"]

    plugins:
        timestamp:
            path:     "bundles/fosckeditor/plugins/timestamp/"
            filename: "plugin.js"

截屏:

CKEditor 插件截图

插件目录结构:

应用程序/公共/捆绑包/fosckeditor/插件/

timestamp/
├── plugin.js
└── timestamp.png

相关链接:

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章