简体   繁体   English

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

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

I'm using CKEditor 4.4, and I tried to use ACF to force the image2 plugin to set width and height as CSS properties (in the style attribute), instead to use the corresponding <img> tag attributes. 我使用的是CKEditor 4.4,我尝试使用ACF强制image2插件将widthheight设置为CSS属性(在style属性中),而不是使用相应的<img>标签属性。

In other terms, what I get now using the editor.getData() method is something like that: editor.getData() ,我现在使用editor.getData()方法得到的是这样的:

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

but I want this other form: 但我想要这种其他形式:

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

I tried to reach this result using allowedContent and disallowedContent in the config.js file. 我试图去够使用这个结果allowedContentdisallowedContentconfig.js文件。 This is what I tried (see this for references): 这是我想什么(见为参考):

//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]";

With this, the result is simply that width and height are no more set (neither as attributes nor in the style), the image cannot be resized and the Image Properties dialog no longer include the input boxes related to image size. 这样,结果就是不再设置widthheight (既不作为属性也不作为样式),无法调整图像的大小,并且“图像属性”对话框不再包含与图像尺寸有关的输入框。

I also tried to reverse the solution propesed by Marco Cortellino in this StackOverflow answer , without positive results. 我也尝试过推Marco Marco Cortellino此StackOverflow答案中提出的解决方案,但没有取得积极的结果。

Can someone help me? 有人能帮我吗?

I solved the problem by override the downcast and upcast methods of image2 plugin (as Reinmar has suggested). 我通过覆盖image2插件的downcastupcast方法解决了这个问题(正如Reinmar所建议的)。

This method processes the image element before it is processed when the editor.getData() method is called. editor.getData()方法时,此方法先处理图像元素。

Therefore, the following code represents a possible solution: 因此,以下代码代表了一种可能的解决方案:

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