yangg 6 miesięcy temu
rodzic
commit
c1ca78310c

+ 39 - 29
ui/src/views/system/document/com/components/TextArea/index.vue

@@ -327,18 +327,29 @@ export default {
   methods: {
     /*canvas 添加 */
     save(data) {
-      // 如果没有传入data参数,使用当前编辑器的内容
-      const content = data ? data.main : this.com.content;
-      console.log(112);
-      // 更新内容
-      this.com.content = content;
-      this.syncContent(); // 同步到预览模式
-
-      // 触发更新事件
-      this.$emit("onUpdate", this.currentIndex, this.com.content);
-
-      // 返回保存成功状态
-      return true;
+      return new Promise(async (resolve, reject) => {
+        try {
+          // 如果没有传入data参数,使用当前编辑器的内容
+          const content = data ? data.main : this.com.content;
+          
+          // 更新内容
+          this.com.content = content;
+          
+          // 等待同步完成
+          await this.$nextTick();
+          await this.syncContent(); 
+
+          // 触发更新事件并等待完成
+          this.$emit("onUpdate", this.currentIndex, this.com.content);
+          await this.$nextTick();
+
+          // 返回保存成功
+          resolve(true);
+        } catch (error) {
+          console.error('保存失败:', error);
+          reject(error);
+        }
+      });
     },
     getSave(isSaved) {
       // 处理保存状态
@@ -1233,27 +1244,26 @@ export default {
       // this.$emit("onUploadAttr",this.dataIndex,this.dataAttr);
     }, */
 
-    syncContent() {
+    async syncContent() {
       if (this.isEdit == "2" && this.com && this.com.content) {
         const formattedContent = this.formatContent(this.com.content);
-        this.replaceData(formattedContent).then((res) => {
-          this.content = res;
-          this.$nextTick(() => {
-            this.bindEvents();
-            this.initializeInputWidths();
-
-            // 确保变量和空格都正确显示
-            if (this.com.attrs) {
-              this.com.attrs.forEach((attr, index) => {
-                const element = this.$el.querySelector(`#${attr.id}`);
-                if (element) {
-                  element.value = attr.content || "";
-                  this.adjustInputWidth({ target: element });
-                }
-              });
+        const res = await this.replaceData(formattedContent);
+        this.content = res;
+        
+        await this.$nextTick();
+        
+        this.bindEvents();
+        this.initializeInputWidths();
+
+        if (this.com.attrs) {
+          this.com.attrs.forEach((attr, index) => {
+            const element = this.$el.querySelector(`#${attr.id}`);
+            if (element) {
+              element.value = attr.content || "";
+              this.adjustInputWidth({ target: element });
             }
           });
-        });
+        }
       }
     },
 

+ 78 - 51
ui/src/views/system/document/com/editor.vue

@@ -215,34 +215,42 @@ export default {
   },
   watch: {
     coms: {
-      handler(val) {
-        if (val == null) return;
-        // 处理拖拽数据,确保属性正确转换
-        this.comList = val.map((item) => {
-          // 首先创建基础对象
-          const processedItem = {
-            ...item,
-            // 确保 attrs 是数组
-            dcb_attrs: Array.isArray(item.attrs)
-              ? item.attrs
-              : JSON.parse(item.attrs || "[]"),
-            // 同步最新内容,优先使用当前显示的内容
-            content: item.content || item.dcb_nr || "",
-            dcb_nr: item.content || item.dcb_nr || "",
-            // 确保 type 使用 dcb_type
-            type: item.dcb_type || item.type,
-            // 确保 name 使用 dcb_name
-            name: item.dcb_name || item.name,
-          };
-          console.log("processedItem:", processedItem);
-          // 确保 content 和 dcb_nr 同步
-          if (processedItem.content !== processedItem.dcb_nr) {
-            processedItem.dcb_nr = processedItem.content;
-          }
+      async handler(val) {
+        // 如果正在保存,则跳过处理
+        if (this.isSaving || !val) return;
+
+        try {
+          // 处理拖拽数据,确保属性正确转换
+          const processedComs = val.map((item) => {
+            // 首先创建基础对象
+            const processedItem = {
+              ...item,
+              // 确保 attrs 是数组
+              dcb_attrs: Array.isArray(item.attrs)
+                ? item.attrs
+                : JSON.parse(item.attrs || "[]"),
+              // 同步最新内容,优先使用当前显示的内容
+              content: item.content || item.dcb_nr || "",
+              dcb_nr: item.content || item.dcb_nr || "",
+              // 确保 type 使用 dcb_type
+              type: item.dcb_type || item.type,
+              // 确保 name 使用 dcb_name
+              name: item.dcb_name || item.name,
+            };
+
+            // 确保 content 和 dcb_nr 同步
+            if (processedItem.content !== processedItem.dcb_nr) {
+              processedItem.dcb_nr = processedItem.content;
+            }
 
-          return processedItem;
-        });
-        this.$emit("onSetComs:", this.comList);
+            return processedItem;
+          });
+
+          this.comList = processedComs;
+          this.$emit("onSetComs:", this.comList);
+        } catch (error) {
+          console.error('处理组件数据时出错:', error);
+        }
       },
       immediate: true,
       deep: true,
@@ -290,6 +298,7 @@ export default {
         overflowY: "auto",
       },
       type: "",
+      isSaving: false, // 添加保存状态标志
     };
   },
   mounted() {
@@ -625,36 +634,54 @@ export default {
           });
         });
     },
-    onEdit(e, state) {
-      this.comList = this.comList.map((it, index) => {
-        it.isEdit = 2;
-
-        // 当切换到预览状态时(state === 2),触发保存
-        if (index === e && state === 2) {
-          // 找到对应的TextArea组件实例并调用save方法
-          this.$nextTick(() => {
-            console.log(111);
-            const componentRef = this.$refs[`component_${index}`];
-            if (componentRef && componentRef.save) {
-              componentRef.save();
-            }
-          });
+    async onEdit(e, state) {
+      try {
+        // 如果是切换到预览模式,先等待保存完成
+        if (state === 2) {
+          console.log("isEditor:",'001');
+          this.isSaving = true; // 设置保存状态
+          const componentRef = this.$refs[`component_${e}`];
+          if (componentRef && componentRef.save) {
+            // 等待保存完成
+            await componentRef.save();
+            
+            // 在保存完成后,手动更新组件状态
+            this.$set(this.comList[e], 'isEdit', 2);
+            
+            // 立即触发更新
+            await this.$nextTick();
+            
+            // 通知父组件更新
+            this.$emit("onRebuild", this.comList);
+            
+            this.isSaving = false; // 重置保存状态
+            return; // 直接返回,不执行后续的批量更新
+          }
         }
 
-        // 当切换到编辑状态时,对当前项进行 attrs 过滤
-        if (index === e && state === 1) {
-          this.filterAttrs(it);
+        // 如果是切换到编辑模式,执行原有逻辑
+        this.comList = this.comList.map((it, index) => {
+          it.isEdit = 2;
+          
+          if (index === e && state === 1) {
+            this.filterAttrs(it);
+          }
+          return it;
+        });
+
+        if (state === 1) {
+          this.comList[e].isEdit = 1;
+          this.onSetActive(e);
         }
 
-        return it;
-      });
+        this.$emit("onRebuild", this.comList);
 
-      if (state == 1) {
-        this.comList[e].isEdit = 1;
-        this.onSetActive(e);
+      } catch (error) {
+        console.error('保存失败:', error);
+        this.$message.error('切换模式失败,请重试');
+      } finally {
+        this.isSaving = false; // 确保在出错时也重置保存状态
       }
-      
-      this.$emit("onRebuild", this.comList);
     },
 
     // 新增 filterAttrs 方法