繁体   English   中英

强制CKEditor image2插件在syle中设置宽度和高度

[英]Force CKEditor image2 plugin to set width and heigth inside the syle

我使用的是CKEditor 4.4,我尝试使用ACF强制image2插件将widthheight设置为CSS属性(在style属性中),而不是使用相应的<img>标签属性。

editor.getData() ,我现在使用editor.getData()方法得到的是这样的:

<img src="text.jpg" width="100" height="100" />

但我想要这种其他形式:

<img src="text.jpg" style="width:100px;height:100px" />

我试图去够使用这个结果allowedContentdisallowedContentconfig.js文件。 这是我想什么(见为参考):

//Allow everything
config.allowedContent = {
    $1: {
        // Use the ability to specify elements as an object.
        elements: CKEDITOR.dtd,
        attributes: true,
        styles: true,
        classes: true
    }
};

config.disallowedContent = "img[width,height]";

这样,结果就是不再设置widthheight (既不作为属性也不作为样式),无法调整图像的大小,并且“图像属性”对话框不再包含与图像尺寸有关的输入框。

我也尝试过推Marco Marco Cortellino此StackOverflow答案中提出的解决方案,但没有取得积极的结果。

有人能帮我吗?

我通过覆盖image2插件的downcastupcast方法解决了这个问题(正如Reinmar所建议的)。

editor.getData()方法时,此方法先处理图像元素。

因此,以下代码代表了一种可能的解决方案:

CKEDITOR.on("instanceCreated", function (ev) {
    ev.editor.on("widgetDefinition", function (evt) {
        var widgetData = evt.data;

        if (widgetData.name != "image" || widgetData.dialog != "image2") return;

        //Override of upcast
        if (!widgetData.stdUpcast) {
            widgetData.stdUpcast = widgetData.upcast;

            widgetData.upcast = function (el, data) {
                var el = widgetData.stdUpcast(el, data);

                if (!el) return el;

                var attrsHolder = el.name == 'a' ? el.getFirst() : el;
                var attrs = attrsHolder.attributes;

                if (el && el.name == "img") {
                    if (el.styles) {
                        attrs.width = (el.styles.width + "").replace('px', '');
                        attrs.height = (el.styles.height + "").replace('px', '');

                        delete el.styles.width;
                        delete el.styles.height;

                        attrs.style = CKEDITOR.tools.writeCssText(el.styles);
                    }                      
                }

                return el;
            }
        }

        //Override of downcast
        if (!widgetData.stdDowncast) {
            widgetData.stdDowncast = widgetData.downcast;

            widgetData.downcast = function (el) {

                el = this.stdDowncast(el);

                var attrsHolder = el.name == 'a' ? el.getFirst() : el;
                var attrs = attrsHolder.attributes;

                var realWidth, realHeight;

                var widgets = ev.editor.widgets.instances;
                for (widget in widgets) {

                    if (widgets[widget].name != "image" || widgets[widget].dialog != "image2") {
                        continue;
                    }

                    realWidth = $(widgets[widget].element.$).width();
                    realHeight = $(widgets[widget].element.$).height();
                }

                var style = CKEDITOR.tools.parseCssText(attrs.style)

                if (attrs.width) {
                    style.width = realWidth + "px";
                    delete attrs.width;
                }
                if (attrs.height) {
                    style.height = realHeight + "px";
                    delete attrs.height;
                }

                attrs.style = CKEDITOR.tools.writeCssText(style);

                return el;
            }
        }
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM