UNPKG

plugin-audio

Version:
47 lines (37 loc) 1.35 kB
/** * @description editor 插件,重写 editor API * @author wulijie */ import {DomEditor, IDomEditor} from "@wangeditor/editor"; import { AudioElement } from './custom-types' import { Transforms } from 'slate' function withAudio<T extends IDomEditor>(editor: T): T { const { isVoid, normalizeNode } = editor const newEditor = editor // 重写 isVoid // @ts-ignore newEditor.isVoid = (elem: AudioElement) => { const { type } = elem if (type === 'audio') { return true } return isVoid(elem) } // 重写 normalizeNode newEditor.normalizeNode = ([node, path]) => { const type = DomEditor.getNodeType(node) // ----------------- audio 后面必须跟一个 p header blockquote ----------------- if (type === 'audio') { // -------------- audio 是 editor 最后一个节点,需要后面插入 p -------------- const isLast = DomEditor.isLastNode(newEditor, node) if (isLast) { Transforms.insertNodes(newEditor, DomEditor.genEmptyParagraph(), { at: [path[0] + 1] }) } } // 执行默认的 normalizeNode ,重要!!! return normalizeNode([node, path]) } // 返回 editor ,重要! return newEditor } export default withAudio