我在config中打开了allowedContent属性。
config.allowedContent = "true"
这允许我将id添加到contenteditable div中的段落。
但是,现在每当我在contenteditable div中按Enter键时,就会生成一个具有相同id的新段落。我想假设在输入密钥后,应该插入一个没有任何ID的新段落,但看起来像是从先前生成的段落中复制了。
有什么方法可以避免这种情况吗?
答案 0 :(得分:0)
试试这个。它不是防弹,但运作良好。即使我写了它,我也讨厌它,所以如果你改进它,请分享爱情;)
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);
}
});