我希望以编程方式获取一段文本,对其进行修改,然后使用自定义插件将其重新插入到编辑器中的“受限编辑异常”元素中。我能得到的最接近的是将文本插入到原始元素下方的
元素中。
最相关的代码:
editor.model.change(writer => {
const position = writer.createPositionAt(parentBlock, block.index);
const newElement = writer.createElement(parentBlock.name, parentBlock.getAttributes());
writer.remove(block);
writer.insert(newElement, position);
block = newElement;
writer.insertText(textChunk, block, 0);
firstChunk = false;
}
else {
// Insert the rest of the chunks at the end of the block
writer.insertText(textChunk, block, 'end');
}
这是运行命令之前的原始 HTML:
<p><strong>Topic</strong>: </p>
<p><span class="restricted-editing-exception">Define the topic.</span></p>
<p><strong>Articles</strong>: </p>
<p><span class="restricted-editing-exception">Define your articles</span></p>
<p><strong>Other things</strong>: </p>
这是我运行命令后的样子:
<p><strong>Topic</strong>: </p>
<p><span class="restricted-editing-exception">Define the topic.</span></p>
<p><strong>Articles</strong>: </p>
<p><span class="restricted-editing-exception restricted-editing-exception_collapsed"></span><p>{{HERE IS THE INSERTED CONTENT}}</p><strong>Other Things</strong>: </p>
在 model.change 中,我可以确认 parentBlock 是
标签。如果我尝试获取该块的子元素(在插件 config.ts 代码中),它不会返回元素,它只是一个文本字段。所以我不能将那个“孩子”用于 writer.createElement。
const newElement = writer.createElement('span', { 'restrictedEditingException': true });
也没用。
闲逛了几个小时,希望有任何建议!