import { Quill } from 'vue-quill-editor'; const BlockEmbed = Quill.import('blots/block/embed'); const Link = Quill.import('formats/link'); const ATTRIBUTES = ['height', 'width', 'controls', 'src', 'poster']; class VideoBlot extends BlockEmbed { static create(value) { const node = super.create(); // 设置基本属性 node.setAttribute('controls', 'controls'); node.setAttribute('controlsList', 'nodownload'); node.setAttribute('preload', 'metadata'); // 支持对象形式的value if (typeof value === 'object') { node.setAttribute('src', this.sanitize(value.url)); node.setAttribute('poster', value.poster || ''); node.setAttribute('width', value.width || '100%'); node.setAttribute('height', value.height || 'auto'); } else { node.setAttribute('src', this.sanitize(value)); } return node; } static formats(node) { return ATTRIBUTES.reduce((formats, attribute) => { if (node.hasAttribute(attribute)) { formats[attribute] = node.getAttribute(attribute); } return formats; }, {}); } static sanitize(url) { return Link.sanitize(url); } static value(node) { return { url: node.getAttribute('src'), poster: node.getAttribute('poster'), width: node.getAttribute('width'), height: node.getAttribute('height') }; } format(name, value) { if (ATTRIBUTES.includes(name)) { if (value) { this.domNode.setAttribute(name, value); } else { this.domNode.removeAttribute(name); } } else { super.format(name, value); } } } VideoBlot.blotName = 'video'; VideoBlot.className = 'ql-video'; VideoBlot.tagName = 'video'; export default VideoBlot;