简体   繁体   English

CKeditor5-插件-小部件-状态

[英]CKeditor5 - plugin - widget - state

I am (re)writing a plugin for CK editor 5. In version 4 i added HTML like this: 我正在(重新)为CK编辑器5编写一个插件。在版本4中,我添加了如下HTML:

<span data-id="my-special-value" class="my-widget">My label</span>

I need the data-id value in PHP to do my stuff. 我需要PHP中的data-id值来完成我的工作。 But i can not figure out how to accomplish this in CKeditor 5. 但是我不知道如何在CKeditor 5中完成此操作。

CKeditor 5 works differently. CKeditor 5的工作原理有所不同。 It is very beautiful, but i still can`t find out how to add a widget like this with additional state (the data-id attribute). 它非常漂亮,但是我仍然找不到如何添加具有这样的状态(data-id属性)的小部件。

I tried: 我试过了:

const viewFragment = editor.data.processor.toView(html);
const modelFragment = editor.data.toModel(viewFragment);
editor.model.insertContent( modelFragment );

My very basic conversioncode: 我非常基本的转换代码:

model.schema.register(pluginSlug, {
    isBlock: false,
    isObject: true,
    allowContentOf: '$block',
    allowWhere: [ '$block', '$text'],
});

// Retrieving the data from the editor.
editor.conversion.for('dataDowncast').add( downcastElementToElement( {
    model: pluginSlug,
    view: (modelItem, writer) => {
        const element = writer.createContainerElement( 'span', { class: 'widget form-element-wysiwyg', test: "test" });

        return element;
    }
}) );

// Rendering the editor content to the user for editing.
editor.conversion.for('editingDowncast').add( downcastElementToElement( {
    model: pluginSlug,
    view: (modelItem, writer) => {
        const element = writer.createContainerElement('span', { class: 'widget form-element-wysiwyg', test: "test" });
        const widget = toWidget( element, writer, { label: 'Target Label' });

        return widget;
    }
}) );

// Loading the data to the editor.
editor.conversion.for('upcast').add( upcastElementToElement( {
    view: {
        name: 'span',
        class: 'widget form-element-wysiwyg'
    },
    model: pluginSlug
}) );

I really can`t find out how to manage this. 我真的找不到解决办法。 This code tries to add: <span data-id="my-special-value" class="my-widget">My label</span> but now it still adds: <span class="my-widget">My label</span> 该代码尝试添加: <span data-id="my-special-value" class="my-widget">My label</span>但现在它仍然添加: <span class="my-widget">My label</span> <span data-id="my-special-value" class="my-widget">My label</span> <span class="my-widget">My label</span>

As you will see, it actually adds: <span class="my-widget" test="test">My label</span> because of the dataDowncast code, but how to get my state from the insertContent code part? 正如您将看到的,由于dataDowncast代码,它实际上添加了: <span class="my-widget" test="test">My label</span> ,但是如何从insertContent代码部分获取我的状态?

A couple of quick advices: 几个快速的建议:

  1. This is incorrect: allowWhere: [ '$block', '$text'] . 这是不正确的: allowWhere: [ '$block', '$text'] You can't have a single element behaving like a block and like a text. 您不能让单个元素的行为像块和文本一样。 You need to choose one – it's either inline or block. 您需要选择一个-它是内联的还是块的。
  2. Seeing that you use <span> I guess "inline" is a better choice. 看到您使用<span>我想“内联”是一个更好的选择。 In CKEditor 5 v12.0.0 (released a couple of days ago) we introduced support for inline widgets. 在CKEditor 5 v12.0.0(几天前发布)中,我们引入了对嵌入式小部件的支持。 Check out the implementing inline widget guide which covers a couple of things that you will need to know – especially the schema. 查看实现内联窗口小部件指南 ,该指南涵盖了您需要了解的几件事,尤其是架构。
  3. In the upcast converter you have view.class . view.class转换器中,您具有view.class This is incorrect – there's only view.classes . 这是不正确的–只有view.classes Plus, if you want to match against multiple classes you need to use an array. 另外,如果要与多个类匹配,则需要使用数组。 See https://ckeditor.com/docs/ckeditor5/latest/api/module_engine_conversion_upcasthelpers-UpcastHelpers.html#function-elementToElement and https://ckeditor.com/docs/ckeditor5/latest/api/module_engine_view_matcher-MatcherPattern.html 参见https://ckeditor.com/docs/ckeditor5/latest/api/module_engine_conversion_upcasthelpers-UpcastHelpers.html#function-elementToElementhttps://ckeditor.com/docs/ckeditor5/latest/api/module_engine_view_matcher-MatcherPattern.html
  4. In CKEditor v12.0.0 the downcastElementToElement() and upcastElementToElement() methods were moved to a different place in the document. 在CKEditor v12.0.0中, downcastElementToElement()upcastElementToElement()方法已移动到文档中的其他位置。 Read more in the migration guide . 迁移指南中了解更多信息。

Finally, the solution to how to upcast data-id and store in the model is that the upcast converter needs to set it on the model element. 最后,如何上载data-id并将其存储在模型中的解决方案是上载转换器需要在模型元素上进行设置。 Check out this example from UpcastHelpers#elementToElement() : UpcastHelpers#elementToElement()查看以下示例:

editor.conversion.for( 'upcast' ).elementToElement( {
    view: {
        name: 'p',
        classes: 'heading'
    },
    model: ( viewElement, modelWriter ) => {
        return modelWriter.createElement( 'heading', { level: viewElement.getAttribute( 'data-level' ) } );
    }
} );

As you can see, the model part can be a callback returning a model element. 如您所见, model部分可以是返回模型元素的回调。 It has access to the view element so you can read the attributes you need. 它可以访问view元素,因此您可以读取所需的属性。

You can also do the same thing using UpcastHelpers#elementToElement() and then UpcastHelpers#attributeToAttribute() separately. 您还可以分别使用UpcastHelpers#elementToElement()UpcastHelpers#attributeToAttribute()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM