CKEditor5在不中断当前元素的情况下插入文本
我有以下代码在当前位置插入文本:CKEditor5在不中断当前元素的情况下插入文本,ckeditor5,Ckeditor5,我有以下代码在当前位置插入文本: editor.model.change( writer => { editor.model.insertContent( writer.createText('[Insert]') ); }); 这在大多数情况下都很好,就像在段落或标题中插入内容一样。 举例如下: 之前: <h2>Sample</h2> <p><span class="te
editor.model.change( writer => {
editor.model.insertContent( writer.createText('[Insert]') );
});
这在大多数情况下都很好,就像在段落或标题中插入内容一样。
举例如下:
之前:
<h2>Sample</h2>
<p><span class="text-huge">Sample formatted text</span></p>
示例
插入后:
<h2>Samp[Insert]le</h2>
Samp[Insert]le
但如果文本预先格式化(例如使用自定义字体大小),则会破坏html元素: 之前:
<h2>Sample</h2>
<p><span class="text-huge">Sample formatted text</span></p>
格式文本示例
插入后:
<p><span class="text-huge">Sample fo</span>[Insert]<span class="text-huge">rmatted text</span></p>
[插入]格式化文本的样本
请注意,元素被拆分,文本被插入,而不应用自定义样式。[Insert]设置在两个跨距之间
如何在不修改html结构的情况下直接插入文本 发生这种情况是因为创建的文本节点没有设置属性。要做到这一点,您需要创建并将其传递给方法。然后,创建的文本节点将具有以下属性:
const model=editor.model;
model.change(writer=>{
const currentAttributes=model.document.selection.getAttributes();
insertContent(writer.createText(“[Foo]”,currentAttributes));
} );
或者,如果要插入文本,可以使用以下方法:
editor.model.change(writer=>{
const selection=editor.model.document.selection;
const currentAttributes=selection.getAttributes();
const insertPosition=selection.focus;
writer.insertText(“[Foo]”,currentAttributes,insertPosition);
} );