fix: 选中block节点,然后粘贴markdown会导致节点位置错误

This commit is contained in:
yanmao 2021-12-07 22:16:33 +08:00
parent 51bcb7ef3f
commit 6e48dd4366
5 changed files with 40 additions and 8 deletions

View File

@ -389,9 +389,13 @@ class ChangeModel implements ChangeInterface {
if (!nodeApi.isBlock(firstNode)) { if (!nodeApi.isBlock(firstNode)) {
range.shrinkToElementNode(); range.shrinkToElementNode();
if (childNodes.length > 0) { if (childNodes.length > 0) {
const children = range.startNode.children();
startRange = { startRange = {
node: range.startNode, node: range.startNode,
offset: range.startOffset, offset:
children.length === 1 && children[0].nodeName === 'BR'
? 0
: range.startOffset,
}; };
} }
let nextNode = firstNode.next(); let nextNode = firstNode.next();
@ -424,6 +428,16 @@ class ChangeModel implements ChangeInterface {
lastNode.remove(); lastNode.remove();
lastNode = $(childNodes[childNodes.length - 1]); 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 node: NodeInterface | null = $(childNodes[0]);
let prev: NodeInterface | null = null; let prev: NodeInterface | null = null;
const appendNodes = []; const appendNodes = [];

View File

@ -366,10 +366,11 @@ export default class Paste {
let fragmentNode = $(fragment); let fragmentNode = $(fragment);
const first = fragmentNode.first(); const first = fragmentNode.first();
//如果光标在文本节点,并且父级节点不是根节点,移除粘贴数据的第一个节点块级节点,让其内容接在光标所在行 //如果光标在文本节点,并且父级节点不是根节点,移除粘贴数据的第一个节点块级节点,让其内容接在光标所在行
const { startNode } = range const cloneRange = range
.cloneRange() .cloneRange()
.shrinkToElementNode() .shrinkToElementNode()
.shrinkToTextNode(); .shrinkToTextNode();
const { startNode } = cloneRange;
if ( if (
startNode.inEditor() && startNode.inEditor() &&
first && first &&

View File

@ -95,7 +95,6 @@ class Range implements RangeInterface {
const startNode = this.startNode; const startNode = this.startNode;
if ( if (
!$(node).isCursor() && !$(node).isCursor() &&
startNode.name === 'p' &&
startNode.children().length === 1 && startNode.children().length === 1 &&
startNode.first()?.name === 'br' startNode.first()?.name === 'br'
) { ) {

View File

@ -93,9 +93,18 @@ export default class extends InlinePlugin<Options> {
const { change } = this.editor; const { change } = this.editor;
const inlineNode = change.inlines.find((node) => this.isSelf(node)); const inlineNode = change.inlines.find((node) => this.isSelf(node));
this.toolbar?.hide(inlineNode); this.toolbar?.hide(inlineNode);
if (inlineNode && !inlineNode.isCard()) { if (inlineNode && inlineNode.length > 0 && !inlineNode.isCard()) {
this.toolbar?.show(inlineNode); const range = change.range.get();
return true; if (
range.collapsed ||
(inlineNode.contains(range.startNode) &&
inlineNode.contains(range.endNode))
) {
this.toolbar?.show(inlineNode);
return true;
} else {
this.toolbar?.hide();
}
} }
return false; return false;
} }

View File

@ -98,8 +98,17 @@ export default class extends InlinePlugin<Options> {
const inlineNode = change.inlines.find((node) => this.isSelf(node)); const inlineNode = change.inlines.find((node) => this.isSelf(node));
this.toolbar?.hide(inlineNode); this.toolbar?.hide(inlineNode);
if (inlineNode && inlineNode.length > 0 && !inlineNode.isCard()) { if (inlineNode && inlineNode.length > 0 && !inlineNode.isCard()) {
this.toolbar?.show(inlineNode); const range = change.range.get();
return true; if (
range.collapsed ||
(inlineNode.contains(range.startNode) &&
inlineNode.contains(range.endNode))
) {
this.toolbar?.show(inlineNode);
return true;
} else {
this.toolbar?.hide();
}
} }
return false; return false;
} }