fix: 插入包含文本和inline卡片的节点会导致文本节点位置异常 #61

This commit is contained in:
yanmao 2021-12-07 00:27:41 +08:00
parent cbe5e37b21
commit fc067674ab
3 changed files with 31 additions and 25 deletions

View File

@ -397,7 +397,8 @@ class ChangeModel implements ChangeInterface {
let nextNode = firstNode.next();
nodeApi.insert(firstNode, range);
while (nextNode && !nodeApi.isBlock(nextNode)) {
range.enlargeToElementNode().collapse(false);
if (range.startContainer.nodeType === Node.TEXT_NODE)
range.enlargeToElementNode().collapse(false);
const newNext = nextNode.next();
nodeApi.insert(nextNode, range);
nextNode = newNext;

View File

@ -529,9 +529,13 @@ class NodeModel implements NodeModelInterface {
.remove();
}
}
// 是否在卡片上,卡片还没有渲染
let elementType = parent?.attributes(CARD_ELEMENT_KEY);
if (!elementType && startNode.isCard()) {
range.setStartAfter(startNode);
}
// 检测是否位于卡片两边节点
const elementType = parent?.attributes(CARD_ELEMENT_KEY);
if (parent && elementType && ['left', 'right'].includes(elementType)) {
if (elementType && parent && ['left', 'right'].includes(elementType)) {
const cardComponent = editor.card.find(parent);
if (cardComponent) {
if (elementType === 'left') {

View File

@ -196,16 +196,37 @@ class Parser implements ParserInterface {
//移除 data-id以免在下次判断类型的时候使用缓存
newNode.removeAttributes(DATA_ID);
//移除符合当前节点的属性样式,剩余的属性样式组成新的节点
Object.keys(attributes).forEach((name) => {
const attrKeys = Object.keys(attributes);
let filterAttrCount = 0;
attrKeys.forEach((name) => {
if (attributes[name]) {
filterAttrCount++;
newNode.removeAttributes(name);
}
});
Object.keys(style).forEach((name) => {
let filterStyleCount = 0;
const styleKeys = Object.keys(style);
styleKeys.forEach((name) => {
if (style[name]) {
filterStyleCount++;
newNode.css(name, '');
}
});
// 如果这个节点过滤掉所有属性样式后还是一个有效的节点就替换掉当前节点
if (
filterAttrCount === attrKeys.length &&
filterStyleCount === styleKeys.length &&
schema.getType(newNode) === 'mark'
) {
node.before(newNode);
const children = node.children();
newNode.append(
children.length > 0 ? children : $('\u200b', null),
);
node.remove();
node = newNode;
return;
}
if (newNode.attributes('style').trim() === '')
newNode.removeAttributes('style');
return newNode;
@ -221,26 +242,6 @@ class Parser implements ParserInterface {
oldRules.push(rule);
let newNode = filter(node);
if (!newNode) return;
// 如果这个节点过滤掉所有属性样式后还是一个有效的节点就替换掉当前节点
const newAttributes = newNode.attributes();
const newStyle = getStyleMap(newAttributes.style || '');
delete newAttributes.style;
if (
Object.keys(newAttributes).length === 0 &&
Object.keys(newStyle).length === 0 &&
schema.getType(newNode) === 'mark'
) {
node.before(newNode);
const children = node.children();
newNode.append(
children.length > 0
? children
: $('\u200b', null),
);
node.remove();
node = newNode;
return;
}
//获取这个新的节点所属类型,并且不能是之前节点一样的规则
let type = schema.getType(
newNode,