feat(card): 插入block卡片会多一行空行

This commit is contained in:
yanmao 2021-11-25 13:24:00 +08:00
parent 35e6263fd0
commit 273ed766a3
3 changed files with 21 additions and 14 deletions

View File

@ -518,6 +518,7 @@ class Block implements BlockModelInterface {
block: NodeInterface | Node | string, block: NodeInterface | Node | string,
range?: RangeInterface, range?: RangeInterface,
splitNode?: (node: NodeInterface) => NodeInterface, splitNode?: (node: NodeInterface) => NodeInterface,
removeCurrentEmptyBlock: boolean = false,
) { ) {
if (!isEngine(this.editor)) return; if (!isEngine(this.editor)) return;
const { change, node, list, inline } = this.editor; const { change, node, list, inline } = this.editor;
@ -543,7 +544,7 @@ class Block implements BlockModelInterface {
} }
// 当前选择范围在段落外面 // 当前选择范围在段落外面
if (container.isEditable()) { if (container.isEditable()) {
node.insert(block, safeRange); node.insert(block, safeRange, removeCurrentEmptyBlock);
safeRange.collapse(false); safeRange.collapse(false);
if (!range) change.apply(safeRange); if (!range) change.apply(safeRange);
return; return;
@ -678,7 +679,7 @@ class Block implements BlockModelInterface {
if (selection.focus) selection.focus.remove(); if (selection.focus) selection.focus.remove();
if (selection.anchor) selection.anchor.remove(); if (selection.anchor) selection.anchor.remove();
// 插入新 Block // 插入新 Block
node.insert(block, safeRange); node.insert(block, safeRange, removeCurrentEmptyBlock);
if (!range) change.apply(safeRange); if (!range) change.apply(safeRange);
} }
/** /**

View File

@ -247,18 +247,23 @@ class CardModel implements CardModelInterface {
if (isInline) { if (isInline) {
inline.insert(card.root, range); inline.insert(card.root, range);
} else { } else {
block.insert(card.root, range, (container) => { block.insert(
//获取最外层的block嵌套节点 card.root,
let blockParent = container.parent(); range,
while (blockParent && !blockParent.isEditable()) { (container) => {
container = blockParent; //获取最外层的block嵌套节点
const parent = blockParent.parent(); let blockParent = container.parent();
if (parent && node.isBlock(parent)) { while (blockParent && !blockParent.isEditable()) {
blockParent = parent; container = blockParent;
} else break; const parent = blockParent.parent();
} if (parent && node.isBlock(parent)) {
return container; blockParent = parent;
}); } else break;
}
return container;
},
true,
);
} }
this.components.push(card); this.components.push(card);
card.focus(range); card.focus(range);

View File

@ -63,6 +63,7 @@ export interface BlockModelInterface {
block: NodeInterface | Node | string, block: NodeInterface | Node | string,
range?: RangeInterface, range?: RangeInterface,
splitNode?: (node: NodeInterface) => NodeInterface, splitNode?: (node: NodeInterface) => NodeInterface,
removeCurrentEmptyBlock?: boolean,
): void; ): void;
/** /**
* block节点为新的节点或设置新属性 * block节点为新的节点或设置新属性