我使用的是CKEditor 4.4,我尝试使用image2插件将width和height设置为CSS属性(在style属性中),而使用相应的<img>标记属性。
用其他术语来说,我现在使用editor.getData()方法得到的是这样的内容:
<img src="text.jpg" width="100" height="100" />但我想要另一种形式:
<img src="text.jpg" style="width:100px;height:100px" />我试图使用allowedContent和disallowedContent在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和height不再设置(既不是属性,也不是样式),图像不能调整大小,图像属性对话框不再包括与图像大小相关的输入框。
我还试图逆转马尔科·科尔泰利诺在这个StackOverflow的答案中提出的解决方案,但没有取得积极的结果。
有人能帮我吗?
发布于 2014-05-15 10:43:07
我通过覆盖downcast和upcast插件的image2方法来解决这个问题(正如雷玛尔所建议的)。
当调用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;
}
}
});
});https://stackoverflow.com/questions/23661114
复制相似问题