Skip to content

Commit

Permalink
refactor: genEmptyParagraph
Browse files Browse the repository at this point in the history
  • Loading branch information
wangfupeng1988 committed May 13, 2022
1 parent 5eeebbf commit f63cd3c
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 28 deletions.
6 changes: 1 addition & 5 deletions packages/basic-modules/src/modules/code-block/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
import { Editor, Transforms, Node as SlateNode, Element as SlateElement } from 'slate'
import { IDomEditor, DomEditor } from '@wangeditor/core'

function genEmptyP(): SlateElement {
return { type: 'paragraph', children: [{ text: '' }] }
}

function getLastTextLineBeforeSelection(codeNode: SlateNode, editor: IDomEditor): string {
const selection = editor.selection
if (selection == null) return ''
Expand Down Expand Up @@ -64,7 +60,7 @@ function withCodeBlock<T extends IDomEditor>(editor: T): T {
// -------------- pre 是 editor 最后一个节点,需要后面插入 p --------------
const isLast = DomEditor.isLastNode(newEditor, node)
if (isLast) {
Transforms.insertNodes(newEditor, genEmptyP(), { at: [path[0] + 1] })
Transforms.insertNodes(newEditor, DomEditor.genEmptyParagraph(), { at: [path[0] + 1] })
}

// -------------- pre 下面必须是 code --------------
Expand Down
6 changes: 1 addition & 5 deletions packages/basic-modules/src/modules/divider/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
import { Transforms, Element } from 'slate'
import { IDomEditor, DomEditor } from '@wangeditor/core'

function genEmptyP(): Element {
return { type: 'paragraph', children: [{ text: '' }] }
}

function withDivider<T extends IDomEditor>(editor: T): T {
const { isVoid, normalizeNode } = editor
const newEditor = editor
Expand All @@ -36,7 +32,7 @@ function withDivider<T extends IDomEditor>(editor: T): T {
// -------------- divider 是 editor 最后一个节点,需要后面插入 p --------------
const isLast = DomEditor.isLastNode(newEditor, node)
if (isLast) {
Transforms.insertNodes(newEditor, genEmptyP(), { at: [path[0] + 1] })
Transforms.insertNodes(newEditor, DomEditor.genEmptyParagraph(), { at: [path[0] + 1] })
}
}

Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/editor/dom-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -749,4 +749,11 @@ export const DomEditor = {
const editorChildrenLength = editorChildren.length
return editorChildren[editorChildrenLength - 1] === node
},

/**
* 生成空白 paragraph
*/
genEmptyParagraph(): Element {
return { type: 'paragraph', children: [{ text: '' }] }
},
}
6 changes: 1 addition & 5 deletions packages/list-module/src/module/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import { Editor, Transforms, Node as SlateNode, Element as SlateElement } from '
import { IDomEditor, DomEditor } from '@wangeditor/core'
import { checkList } from './helper'

function genEmptyP(): SlateElement {
return { type: 'paragraph', children: [{ text: '' }] }
}

function deleteHandler(newEditor: IDomEditor): boolean {
const [nodeEntry] = Editor.nodes(newEditor, {
match: n => newEditor.children[0] === n, // editor 第一个节点
Expand Down Expand Up @@ -67,7 +63,7 @@ function withList<T extends IDomEditor>(editor: T): T {
match: n => DomEditor.checkNodeType(n, 'list-item'),
})
const emptyParagraphPath = [selection.anchor.path[0] + 1] // 在 ul/ol 的下一行
Transforms.insertNodes(newEditor, genEmptyP(), {
Transforms.insertNodes(newEditor, DomEditor.genEmptyParagraph(), {
at: emptyParagraphPath,
})
newEditor.select({ path: emptyParagraphPath.concat(0), offset: 0 }) // 选中空行的文字
Expand Down
10 changes: 3 additions & 7 deletions packages/table-module/src/module/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ import {
Path,
} from 'slate'
import { IDomEditor, DomEditor } from '@wangeditor/core'
// import $ from '../utils/dom'

function genEmptyParagraph(): SlateElement {
return { type: 'paragraph', children: [{ text: '' }] }
}

// table cell 内部的删除处理
function deleteHandler(newEditor: IDomEditor): boolean {
Expand Down Expand Up @@ -98,7 +93,7 @@ function withTable<T extends IDomEditor>(editor: T): T {
const topLevelNodesLength = topLevelNodes.length
// 在最后一个单元格按tab时table末尾如果没有p则插入p后光标切到p上
if (DomEditor.checkNodeType(topLevelNodes[topLevelNodesLength - 1], 'table')) {
const p = genEmptyParagraph()
const p = DomEditor.genEmptyParagraph()
Transforms.insertNodes(newEditor, p, { at: [topLevelNodesLength] })
// 在表格末尾插入p后再次执行使光标切到p上
newEditor.handleTab()
Expand Down Expand Up @@ -129,7 +124,8 @@ function withTable<T extends IDomEditor>(editor: T): T {
// -------------- table 是 editor 最后一个节点,需要后面插入 p --------------
const isLast = DomEditor.isLastNode(newEditor, node)
if (isLast) {
Transforms.insertNodes(newEditor, genEmptyParagraph(), { at: [path[0] + 1] })
const p = DomEditor.genEmptyParagraph()
Transforms.insertNodes(newEditor, p, { at: [path[0] + 1] })
}
}

Expand Down
8 changes: 2 additions & 6 deletions packages/video-module/src/module/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
* @author wangfupeng
*/

import { Transforms, Element } from 'slate'
import { Transforms } from 'slate'
import { IDomEditor, DomEditor } from '@wangeditor/core'
import { CustomElement } from '../../../custom-types'

function genEmptyP(): Element {
return { type: 'paragraph', children: [{ text: '' }] }
}

function withVideo<T extends IDomEditor>(editor: T): T {
const { isVoid, normalizeNode } = editor
const newEditor = editor
Expand All @@ -35,7 +31,7 @@ function withVideo<T extends IDomEditor>(editor: T): T {
// -------------- video 是 editor 最后一个节点,需要后面插入 p --------------
const isLast = DomEditor.isLastNode(newEditor, node)
if (isLast) {
Transforms.insertNodes(newEditor, genEmptyP(), { at: [path[0] + 1] })
Transforms.insertNodes(newEditor, DomEditor.genEmptyParagraph(), { at: [path[0] + 1] })
}
}

Expand Down

0 comments on commit f63cd3c

Please sign in to comment.