首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CKeditor5 -插件-小部件状态

CKeditor5 -插件-小部件状态
EN

Stack Overflow用户
提问于 2019-03-06 13:39:20
回答 1查看 672关注 0票数 0

我正在为CK编辑器5编写一个插件。在第4版中,我添加了如下HTML:

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

我需要PHP中的数据id值来完成我的工作。但我不知道如何在CKeditor 5中实现这一点。

CKeditor 5的工作方式不同。它非常漂亮,但我仍然无法找到如何用附加状态( data-id属性)添加这样的小部件。

我试过:

代码语言:javascript
复制
const viewFragment = editor.data.processor.toView(html);
const modelFragment = editor.data.toModel(viewFragment);
editor.model.insertContent( modelFragment );

我最基本的转换代码:

代码语言:javascript
复制
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
}) );

我真的可以t find out how to manage this. This code tries to add:我的标签but now it still adds:我的标签

正如您将看到的,它实际上添加了:<span class="my-widget" test="test">My label</span>,因为dataDowncast代码,但是如何从insertContent代码部分获得我的状态?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-07 09:26:06

以下是几条简短的建议:

  1. 这是不正确的:allowWhere: [ '$block', '$text']。不能有一个元素表现得像块和文本。你需要选择一个-它要么是内嵌的,要么是块的。
  2. 看到您使用<span>,我想“内联”是一个更好的选择。在CKEditor 5 v12.0.0 (几天前发布)中,我们引入了对内联小部件的支持。请查看实现内联小部件指南,它涵盖了您需要了解的几件事--特别是模式。
  3. 在上播转换器中有view.class。这是不正确的-只有view.classes。此外,如果要匹配多个类,则需要使用数组。见upcasthelpers-UpcastHelpers.html#function-elementToElementmatcher-MatcherPattern.html
  4. 在CKEditor v12.0.0中,downcastElementToElement()upcastElementToElement()方法被移到文档中不同的位置。在迁移指南中阅读更多内容。

最后,如何向上转换data-id并存储在模型中的解决方案是,上播转换器需要将其设置在模型元素上。查看UpcastHelpers#elementToElement()中的这个示例

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

如您所见,model部件可以是一个回调,返回一个模型元素。它可以访问视图元素,这样就可以读取所需的属性。

您还可以分别使用UpcastHelpers#elementToElement()UpcastHelpers#attributeToAttribute()进行相同的操作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55024472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档