From 4169dbf39501a10da62f8e06ecfadadfa42e4a83 Mon Sep 17 00:00:00 2001 From: yanmao <55792257+yanmao-cc@users.noreply.github.com> Date: Mon, 27 Dec 2021 22:42:54 +0800 Subject: [PATCH] update: when marks are stored, do not nest --- plugins/mark-range/src/index.ts | 29 +++++++++++++++++++++++--- plugins/mention/src/component/index.ts | 5 +++-- plugins/status/src/components/index.ts | 5 +++-- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/plugins/mark-range/src/index.ts b/plugins/mark-range/src/index.ts index aac9b7b9..0c871a2c 100644 --- a/plugins/mark-range/src/index.ts +++ b/plugins/mark-range/src/index.ts @@ -295,7 +295,7 @@ export default class extends MarkPlugin { subRanges.forEach((subRange) => { //如果是卡片,就给卡片加上预览样式 const cardComponent = card.find(subRange.startNode); - if (cardComponent) { + if (cardComponent && !cardComponent.executeMark) { text += `[card:${ (cardComponent.constructor as CardEntry).cardName },${cardComponent.id}]`; @@ -670,8 +670,17 @@ export default class extends MarkPlugin { const rangeClone = range.cloneRange(); if (childNode.isCard()) { + const cardComponent = card.find(childNode); + if (cardComponent && cardComponent.executeMark) { + const idName = this.getIdName(key); + const markNode = cardComponent.root.find( + `[${idName}="${id}"]`, + ); + cardComponent.executeMark(markNode.clone(), false); + } else { + childNode.removeAttributes(this.getIdName(key)); + } rangeClone.select(childNode); - childNode.removeAttributes(this.getIdName(key)); } else { rangeClone.select(childNode, true); const selection = rangeClone.createSelection(); @@ -765,7 +774,21 @@ export default class extends MarkPlugin { elements.forEach((element) => { const node = $(element); if (node.isCard()) { - node.attributes(this.getIdName(key), id.join(',')); + const cardComponent = card.find(node); + if (cardComponent && cardComponent.executeMark) { + cardComponent.executeMark( + $( + `<${this.tagName} ${ + this.MARK_KEY + }="${key}" ${this.getIdName(key)}="${id.join( + ',', + )}" />`, + ), + true, + ); + } else { + node.attributes(this.getIdName(key), id.join(',')); + } } }); this.editor.mark.wrap( diff --git a/plugins/mention/src/component/index.ts b/plugins/mention/src/component/index.ts index d6a8aef5..cf9e991a 100644 --- a/plugins/mention/src/component/index.ts +++ b/plugins/mention/src/component/index.ts @@ -294,7 +294,7 @@ class Mention extends Card { if (child) this.editor.mark.wrapByNode(child, mark); }); const marks = this.queryMarks().map( - (child) => child.get()?.outerHTML || '', + (child) => child.clone().get()?.outerHTML || '', ); this.setValue({ marks, @@ -315,7 +315,8 @@ class Mention extends Card { if (!this.#container) return []; return this.#container .allChildren() - .filter((child) => child.isElement()); + .filter((child) => child.isElement()) + .map((c) => c.clone()); } render(): string | void | NodeInterface { diff --git a/plugins/status/src/components/index.ts b/plugins/status/src/components/index.ts index 7408306e..85bb27b2 100644 --- a/plugins/status/src/components/index.ts +++ b/plugins/status/src/components/index.ts @@ -192,7 +192,7 @@ class Status extends Card { if (child) this.editor.mark.wrapByNode(child, mark); }); const marks = this.queryMarks().map( - (child) => child.get()?.outerHTML || '', + (child) => child.clone().get()?.outerHTML || '', ); this.setValue({ marks, @@ -214,7 +214,8 @@ class Status extends Card { if (!this.#container) return []; return this.#container .allChildren() - .filter((child) => child.isElement()); + .filter((child) => child.isElement()) + .map((c) => c.clone()); } focusEditor() {