diff --git a/packages/list-module/src/module/plugin.ts b/packages/list-module/src/module/plugin.ts index 255b85edb..f9b4c0680 100644 --- a/packages/list-module/src/module/plugin.ts +++ b/packages/list-module/src/module/plugin.ts @@ -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 @@ -30,6 +31,70 @@ function withList(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 diff --git a/packages/table-module/src/module/plugin.ts b/packages/table-module/src/module/plugin.ts index ec5d4b92a..68663f091 100644 --- a/packages/table-module/src/module/plugin.ts +++ b/packages/table-module/src/module/plugin.ts @@ -88,17 +88,17 @@ function withTable(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)