bianjiang 1 år sedan
förälder
incheckning
5421f98e5f
1 ändrade filer med 21 tillägg och 3 borttagningar
  1. 21 3
      src/modules/editor/components/CompUI/basicUI/Text/component2.tsx

+ 21 - 3
src/modules/editor/components/CompUI/basicUI/Text/component2.tsx

@@ -123,6 +123,7 @@ const EditorComp = defineComponent({
       blurCanceler?.();
     });
     const preHeight = ref<number>(0);
+    const preTag = ref();
     const initHeight = () => {
       const dom: HTMLElement | null = document.querySelector(
         `#editor_${props.compId}`
@@ -220,11 +221,28 @@ const EditorComp = defineComponent({
       selection?.addRange(range);
 
       editorRefVal.model.document.on("change:data", () => {
-        const value = editorRefVal?.getData();
-
+        let value = editorRefVal?.getData();
+        console.log(value);
         if (comp.value.text !== value) {
-          actions.updateCompData(comp, "value.text", value);
+          if (!value) {
+            preTag.value = comp.value.text;
+          }
+          if (!comp.value.text && preTag.value) {
+            const text = value?.replace(/<[^>]+>/g, "");
+            const spanTagReg =
+              /([\s\S]*?<span[\s\S]*?>)([\s\S]*?)(<\/span>[\s\S]*?)/gi;
 
+            value = preTag.value?.replace(spanTagReg, `$1${text}$3`);
+            editorRefVal?.setData(value || "");
+            editorRefVal?.focus();
+            const selection = window.getSelection();
+            nextTick(() => {
+              if (selection?.focusNode) {
+                selection?.setPosition(selection?.focusNode, text?.length);
+              }
+            });
+          }
+          actions.updateCompData(comp, "value.text", value);
           nextTick(() => {
             const element = editorRefVal?.ui.view.editable.element || null;
             if (!element) {