Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 解决 table 下一行空行无法删除问题,解决 图片、视频 下一行空行连续删除 等问题 #5969

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 66 additions & 1 deletion packages/list-module/src/module/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
* @author wangfupeng
*/

import { Editor, Transforms, Range } from 'slate'
import { Editor, Transforms, Range, BaseText, BaseRange, Node as SlateNode } from 'slate'
import { IDomEditor, DomEditor } from '@wangeditor/core'
import { ListItemElement } from './custom-types'
import { BaseElement } from '../../../custom-types'

/**
* 获取选中的 top elems
Expand All @@ -30,6 +31,70 @@ function withList<T extends IDomEditor>(editor: T): T {
return
}

const {
anchor: {
path: [pathIdx],
offset,
},
} = newEditor.selection as BaseRange

// bugfix: 如果上一行是 块(图片、table) 节点,并且 本级 是 空行,执行 合并
if (offset === 0 && pathIdx - 1 >= 0) {
const { hoverbarKeys: _hoverbarKeys } = newEditor.getConfig()

// 本级 node
const node = SlateNode.get(newEditor, [pathIdx]) as BaseElement

// 上一层 node
const preNode = SlateNode.get(newEditor, [pathIdx - 1]) as BaseElement

let nextNode: BaseElement | null = null

const BLACK_TYPE_LIST = ['table']

if (BLACK_TYPE_LIST.includes(preNode.type)) {
return Transforms.removeNodes(editor, { mode: 'highest' })
}

try {
// 下一层
nextNode = SlateNode.get(newEditor, [pathIdx + 1]) as BaseElement
} catch (error) {
console.warn('没有下一个元素')
}

let path = [pathIdx + 1, 0]

if (nextNode) {
// 下一行是 table 则本层和上一层合并,否则会删除到 table 里面
if (BLACK_TYPE_LIST.includes(nextNode.type)) {
path = [pathIdx, 0]
}
} else {
path = [pathIdx, 0]
}

if (
!_hoverbarKeys![node.type] &&
_hoverbarKeys![preNode.type] &&
// 本级文本必须为空
(node.children[0] as BaseText).text === ''
) {
const options = {
at: {
path,
offset: 0,
},
hanging: true,
voids: false,
}

// 合并
Transforms.mergeNodes(newEditor, options)
return
}
}

if (Range.isExpanded(selection)) {
deleteBackward(unit)
return
Expand Down
22 changes: 11 additions & 11 deletions packages/table-module/src/module/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ function withTable<T extends IDomEditor>(editor: T): T {
if (res) return // 命中 table cell ,自己处理删除

// 防止从 table 后面的 p 删除时,删除最后一个 cell - issues/4221
const { selection } = newEditor
if (selection) {
const before = Editor.before(newEditor, selection) // 前一个 location
if (before) {
const isTableOnBeforeLocation = isTableLocation(newEditor, before) // before 是否是 table
const isTableOnCurSelection = isTableLocation(newEditor, selection) // 当前是否是 table
if (isTableOnBeforeLocation && !isTableOnCurSelection) {
return // 如果当前不是 table ,前面是 table ,则不执行删除。否则会删除 table 最后一个 cell
}
}
}
// const { selection } = newEditor
// if (selection) {
// const before = Editor.before(newEditor, selection) // 前一个 location
// if (before) {
// const isTableOnBeforeLocation = isTableLocation(newEditor, before) // before 是否是 table
// const isTableOnCurSelection = isTableLocation(newEditor, selection) // 当前是否是 table
// if (isTableOnBeforeLocation && !isTableOnCurSelection) {
// return // 如果当前不是 table ,前面是 table ,则不执行删除。否则会删除 table 最后一个 cell
// }
// }
// }

// 执行默认的删除
deleteBackward(unit)
Expand Down