在假元素前后插入空白 P 标签

发布于 2024-12-05 16:35:55 字数 3106 浏览 1 评论 0原文

我是 CKEditor 的新手,正在编写我的第一个插件,但我在任何地方都找不到这个插件的答案。

我有一个插件,它在源中创建一个 div 元素,仅包含文本 !!!!!NEWSTABLE!!!!!。 class 属性设置为 newsDiv。在所见即所得编辑器上,我只希望它显示一个假占位符元素。这工作正常,问题是当我在源模式后切换回所见即所得模式时,它会在插入的 div 上方和下方插入空白段落标签。 将 EnterMode 切换为 CKEDITOR.ENTER_BR 可以解决此问题,但我仍然希望使用 Enter 键来创建段落而不是空格。

我的插件代码(受flash插件源代码启发)如下:

(function() {

    function isNewsEmbed(element) {
        return (element.attributes.class == 'newsDiv');
    }

    function createFakeElement(editor, realElement) {
        return editor.createFakeParserElement(realElement, 'cke_news', 'div', false);
    }

    CKEDITOR.plugins.add('newsTable', {
        init: function(editor) {
            var pluginName = 'newsTable';
            editor.ui.addButton('NewsTable', {
                label: 'Add News Section',
                icon: 'https://d26h7uo9h7bqnn.cloudfront.net/famfamfam/newspaper_add.png',
                command: pluginName
            });
            editor.addCommand(pluginName, {
                exec: function(editor) {
                    var node = new CKEDITOR.dom.element('div');
                    node.setAttribute('class', 'newsDiv');
                    node.setHtml('!!!!!NEWSTABLE!!!!!');
                    elem = editor.createFakeElement(node, 'cke_news', 'div', false)
                    editor.insertElement(elem);
                }
            }); 
            var css = 'img.cke_news{' +
                'background-image: url(' + CKEDITOR.getUrl( this.path + 'images/placeholder.png' ) + ');' +
                'background-position: center center;' +
                'background-repeat: no-repeat;' +
                'border: 1px solid #a9a9a9;' +
                'width: 80px;' +
                'height: 80px;' +
            '}';
            editor.addCss(css);
        },
        afterInit: function(editor) {
            var dataProcessor = editor.dataProcessor,
                dataFilter = dataProcessor && dataProcessor.dataFilter;

            if (dataFilter) {
                dataFilter.addRules({
                    elements: {
                        'div' : function( element ) {
                            var attributes = element.attributes,
                            classId = attributes.classid && String( attributes.classid ).toLowerCase();
                            if (!classId && isNewsEmbed(element)){
                                return createFakeElement( editor, element );
                            }
                            return null;
                        }
                    }
                });
            }
        },
        requires : [ 'fakeobjects' ]
    });

})();

按下激活此插件的按钮后,我按“源”,它符合预期...

<p>
<div class="newsDiv">
    !!!!!NEWSTABLE!!!!!</div>
</p>

但是,如果从那里进入所见即所得模式并返回再次(不做任何其他事情)代码已更改为以下内容,这是我不期望的。

<p>
&nbsp;</p>
<div class="newsDiv">
!!!!!NEWSTABLE!!!!!</div>
<p>
&nbsp;</p>

我做错了什么?

I'm new to CKEditor, writing my first plugin and i just can't find the answer to this one anywhere.

I have a plugin which creates a div element in the source containing just the text !!!!!NEWSTABLE!!!!!. The class attribute is set to newsDiv. On the WYSIWYG editor, I just want it to display a fake placeholder element. This works fine, the problem is that when I switch back to WYSIWYG mode after having been in source mode, it inserts blank paragraph tags above and below the inserted div.
Switching enterMode to CKEDITOR.ENTER_BR fixes this, but I would still like the enter key to make a paragraph rather than a break-space.

My plugin code (inspired by the flash plugin source code) is as follows:

(function() {

    function isNewsEmbed(element) {
        return (element.attributes.class == 'newsDiv');
    }

    function createFakeElement(editor, realElement) {
        return editor.createFakeParserElement(realElement, 'cke_news', 'div', false);
    }

    CKEDITOR.plugins.add('newsTable', {
        init: function(editor) {
            var pluginName = 'newsTable';
            editor.ui.addButton('NewsTable', {
                label: 'Add News Section',
                icon: 'https://d26h7uo9h7bqnn.cloudfront.net/famfamfam/newspaper_add.png',
                command: pluginName
            });
            editor.addCommand(pluginName, {
                exec: function(editor) {
                    var node = new CKEDITOR.dom.element('div');
                    node.setAttribute('class', 'newsDiv');
                    node.setHtml('!!!!!NEWSTABLE!!!!!');
                    elem = editor.createFakeElement(node, 'cke_news', 'div', false)
                    editor.insertElement(elem);
                }
            }); 
            var css = 'img.cke_news{' +
                'background-image: url(' + CKEDITOR.getUrl( this.path + 'images/placeholder.png' ) + ');' +
                'background-position: center center;' +
                'background-repeat: no-repeat;' +
                'border: 1px solid #a9a9a9;' +
                'width: 80px;' +
                'height: 80px;' +
            '}';
            editor.addCss(css);
        },
        afterInit: function(editor) {
            var dataProcessor = editor.dataProcessor,
                dataFilter = dataProcessor && dataProcessor.dataFilter;

            if (dataFilter) {
                dataFilter.addRules({
                    elements: {
                        'div' : function( element ) {
                            var attributes = element.attributes,
                            classId = attributes.classid && String( attributes.classid ).toLowerCase();
                            if (!classId && isNewsEmbed(element)){
                                return createFakeElement( editor, element );
                            }
                            return null;
                        }
                    }
                });
            }
        },
        requires : [ 'fakeobjects' ]
    });

})();

After pressing the button that activates this plugin, I press 'Source' and it is as expected...

<p>
<div class="newsDiv">
    !!!!!NEWSTABLE!!!!!</div>
</p>

However, if from there ones goes into WYSIWYG mode and back again (without doing anything else) the code has changed to the following, which i dont expect.

<p>
 </p>
<div class="newsDiv">
!!!!!NEWSTABLE!!!!!</div>
<p>
 </p>

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

避讳 2024-12-12 16:35:55

我知道这是一个相当晚的答案(我正在寻找解决另一个问题,并偶然发现了这个问题),但你不能这样做的原因是因为

和 < code>

标签是“块级”元素,您不能将一个块级元素放入另一个块级元素中。 (这只是无效的 HTML)。

CKEditor 会按照您在上面看到的方式自动修复此问题。

I know it's a rather late answer (I was searching to solve another issue, and stumbled upon this question), but the reason you can't do this, is because both the <p> and <div> tags are "block level" elements, and you can't put a block level element in another block level element. (It's just invalid HTML).

CKEditor automatically repairs this, in the way you're seeing above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文