bianjiang 1 năm trước cách đây
mục cha
commit
7655e065c2

+ 10 - 5
src/modules/editor/components/CompUI/basicUI/Text/component2.tsx

@@ -222,17 +222,22 @@ const EditorComp = defineComponent({
 
       editorRefVal.model.document.on("change:data", () => {
         let value = editorRefVal?.getData();
-        console.log(value);
         if (comp.value.text !== value) {
           if (!value) {
-            preTag.value = comp.value.text;
+            const tagReg =
+              /(<(p|h2|h3)[\s\S]*?>)(<span[\s\S]*?>)?([\s\S]*?)(<\/span>)?(<\/(p|h2|h3)>)/gi;
+            const blocks = comp.value.text.match(tagReg);
+            if (!blocks) {
+              return;
+            }
+            preTag.value = blocks[0];
           }
           if (!comp.value.text && preTag.value) {
             const text = value?.replace(/<[^>]+>/g, "");
-            const spanTagReg =
-              /([\s\S]*?<span[\s\S]*?>)([\s\S]*?)(<\/span>[\s\S]*?)/gi;
+            const tagReg =
+              /(<(p|h2|h3)[\s\S]*?>)(<span[\s\S]*?>)?([\s\S]*?)(<\/span>)?(<\/(p|h2|h3)>)/gi;
 
-            value = preTag.value?.replace(spanTagReg, `$1${text}$3`);
+            value = preTag.value?.replace(tagReg, `$1$3${text}$5$6`);
             editorRefVal?.setData(value || "");
             editorRefVal?.focus();
             const selection = window.getSelection();