在CKEditor 5中用文本创建不可编辑的块 [英] Create non editable block with text in CKEditor 5

查看:425
本文介绍了在CKEditor 5中用文本创建不可编辑的块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在CKEditor 5中使用文本创建不可编辑的块.

How do I Create a non editable block with text in CKEditor 5.

我需要在最终视图中生成以下内容的东西:

I need something which in the final view generate a:

<div>Non editable message here</div>

我尝试使用UIElement,然后设置innerHTML,但这仍然使元素可编辑.

I tried to use a UIElement and then set innerHTML but that still makes the element editable.

仅供参考:她是我最终使用的插件.

Just for reference: Her is the plugin I ended up using.

import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import DecoupledEditor from '@ckeditor/ckeditor5-editor-decoupled/src/decouplededitor';
import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
import Heading from '@ckeditor/ckeditor5-basic-styles/src/bold';
import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
import imageIcon from '@ckeditor/ckeditor5-core/theme/icons/image.svg';
import Command from '@ckeditor/ckeditor5-core/src/command';
import { downcastElementToElement,downcastAttributeToAttribute,downcastAttributeToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
import { upcastElementToElement, upcastAttributeToAttribute,modelToViewAttributeConverter} from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
import attachPlaceholder from "@ckeditor/ckeditor5-engine/src/view/placeholder";
import ViewPosition from '@ckeditor/ckeditor5-engine/src/view/position';
import toWidgetEditable from '@ckeditor/ckeditor5-widget/src/utils';
import ClickObserver from '@ckeditor/ckeditor5-engine/src/view/observer/clickobserver';
import UpcastDispatcher from '@ckeditor/ckeditor5-engine/src/conversion/upcastdispatcher'; 
import { toWidget } from '@ckeditor/ckeditor5-widget/src/utils';

export default class TestWidget2 extends Plugin {
    static get pluginName() {
        return 'TestWidget2';
    }

    init() {
        console.log('TestWidget2::init()');
        const editor=this.editor;
        const model=editor.model;

        model.schema.register( 'myWidget', {
            inheritAllFrom: '$block',
            isObject: true
        } );

        editor.conversion.for( 'dataDowncast' )
            .add( downcastElementToElement( {
                model: 'myWidget',
                view: ( modelItem, writer ) => {
                    const elm=writer.createContainerElement( 'div', { class: 'widget' } );
                    return toWidget( div, writer, { label: 'widget label' } );
                }
            } ) );

        editor.conversion.for( 'editingDowncast' )
            .add( downcastElementToElement( {
                model: 'myWidget',
                view: ( modelItem, writer ) => {
                    const div = writer.createContainerElement( 'div', { class: 'widget' } );

                    return toWidget( div, writer, { label: 'widget label' } );
                }
            } ) );

        editor.conversion.for( 'upcast' )
            .add( upcastElementToElement( {
                view: {
                    name: 'div',
                    class: 'widget'
                },
                model: 'myWidget'
            } ) );
    }
}

推荐答案

您可以创建

关键是要使用 <代码将元素向下转换到编辑视图时,> toWidget() 函数.另外,该元素应在也不要忘记加载 Widget 插件,该插件会启用对小部件的支持.

Also don't forget to load the Widget plugin which turns on the support for widgets.

这篇关于在CKEditor 5中用文本创建不可编辑的块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆