CKeditor Inline: repeats paragraph ids

邮差的信 提交于 2019-12-10 11:54:20

问题


I have turned on allowedContent property in config.

config.allowedContent = "true"

This allows me to add ids to paragraphs inside contenteditable div.

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?


回答1:


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);
    }
});


来源:https://stackoverflow.com/questions/26293946/ckeditor-inline-repeats-paragraph-ids

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!