强制CKEditor image2插件设置样式内的宽度和高度

时间:2014-05-14 17:17:55

标签: javascript css ckeditor

我正在使用CKEditor 4.4,我尝试使用ACF强制image2插件将widthheight设置为CSS属性(在{{1}中) }),而是使用相应的style标记属性。

换句话说,我现在使用<img>方法获得的是:

editor.getData()

但我想要另一种形式:

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

我尝试使用<img src="text.jpg" style="width:100px;height:100px" /> 文件中的allowedContentdisallowedContent来覆盖此结果。这就是我的尝试(参见this的参考资料):

config.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]"; width不再设置(既不作为属性也不作为样式),图像无法调整大小,图像属性对话框不再包含与图像大小相关的输入框。

我还试图在Marco Cortellino中撤消this StackOverflow answer所提出的解决方案,但没有取得积极成果。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

我通过覆盖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;
            }
        }
    });
});