diff --git a/packages/engine/src/change/index.ts b/packages/engine/src/change/index.ts index c6c24c2b..106b3764 100644 --- a/packages/engine/src/change/index.ts +++ b/packages/engine/src/change/index.ts @@ -389,9 +389,13 @@ class ChangeModel implements ChangeInterface { if (!nodeApi.isBlock(firstNode)) { range.shrinkToElementNode(); if (childNodes.length > 0) { + const children = range.startNode.children(); startRange = { node: range.startNode, - offset: range.startOffset, + offset: + children.length === 1 && children[0].nodeName === 'BR' + ? 0 + : range.startOffset, }; } let nextNode = firstNode.next(); @@ -424,6 +428,16 @@ class ChangeModel implements ChangeInterface { lastNode.remove(); lastNode = $(childNodes[childNodes.length - 1]); } + if (!startRange) { + const children = range.startNode.children(); + startRange = { + node: range.startNode, + offset: + children.length === 1 && children[0].nodeName === 'BR' + ? 0 + : range.startOffset, + }; + } let node: NodeInterface | null = $(childNodes[0]); let prev: NodeInterface | null = null; const appendNodes = []; diff --git a/packages/engine/src/change/paste.ts b/packages/engine/src/change/paste.ts index 658911d2..80c9bc6f 100644 --- a/packages/engine/src/change/paste.ts +++ b/packages/engine/src/change/paste.ts @@ -366,10 +366,11 @@ export default class Paste { let fragmentNode = $(fragment); const first = fragmentNode.first(); //如果光标在文本节点,并且父级节点不是根节点,移除粘贴数据的第一个节点块级节点,让其内容接在光标所在行 - const { startNode } = range + const cloneRange = range .cloneRange() .shrinkToElementNode() .shrinkToTextNode(); + const { startNode } = cloneRange; if ( startNode.inEditor() && first && diff --git a/packages/engine/src/range.ts b/packages/engine/src/range.ts index 2de51952..cdcd3947 100644 --- a/packages/engine/src/range.ts +++ b/packages/engine/src/range.ts @@ -95,7 +95,6 @@ class Range implements RangeInterface { const startNode = this.startNode; if ( !$(node).isCursor() && - startNode.name === 'p' && startNode.children().length === 1 && startNode.first()?.name === 'br' ) { diff --git a/plugins/link-vue/src/index.ts b/plugins/link-vue/src/index.ts index d3466fa3..c133f642 100644 --- a/plugins/link-vue/src/index.ts +++ b/plugins/link-vue/src/index.ts @@ -93,9 +93,18 @@ export default class extends InlinePlugin { const { change } = this.editor; const inlineNode = change.inlines.find((node) => this.isSelf(node)); this.toolbar?.hide(inlineNode); - if (inlineNode && !inlineNode.isCard()) { - this.toolbar?.show(inlineNode); - return true; + if (inlineNode && inlineNode.length > 0 && !inlineNode.isCard()) { + const range = change.range.get(); + if ( + range.collapsed || + (inlineNode.contains(range.startNode) && + inlineNode.contains(range.endNode)) + ) { + this.toolbar?.show(inlineNode); + return true; + } else { + this.toolbar?.hide(); + } } return false; } diff --git a/plugins/link/src/index.ts b/plugins/link/src/index.ts index 973a2b41..235e10ba 100644 --- a/plugins/link/src/index.ts +++ b/plugins/link/src/index.ts @@ -98,8 +98,17 @@ export default class extends InlinePlugin { const inlineNode = change.inlines.find((node) => this.isSelf(node)); this.toolbar?.hide(inlineNode); if (inlineNode && inlineNode.length > 0 && !inlineNode.isCard()) { - this.toolbar?.show(inlineNode); - return true; + const range = change.range.get(); + if ( + range.collapsed || + (inlineNode.contains(range.startNode) && + inlineNode.contains(range.endNode)) + ) { + this.toolbar?.show(inlineNode); + return true; + } else { + this.toolbar?.hide(); + } } return false; }