| 2 | import { Editable } from './editable' |
| 3 | |
| 4 | export const withNormalizeNode = <T extends Editor>(editor: T) => { |
| 5 | const e = editor as T & Editable |
| 6 | |
| 7 | const { normalizeNode } = editor |
| 8 | |
| 9 | e.normalizeNode = entry => { |
| 10 | const [node, path] = entry |
| 11 | if (Editor.isBlock(e, node)) { |
| 12 | const { type, ...attributes } = node |
| 13 | let isUnwrap = false |
| 14 | const isParagraph = !type || type === 'paragraph' |
| 15 | // 相同type类的block不嵌套,paragraph 下不能嵌套block节点 |
| 16 | for (const [child, childPath] of Node.children(editor, path)) { |
| 17 | if (Editor.isBlock(e, child)) { |
| 18 | if (!isUnwrap && !isParagraph && child.type === type) { |
| 19 | Transforms.unwrapNodes(editor, { at: childPath }) |
| 20 | return |
| 21 | } else if (isParagraph) { |
| 22 | Transforms.setNodes(editor, attributes, { at: childPath }) |
| 23 | isUnwrap = true |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | if (isUnwrap) { |
| 28 | Transforms.unwrapNodes(editor, { at: path }) |
| 29 | return |
| 30 | } |
| 31 | } |
| 32 | normalizeNode(entry) |
| 33 | } |
| 34 | |
| 35 | return e |
| 36 | } |