fix: mark reverse wrap priority judgment

This commit is contained in:
yanmao 2021-12-24 16:42:33 +08:00
parent 2aab24b884
commit fac805f660
4 changed files with 58 additions and 10 deletions

View File

@ -21,6 +21,7 @@ import { Backspace, Enter } from './typing';
import { $ } from '../node';
import { isBlockPlugin } from '../plugin';
import { isNode } from '../node/utils';
import { CardType } from '../card/enum';
class Block implements BlockModelInterface {
private editor: EditorInterface;
@ -855,7 +856,7 @@ class Block implements BlockModelInterface {
if (range.startNode.isRoot()) range.shrinkToElementNode();
if (
!range.startNode.inEditor() ||
this.editor.card.find(range.startNode)
this.editor.card.find(range.startNode)?.type === CardType.BLOCK
)
return [];
const sc = range.startContainer;

View File

@ -479,7 +479,8 @@ class Inline implements InlineModelInterface {
let appendChild: NodeInterface | undefined | null = undefined;
const appendToParent = (childrenNodes: NodeInterface) => {
childrenNodes.each((child, index) => {
if (childrenNodes.eq(index)?.isCard()) {
const childNode = childrenNodes.eq(index);
if (childNode?.isCard()) {
appendChild = appendChild
? appendChild.next()
: parent.first();
@ -488,9 +489,9 @@ class Inline implements InlineModelInterface {
}
if (appendChild) {
appendChild.after(child);
appendChild = childrenNodes.eq(index);
appendChild = childNode;
} else {
appendChild = childrenNodes.eq(index);
appendChild = childNode;
parent.prepend(child);
}
});
@ -499,13 +500,14 @@ class Inline implements InlineModelInterface {
const leftNodes = leftChildren.toArray();
appendToParent(leftChildren);
const rightChildren = right.children();
const rightNodes = rightChildren.toArray();
let rightNodes = rightChildren.toArray();
// 根据跟踪节点的root节点和path获取其在rightNodes中的新节点
if (keelpRoot)
keelpNode = rightNodes
.find((node) => node.equal(keelpRoot!))
?.getChildByPath(keelpPath);
appendToParent(rightChildren);
rightNodes = rightChildren.toArray();
// 重新设置范围
//移除左右两边的 br 标签
if (leftNodes.length === 1 && leftNodes[0].name === 'br') {

View File

@ -358,14 +358,14 @@ class Mark implements MarkModelInterface {
const leftNodes = leftChildren.toArray();
appendToParent(leftChildren);
const rightChildren = right.children();
const rightNodes = rightChildren.toArray();
let rightNodes = rightChildren.toArray();
// 根据跟踪节点的root节点和path获取其在rightNodes中的新节点
if (keelpRoot)
keelpNode = rightNodes
.find((node) => node.equal(keelpRoot!))
?.getChildByPath(keelpPath);
appendToParent(rightChildren);
rightNodes = rightChildren.toArray();
let zeroWidthNode = $('\u200b', null);
// 重新设置范围
@ -842,7 +842,7 @@ class Mark implements MarkModelInterface {
}
} else if (node.isText() && !nodeApi.isEmpty(node)) {
nodeApi.removeZeroWidthSpace(node);
const parent = node.parent();
let parent = node.parent();
//父级和当前要包裹的节点,属性和值都相同,那就不包裹。只有属性一样,并且父节点只有一个节点那就移除父节点包裹,然后按插件情况合并值
if (parent && nodeApi.isMark(parent)) {
if (this.compare(parent.clone(), mark, true)) return false;
@ -859,11 +859,53 @@ class Mark implements MarkModelInterface {
return parent;
}
//插件一样,不合并,直接移除
else if (plugin && plugin === curPlugin)
else if (plugin && plugin === curPlugin) {
nodeApi.unwrap(parent);
parent = node.parent();
}
}
}
nodeApi.wrap(node, mark);
let targetNode = parent;
let curPlugin = targetNode
? this.findPlugin(targetNode)
: undefined;
/**
* <span>123</span> -> <strong><span>123</span></strong>
* <strong><span>123</span><b>123</b></strong> -> <font><strong><span>123</span></span></font><strong><b>123</b></strong>
*/
let isHandle = false;
while (
targetNode &&
nodeApi.isMark(targetNode) &&
plugin &&
curPlugin &&
plugin.mergeLeval > curPlugin.mergeLeval
) {
isHandle = true;
const targetChildrens = targetNode.children().toArray();
const cloneMark = targetNode.clone();
targetChildrens.forEach((targetChild) => {
// 零宽字符的文本跳过
if (
targetChild.isCursor() ||
(targetChild.type === 3 &&
/^\u200b$/.test(targetChild.text() || ''))
) {
return;
}
if (node.equal(targetChild) || targetChild.contains(node)) {
node = nodeApi.wrap(
nodeApi.replace(node, cloneMark),
mark,
);
} else {
nodeApi.wrap(targetChild, mark);
}
});
nodeApi.unwrap(targetNode);
targetNode = this.closest(node);
}
if (!isHandle) nodeApi.wrap(node, mark);
return node;
}
return;

View File

@ -464,6 +464,9 @@ class NodeModel implements NodeModelInterface {
clone.append(childNode);
childNode = nextNode;
}
if (source.isText()) {
clone.append(source.clone());
}
return source.replaceWith(clone);
}