fix: mark wrap

This commit is contained in:
yanmao 2022-01-06 14:04:28 +08:00
parent 3df7f31b14
commit 2081d2a0b1
5 changed files with 48 additions and 18 deletions

View File

@ -226,10 +226,10 @@ class Engine<T extends EngineOptions = EngineOptions>
return toJSON0(this.container);
}
private normalize() {
normalize(container: NodeInterface = this.container) {
let block = $('<p />');
// 保证所有行内元素都在段落内
let childNodes = this.container.children();
let childNodes = container.children();
childNodes.each((_, index) => {
const node = childNodes.eq(index);
if (!node) return;
@ -244,10 +244,10 @@ class Engine<T extends EngineOptions = EngineOptions>
});
if (block.get<HTMLElement>()!.childNodes.length > 0) {
this.container.append(block);
container.append(block);
}
// 处理空段落
childNodes = this.container.children();
childNodes = container.children();
childNodes.each((_, index) => {
const node = childNodes.eq(index);
if (!node) return;

View File

@ -994,6 +994,16 @@ class Mark implements MarkModelInterface {
//插件一样,不合并,分割后插入
else if (plugin && plugin === curPlugin) {
this.split(safeRange);
if (safeRange.collapsed) {
let markParent = parent.parent();
while (
markParent &&
nodeApi.isMark(markParent)
) {
mark = nodeApi.wrap(mark, markParent);
markParent = markParent.parent();
}
}
result = false;
break;
}
@ -1687,7 +1697,7 @@ class Mark implements MarkModelInterface {
const { node } = this.editor;
mark = isNode(mark) ? $(mark) : mark;
const isMark = node.isMark(mark);
if (isMark) {
if (isMark && !mark.isCursor()) {
const childrenNodes = mark.children();
childrenNodes.each((_, index) => {
const child = childrenNodes.eq(index);
@ -1726,6 +1736,17 @@ class Mark implements MarkModelInterface {
) {
next.remove();
}
const prev = mark.prev();
const prevP = prev?.prev();
if (
prev &&
prevP &&
prev.isText() &&
/^\u200b$/g.test(prev.text()) &&
!node.isInline(prevP)
) {
prev.remove();
}
}
}
}

View File

@ -239,19 +239,19 @@ class NodeModel implements NodeModelInterface {
outer: NodeInterface,
mergeSame: boolean = false,
) {
const { node, mark } = this.editor;
const { mark } = this.editor;
if (isNode(source)) source = $(source);
outer = node.clone(outer, false);
outer = this.clone(outer, false);
// 文本节点
if (source.isText()) {
outer.append(node.clone(source, false));
outer.append(this.clone(source, false));
return source.replaceWith(outer);
}
// 包裹样式节点
if (mergeSame && this.isMark(outer)) {
//合并属性和样式值
const outerClone = node.clone(outer, false, false);
const outerClone = this.clone(outer, false, false);
if (source.name === outer.name) {
const attributes = source.attributes();
delete attributes.style;
@ -270,9 +270,9 @@ class NodeModel implements NodeModelInterface {
Object.keys(styles).forEach((key) => {
if (!outer.css(key)) outer.css(key, styles[key]);
});
outer.append(node.clone(source, true, false).children());
outer.append(this.clone(source, true, false).children());
} else {
outer.append(node.clone(source, true, false));
outer.append(this.clone(source, true, false));
}
const children = outer.allChildren();
@ -288,10 +288,12 @@ class NodeModel implements NodeModelInterface {
return source.replaceWith(outer);
}
// 其它情况
const shadowNode = node.clone(source, false, false);
const parent = source.parent();
const shadowNode = this.clone(source, false, false);
source.after(shadowNode);
outer.append(source);
return shadowNode.replaceWith(outer);
// 没有父级节点就直接返回包裹后的节点
return parent ? shadowNode.replaceWith(outer) : outer;
}
/**
@ -878,7 +880,7 @@ class NodeModel implements NodeModelInterface {
cloneNode.append($('<br />'));
}
if (
this.editor.node.isBlock(cloneNode) &&
this.isBlock(cloneNode) &&
this.isEmptyWithTrim(cloneNode)
) {
cloneNode.html('<br />');
@ -891,10 +893,7 @@ class NodeModel implements NodeModelInterface {
) {
childNode.append($('<br />'));
}
if (
this.editor.node.isBlock(childNode) &&
this.isEmptyWithTrim(childNode)
) {
if (this.isBlock(childNode) && this.isEmptyWithTrim(childNode)) {
childNode.html('<br />');
}

View File

@ -176,6 +176,11 @@ export interface EngineInterface<T extends EngineOptions = EngineOptions>
* placeholder
*/
hidePlaceholder(): void;
/**
*
* @param container
*/
normalize(container?: NodeInterface): void;
/**
*
* @param eventType

View File

@ -254,8 +254,13 @@ export default class<T extends QuoteOptions> extends BlockPlugin<T> {
pasteHtml(node: NodeInterface) {
if (!isEngine(this.editor)) return;
if (node.name === this.tagName) {
const nodeApi = this.editor.node;
node.css('padding-left', '');
node.css('text-indent', '');
if (nodeApi.isEmpty(node)) {
node.append('<p><br/></p>');
}
this.editor.normalize(node);
return false;
}
return true;