首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >强制CKEditor image2插件在系统内设置宽度和宽度

强制CKEditor image2插件在系统内设置宽度和宽度
EN

Stack Overflow用户
提问于 2014-05-14 17:17:55
回答 1查看 1.9K关注 0票数 0

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

用其他术语来说,我现在使用editor.getData()方法得到的是这样的内容:

代码语言:javascript
复制
<img src="text.jpg" width="100" height="100" />

但我想要另一种形式:

代码语言:javascript
复制
<img src="text.jpg" style="width:100px;height:100px" />

我试图使用allowedContentdisallowedContentconfig.js文件中达到这个结果。这就是我尝试过的(参考资料见 ):

代码语言:javascript
复制
//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不再设置(既不是属性,也不是样式),图像不能调整大小,图像属性对话框不再包括与图像大小相关的输入框。

我还试图逆转马尔科·科尔泰利诺这个StackOverflow的答案中提出的解决方案,但没有取得积极的结果。

有人能帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-15 10:43:07

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

当调用editor.getData()方法时,此方法在处理图像元素之前对其进行处理。

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

代码语言:javascript
复制
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;
            }
        }
    });
});
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23661114

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档