javascript 在CKEditor 5中创建不可编辑的文本块

wgmfuz8q  于 2023-01-11  发布在  Java
关注(0)|答案(3)|浏览(285)

如何在CKEditor 5中创建一个不可编辑的文本块。
我需要在最终视图中生成的东西:

<div>Non editable message here</div>

我尝试使用UIElement,然后设置innerHTML,但这仍然使元素可编辑。
仅供参考:她是我最终使用的插件.

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'
            } ) );
    }
}
r1zhe5dt

r1zhe5dt1#

您可以为此创建widget API

import Widget from '@ckeditor/ckeditor5-widget/src/widget';
import { toWidget } from '@ckeditor/ckeditor5-widget/src/utils';
import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        plugins: [ Essentials, Paragraph, Widget, /* ... other plugins ... */ ],
        toolbar: [ 'undo', 'redo' ]
    } )
    .then( editor => {
        window.editor = editor;

        const model = editor.model;

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

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

        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'
            } ) );

        editor.setData(
            '<p>foobar</p>' +
            '<div class="widget">Non editable text goes here</div>' +
            '<p>foobar</p>'
        );
    } )
    .catch( err => {
        console.error( err.stack );
    } );

关键点是在将元素向下转换到编辑视图时使用toWidget()函数,而且该元素应该在模式中标记为对象。
另外,不要忘记加载Widget插件,它打开了对小部件的支持。

mm9b1k5b

mm9b1k5b2#

昨天我遇到了一个问题,当isObject小部件内容中有一个html表时,该表仍然是可编辑的,并且也有控件。
唯一的解决方法是通过css禁用小部件内部的交互。

.ck-widget.my-wdiget *:not(.ck.ck-reset_all.ck-widget__type-around) {
    pointer-events: none;
}

.ck.ck-reset_all.ck-widget__type-around是widget控件的类,用于在widget之前或之后添加换行符,仍然可用。

eufgjt7s

eufgjt7s3#

根据文档isReadOnly : Boolean将其设置为true。官方文档

相关问题