Javascript 在CKEditor5中插入带有类和id的span在粘贴时产生错误

Javascript 在CKEditor5中插入带有类和id的span在粘贴时产生错误,javascript,ckeditor5,Javascript,Ckeditor5,我正在开发一个CKEditor5插件,它需要我插入一个带有id和一些类的span 这是我的代码,允许: editor.model.schema.register('span', { inheritAllFrom: '$block', allowIn: ['paragraph'], allowAttributes: ['id', 'class'] }); editor.conversion.elementToElement({model: 'span', view: 'sp

我正在开发一个CKEditor5插件,它需要我插入一个带有id和一些类的span

这是我的代码,允许:

editor.model.schema.register('span', {
    inheritAllFrom: '$block',
    allowIn: ['paragraph'],
    allowAttributes: ['id', 'class']
});
editor.conversion.elementToElement({model: 'span', view: 'span'});          
editor.conversion.attributeToAttribute({model: 'class', view: 'class'});
editor.conversion.attributeToAttribute({model: {name: 'span', key: 'id'}, view: 'id'});
这使我可以毫无问题地插入跨度。我的问题是,如果我粘贴一些带有跨距的内容(例如,FontBackGroundColor插件中设置了背景颜色的文本),然后按backspace,我会得到以下错误:

VM28478:5 Uncaught CKEditorError: move-operation-node-into-itself: Trying to move a range of nodes into one of nodes from that range. Read more: https://ckeditor.com/docs/ckeditor5/latest/framework/guides/support/error-codes.html#error-move-operation-node-into-itself

    at cc._validate (<anonymous>:5:277330)
    at Jc.on.priority (<anonymous>:5:324234)
    at Jc.fire (<anonymous>:5:101659)
    at Jc.(anonymous function) [as applyOperation] (<anonymous>:5:115956)
    at gc.move (<anonymous>:5:289708)
    at gc.insert (<anonymous>:5:287384)
    at t (<anonymous>:5:319802)
    at <anonymous>:5:319915
    at Jc.change (<anonymous>:5:324808)
    at Nc (<anonymous>:5:318986)
VM28478:5未捕获的CKEditorError:将操作节点移动到自身中:尝试将一个范围的节点移动到该范围内的一个节点中。阅读更多:https://ckeditor.com/docs/ckeditor5/latest/framework/guides/support/error-codes.html#error-将操作节点移动到自身中
抄送时确认(:5:277330)
在Jc.on.priority(:5:324234)
在Jc.fire(:5:101659)
at Jc.(匿名函数)[作为应用程序操作](:5:115956)
在gc.move(:5:289708)
在gc处插入(:5:287384)
t时(:5:319802)
时间:5:319915
在Jc.change(:5:324808)
在北卡罗来纳州(:5:318986)

我的方法正确吗?我试图阅读文档,但我认为我的理解不太正确。

这种方法不适合CKEditor 5。在CKEditor 5中,存在一个将某些特征的视图与其在模型中的存储方式分开的方法。因此,在实现CKEditor 5特性的模型时,您应该考虑它解决了什么问题,而不是它在HTML中的表示方式。这种方法在定义模型行为时需要更多的工作,但这种设计使CKEditor 5能够协同工作

话虽如此,在很多情况下,人们只想保留所有
及其样式、类和自定义属性。在CKEditor 5中,您应该将一些样式、类或属性映射到模型元素或
$text
节点属性,以匹配特征语义,而不是HTML数据

请记住,这不是CKEditor的理想解决方案,您应该

  • 通过扩展内置的
    $text
    ,在文本上定义存储属性的一些属性

    editor.model.schema.extend(“$text”{
    allowAttributes:['spanId','spanClass']
    } );
    
  • 提供将
    映射到模型文本属性的(视图到模型)和(模型到视图)转换。您可以在中阅读更多关于此的信息。但下面这样的方法应该有效:

    editor.conversion.for('upcast')。元素到属性({
    视图:{
    名称:“span”,
    属性:{
    “id”:/+/
    }
    },
    型号:{
    键:“spanId”,
    值:viewElement=>viewElement.getAttribute('id')
    }
    } );
    editor.conversion.for('downcast')。attributeToElement({
    型号:“spanId”,
    视图:(modelAttributeValue,viewWriter)=>{
    返回viewWriter.createAttributeElement('span'{
    id:modelAttributeValue
    } );
    }
    } );
    
  • 现在,您的设置不起作用的原因是您将块插入了另一个块中-这在CKEditor 5模型中不受支持。大多数HTML内联元素通常转换为一些文本属性,因此需要
    元素到属性的转换