如何在羽毛笔编辑器中添加图像属性?我想添加"alt"和"title"属性



当我上传图像时,只保存它的'src'。我想添加替代文本和标题的SEO目的。我试着在Quill文档中搜索一个模块,但是找不到。

可能不是直接的答案,但相关。以下是在从全文html初始化时保留图像属性的解决方案。

Solution1:

class ImageBlot extends Image {
  static create(value) {
    if (typeof value == 'string') {
      return super.create(value);
    } else {
      return value;
    }
  }
  static value(domNode) {
    return domNode;
  }
}
Quill.register(ImageBlot);

Solution2:

class ImageBlot extends Image {
  static get ATTRIBUTES() {
    return [ 'alt', 'height', 'width', 'class', 'data-original', 'data-width', 'data-height', 'style-data' ]
  }
  static formats(domNode) {
    return this.ATTRIBUTES.reduce(function(formats, attribute) {
      if (domNode.hasAttribute(attribute)) {
        formats[attribute] = domNode.getAttribute(attribute);
      }
      return formats;
    }, {});
  }
  format(name, value) {
    if (this.constructor.ATTRIBUTES.indexOf(name) > -1) {
      if (value) {
        this.domNode.setAttribute(name, value);
      } else {
        this.domNode.removeAttribute(name);
      }
    } else {
      super.format(name, value);
    }
  }
}
Quill.register(ImageBlot);

可以使用solution2指定属性的白名单

最新更新