Skip to content

Commit

Permalink
refactor: isLastNode
Browse files Browse the repository at this point in the history
  • Loading branch information
wangfupeng1988 committed May 13, 2022
1 parent 988fc31 commit 5eeebbf
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 53 deletions.
9 changes: 3 additions & 6 deletions packages/basic-modules/src/modules/code-block/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,9 @@ function withCodeBlock<T extends IDomEditor>(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] })
}

Expand Down
9 changes: 3 additions & 6 deletions packages/basic-modules/src/modules/divider/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@ function withDivider<T extends IDomEditor>(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] })
}
}
Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/editor/dom-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
38 changes: 3 additions & 35 deletions packages/table-module/src/module/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,44 +125,12 @@ function withTable<T extends IDomEditor>(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 - 粘贴文本
Expand Down
9 changes: 3 additions & 6 deletions packages/video-module/src/module/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,9 @@ function withVideo<T extends IDomEditor>(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] })
}
}
Expand Down

0 comments on commit 5eeebbf

Please sign in to comment.