reactjs - 如何在使用 reactQuill 组件时自定义 'image' 标签的宽高和样式?
问题描述
我正在研究小型反应项目。我将在 'toolbar' 中显示使用width、height和一些样式(例如'display:inline'或'margin:auto' )指定的图像。
我就这样编写了我的项目以供参考。
.... ....
// before defining of the component
const WHITE_STYLE = ['margin', 'display', 'float'];
class ImageBlot extends BaseImage {
static formats(domNode:any) {
return ATTRIBUTES.reduce(function(formats:any, attribute:any) {
if (domNode.hasAttribute(attribute)) {
formats[attribute] = domNode.getAttribute(attribute);
}
return formats;
}, {});
}
format(name:any, value:any) {
if (ATTRIBUTES.indexOf(name) > -1) {
if (value) {
if (name === 'style') {
value = this.sanitize_style(value);
}
this.domNode.setAttribute(name, value);
} else {
this.domNode.removeAttribute(name);
}
} else {
super.format(name, value);
}
}
sanitize_style(style:any) {
let style_arr = style.split(";")
let allow_style = "";
style_arr.forEach((v:any, i:number) => {
if (WHITE_STYLE.indexOf(v.trim().split(":")[0]) !== -1) {
allow_style += v + ";"
}
})
return allow_style;
}
}
ImageBlot.blotName = 'imageBlot';
ImageBlot.className = 'image-blot';
ImageBlot.tagName = 'img';
Quill.register({ 'formats/imageBlot': ImageBlot });
.... ..... .....
.... ..... .....
//adding image tag dynamically...
if(this.ref && this.ref.current) {
let index = this.state.index || 0;
this.ref?.current.getEditor().insertEmbed(index, 'image', this.imageUrl, 'user');
this.ref?.current.getEditor().formatText(index, 1, 'width', 100);
this.ref?.current.getEditor().formatText(index, 1, 'height', 100);
if( imageProperty.displayType === '2' && imageProperty.style === 'right')
this.ref?.current.getEditor().formatText(index, 1, 'style', 'float:right');
if( imageProperty.displayType === '2' && imageProperty.style === 'left')
this.ref?.current.getEditor().formatText(index, 1, 'style', 'float:left');
if( imageProperty.displayType === '1')
this.ref?.current.getEditor().formatText(index, 1, 'style', 'display:block;margin:auto');
}
.... ....
不幸的是,结果只应用了宽度样式,没有别的。我该怎么做?谢谢!
解决方案
推荐阅读
- kotlin - 是否可以链接两个不同的列表
- javascript - mapbox GL JS 中的 GeoJson 不会通过 URL 接受它,格式似乎很好
- python - 使用python从工件下载.xml文件时下载损坏的xml
- android - 拍照并将其保存在 Android 的内部存储中
- javascript - (JavaScript) 获取数组中交替值的总和并从中获取最大值
- javascript - 为什么 JS 编译器不能识别对 Array.prototype.filter 的调用
- c++ - 具有可变非类型参数的模板特化
- javascript - 在 HTML 画布中单独旋转平铺图像
- powerbi - 将 Azure 时序见解数据导入 Power BI
- regex - 正则表达式捕获开始词之间的所有内容,直到结束特定的结束词