如何在CKEditor5中显示自定义HTML元素?

huangapple go评论50阅读模式
英文:

How to show custom HTML element in CKEditor5?

问题

我们正在升级一个使用CKEditor4的传统系统到CKEditor5。在编辑器的内容中,包含类似以下的内容:

<whatsapp>
This is pretext for whatsapp
</whatsapp>

我们希望在编辑器正文中显示整个内容,包括 <whatsapp></whatsapp>

这在CKEditor4中工作正常,但在CKEditor5中显示为空白。

下面是使用的CKEditor5配置。

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
        <style>
            #container {
                width: 1000px;
                margin: 20px auto;
            }
            .ck-editor__editable[role="textbox"] {
                /* 编辑区域 */
                min-height: 200px;
            }
            .ck-content .image {
                /* 块级图片 */
                max-width: 80%;
                margin: 20px auto;
            }
        </style>
        <div id="container">
            <div id="editor">
            </div>
        </div>
        <script src="https://cdn.ckeditor.com/ckeditor5/36.0.1/super-build/ckeditor.js"></script>
        <script>
            CKEDITOR.ClassicEditor.create(document.getElementById("editor"), {
                toolbar: {
                    items: [
                        'sourceEditing'
                    ],
                    shouldNotGroupWhenFull: true
                },
                htmlSupport: {
                    allow: [
                        {
                            name: /.*/,
                            attributes: true,
                            classes: true,
                            styles: true
                        }
                    ]
                },
                removePlugins: [
                    'CKBox',
                    'CKFinder',
                    'EasyImage',
                    'RealTimeCollaborativeComments',
                    'RealTimeCollaborativeTrackChanges',
                    'RealTimeCollaborativeRevisionHistory',
                    'PresenceList',
                    'Comments',
                    'TrackChanges',
                    'TrackChangesData',
                    'RevisionHistory',
                    'Pagination',
                    'WProofreader',
                    'MathType'
                ]
            }).then(editor => {
                let mydata = '<div>hello world</div><whatsapp>hi whatsapp</whatsapp><wechat>hey wechat</wechat><div>bye world</div>';
                editor.setData(mydata)
            });
        </script>
    </body>
</html>
英文:

We are upgrading a legacy system that uses CKEditor4 to CKEditor5. In one of the editor's content, it contains something like this:

&lt;whatsapp&gt;
This is pretext for whatsapp
&lt;/whatsapp&gt;

We would like to show the entire content, including the &lt;whatsapp&gt; and &lt;/whatsapp&gt; in the editor body.

This works fine in CKEditor4 but is displayed as blank in CKEditor5.

The CKEditor5 config used is shown below.

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;
&lt;!-- language: lang-html --&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;style&gt;
#container {
width: 1000px;
margin: 20px auto;
}
.ck-editor__editable[role=&quot;textbox&quot;] {
/* editing area */
min-height: 200px;
}
.ck-content .image {
/* block images */
max-width: 80%;
margin: 20px auto;
}
&lt;/style&gt;
&lt;div id=&quot;container&quot;&gt;
&lt;div id=&quot;editor&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;script src=&quot;https://cdn.ckeditor.com/ckeditor5/36.0.1/super-build/ckeditor.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
CKEDITOR.ClassicEditor.create(document.getElementById(&quot;editor&quot;), {
toolbar: {
items: [
&#39;sourceEditing&#39;
],
shouldNotGroupWhenFull: true
},
htmlSupport: {
allow: [
{
name: /.*/,
attributes: true,
classes: true,
styles: true
}
]
},
removePlugins: [
&#39;CKBox&#39;,
&#39;CKFinder&#39;,
&#39;EasyImage&#39;,
&#39;RealTimeCollaborativeComments&#39;,
&#39;RealTimeCollaborativeTrackChanges&#39;,
&#39;RealTimeCollaborativeRevisionHistory&#39;,
&#39;PresenceList&#39;,
&#39;Comments&#39;,
&#39;TrackChanges&#39;,
&#39;TrackChangesData&#39;,
&#39;RevisionHistory&#39;,
&#39;Pagination&#39;,
&#39;WProofreader&#39;,
&#39;MathType&#39;
]
}).then(editor =&gt; {
let mydata = &#39;&lt;div&gt;hello world&lt;/div&gt;&lt;whatsapp&gt;hi whatsapp&lt;/whatsapp&gt;&lt;wechat&gt;hey wechat&lt;/wechat&gt;&lt;div&gt;bye world&lt;/div&gt;&#39;;
editor.setData(mydata)
});
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;!-- end snippet --&gt;

答案1

得分: 0

  1. 有两种方法可以实现这一点。
  2. 通过插件创建向上转型和向下转型转换。
  3. 使用 通用 HTML 支持

要创建自定义插件,您需要通过 npmwebpack 构建编辑器。以下是一个非常简单的示例,说明如何实现这一点:

import Plugin from '@ckeditor/ckeditor5-core/src/plugin';

class WhatsappPlugin extends Plugin {
    static elementName = 'whatsapp';

    init() {
        registerSchemaAndConversion(this.editor, WhatsappPlugin.elementName);
    }
}

function registerSchemaAndConversion(editor, elementName) {
    if (!editor.model.schema.isRegistered(elementName)) {
        editor.model.schema.register(elementName, {
            inheritAllFrom: '$block',
        });
    }

    editor.conversion.for('downcast').elementToElement({
        model: elementName,
        view: elementName,
    });

    editor.conversion.for('upcast').elementToElement({
        view: {
            name: elementName,
        },
        model: elementName,
        converterPriority: 'low',
    });
}

class Editor extends ClassicEditor {}

Editor.defaultConfig = {
    plugins: [WhatsappPlugin, ...Editor.builtinPlugins],
}

export default Editor;
英文:

There're two ways to achieve this.

  1. Creates the upcast and downcast conversion via Plugin
  2. Uses the general HTML support

To create custom plugin, you will need to build the editor via the npm and webpack. Below shows a very simple example how this can be achieved

import Plugin from &#39;@ckeditor/ckeditor5-core/src/plugin&#39;;

class WhatsappPlugin extends Plugin {
    static elementName = &#39;whatsapp&#39;;

    init() {
        registerSchemaAndConversion(this.editor, WhatsappPlugin.elementName);
    }
}

function registerSchemaAndConversion(editor, elementName) {
    if (!editor.model.schema.isRegistered(elementName)) {
        editor.model.schema.register(elementName, {
            inheritAllFrom: &#39;$block&#39;,
        });
    }

    editor.conversion.for(&#39;downcast&#39;).elementToElement({
        model: elementName,
        view: elementName,
    });

    editor.conversion.for(&#39;upcast&#39;).elementToElement({
        view: {
            name: elementName,
        },
        model: elementName,
        converterPriority: &#39;low&#39;,
    });
}

class Editor extends ClassicEditor {}

Editor.defaultConfig = {
    plugins: [WhatsappPlugin, ...Editor.builtinPlugins],
}

export default Editor;

huangapple
  • 本文由 发表于 2023年4月4日 10:24:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定