From 5eeebbf71324f3b2830c973f91a558f682946833 Mon Sep 17 00:00:00 2001 From: wangfupeng Date: Fri, 13 May 2022 15:59:00 +0800 Subject: [PATCH] refactor: isLastNode --- .../src/modules/code-block/plugin.ts | 9 ++--- .../src/modules/divider/plugin.ts | 9 ++--- packages/core/src/editor/dom-editor.ts | 11 ++++++ packages/table-module/src/module/plugin.ts | 38 ++----------------- packages/video-module/src/module/plugin.ts | 9 ++--- 5 files changed, 23 insertions(+), 53 deletions(-) diff --git a/packages/basic-modules/src/modules/code-block/plugin.ts b/packages/basic-modules/src/modules/code-block/plugin.ts index 999bbf2c3..83f90ea67 100644 --- a/packages/basic-modules/src/modules/code-block/plugin.ts +++ b/packages/basic-modules/src/modules/code-block/plugin.ts @@ -61,12 +61,9 @@ function withCodeBlock(editor: T): T { } if (type === 'pre') { - const editorChildren = newEditor.children - const editorChildrenLength = editorChildren.length - const isLastNode = editorChildren[editorChildrenLength - 1] === node - - if (isLastNode) { - // -------------- pre 是 editor 最后一个节点,需要后面插入 p -------------- + // -------------- pre 是 editor 最后一个节点,需要后面插入 p -------------- + const isLast = DomEditor.isLastNode(newEditor, node) + if (isLast) { Transforms.insertNodes(newEditor, genEmptyP(), { at: [path[0] + 1] }) } diff --git a/packages/basic-modules/src/modules/divider/plugin.ts b/packages/basic-modules/src/modules/divider/plugin.ts index 32d1df07e..5ef40f4ba 100644 --- a/packages/basic-modules/src/modules/divider/plugin.ts +++ b/packages/basic-modules/src/modules/divider/plugin.ts @@ -33,12 +33,9 @@ function withDivider(editor: T): T { return normalizeNode([node, path]) } - const editorChildren = newEditor.children - const editorChildrenLength = editorChildren.length - const isLastNode = editorChildren[editorChildrenLength - 1] === node - - if (isLastNode) { - // -------------- divider 是 editor 最后一个节点,需要后面插入 p -------------- + // -------------- divider 是 editor 最后一个节点,需要后面插入 p -------------- + const isLast = DomEditor.isLastNode(newEditor, node) + if (isLast) { Transforms.insertNodes(newEditor, genEmptyP(), { at: [path[0] + 1] }) } } diff --git a/packages/core/src/editor/dom-editor.ts b/packages/core/src/editor/dom-editor.ts index 35c63d48b..3aa98f96d 100644 --- a/packages/core/src/editor/dom-editor.ts +++ b/packages/core/src/editor/dom-editor.ts @@ -738,4 +738,15 @@ export const DomEditor = { } } }, + + /** + * 是否是编辑器里最后一个元素 + * @param editor editor + * @param node node + */ + isLastNode(editor: IDomEditor, node: Node) { + const editorChildren = editor.children || [] + const editorChildrenLength = editorChildren.length + return editorChildren[editorChildrenLength - 1] === node + }, } diff --git a/packages/table-module/src/module/plugin.ts b/packages/table-module/src/module/plugin.ts index 3a0cf0868..f3b785ab8 100644 --- a/packages/table-module/src/module/plugin.ts +++ b/packages/table-module/src/module/plugin.ts @@ -125,44 +125,12 @@ function withTable(editor: T): T { // 未命中 table ,执行默认的 normalizeNode return normalizeNode([node, path]) } - const topLevelNodes = newEditor.children || [] - const topLevelNodesLength = topLevelNodes.length - const isLastNode = topLevelNodes[topLevelNodesLength - 1] === node - if (isLastNode) { - // -------------- table 是 editor 最后一个节点,需要后面插入 p -------------- + // -------------- table 是 editor 最后一个节点,需要后面插入 p -------------- + const isLast = DomEditor.isLastNode(newEditor, node) + if (isLast) { Transforms.insertNodes(newEditor, genEmptyParagraph(), { at: [path[0] + 1] }) } - - // --------------------- table 后面必须跟一个 p header blockquote(否则后面无法继续输入文字) --------------------- - const nextNode = topLevelNodes[path[0] + 1] || {} - if (SlateElement.isElement(nextNode)) { - const { type: nextNodeType = '' } = nextNode - if ( - nextNodeType !== 'paragraph' && - nextNodeType !== 'blockquote' && - !nextNodeType.startsWith('header') - ) { - // table node 后面不是 p 或 header ,则插入一个空 p - const p = genEmptyParagraph() - const insertPath = [path[0] + 1] - Transforms.insertNodes(newEditor, p, { - at: insertPath, // 在表格后面插入 - }) - } - } - - // --------------------- table 前面不能是 table 或者 void(否则前面插入 p) --------------------- - const prevNode = topLevelNodes[path[0] - 1] || {} - if (SlateElement.isElement(prevNode)) { - const prevNodeType = DomEditor.getNodeType(prevNode) - if (prevNodeType === 'table' || newEditor.isVoid(prevNode)) { - const p = genEmptyParagraph() - Transforms.insertNodes(newEditor, p, { - at: path, // 在表格前面插入 - }) - } - } } // 重写 insertData - 粘贴文本 diff --git a/packages/video-module/src/module/plugin.ts b/packages/video-module/src/module/plugin.ts index 9b3257915..b3aaf422b 100644 --- a/packages/video-module/src/module/plugin.ts +++ b/packages/video-module/src/module/plugin.ts @@ -32,12 +32,9 @@ function withVideo(editor: T): T { // ----------------- video 后面必须跟一个 p header blockquote ----------------- if (type === 'video') { - const editorChildren = newEditor.children - const editorChildrenLength = editorChildren.length - const isLastNode = editorChildren[editorChildrenLength - 1] === node - - if (isLastNode) { - // -------------- video 是 editor 最后一个节点,需要后面插入 p -------------- + // -------------- video 是 editor 最后一个节点,需要后面插入 p -------------- + const isLast = DomEditor.isLastNode(newEditor, node) + if (isLast) { Transforms.insertNodes(newEditor, genEmptyP(), { at: [path[0] + 1] }) } }