问题描述
我已在配置中打开allowedContent属性.
I have turned on allowedContent property in config.
config.allowedContent = "true"
这使我可以将ID添加到contenteditable div内的段落中.
This allows me to add ids to paragraphs inside contenteditable div.
但是,现在每当我在contenteditable div中按回车键时,都会生成具有相同ID的新段落.我假设在敲击Enter键之后,应该插入一个没有任何ID的新段落,但看起来这些ID是从先前生成的段落中复制的.
However, now whenever I hit enter key inside the contenteditable div a new paragraph with same id is generated. I would assume after hiiting enter key a new paragraph should be inserted without any ids but it looks like the ids are copied from previously generated paragraph.
有什么办法可以避免这种情况?
Is there any way to avoid this?
推荐答案
尝试一下.它不是防弹的,但效果很好.即使我写了它,我还是很讨厌它,因此,如果您对此有所改进,请分享爱;)
Try this. It's not bullet proof but works well enough. Even though I wrote it, I kind of hate it so if you improve on it, please share the love ;)
editor.on('key', function (evt) {
// Only if editor is not in source mode.
if (editor.mode === 'source') { return; }
// Enter is keyCode 13
if (evt.data.keyCode === 13) {
// if we call getStartElement too soon, we get the wrong element sometimes
setTimeout(function () {
var selection = editor.getSelection();
if (typeof selection === 'undefined') { return; }
var startElement = selection.getStartElement();
// If there are spans nested in the paragraph preserve them
// And we need to find the parent paragraph
// This could be optimized...
if (startElement.getName() == 'span') {
var text = "";
while (startElement.getName() == 'span') {
text += startElement.getHtml();
startElement = startElement.getParent();
}
if (text.length === 0) {
startElement.setHtml(' ');
} else {
startElement.setHtml(text);
}
}
// HERE I remove the "id" attribute.
startElement.removeAttribute("id");;
}, 10);
}
});
这篇关于CKeditor内联:重复段落ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!