yangg преди 5 месеца
родител
ревизия
f2e8539741
променени са 1 файла, в които са добавени 76 реда и са изтрити 52 реда
  1. 76 52
      ui/src/views/system/document/create.vue

+ 76 - 52
ui/src/views/system/document/create.vue

@@ -1772,11 +1772,27 @@ export default {
       this.operation = "drag";
     },
 
-    onRebuild(updatedComList) {
-      this.coms = [...updatedComList];
-      this.$nextTick(() => {
-        this.$forceUpdate();
+    onRebuild(comList) {
+      // 深拷贝以避免引用问题
+      const newComList = JSON.parse(JSON.stringify(comList));
+      
+      // 确保每个组件的属性都被正确保留
+      newComList.forEach(com => {
+        com.attrs = com.attrs || [];
+        com.dcb_attrs = com.dcb_attrs || [];
+        
+        // 合并属性,避免重复
+        if (com.dcb_attrs.length > 0) {
+          const existingIds = new Set(com.attrs.map(a => a.id));
+          com.dcb_attrs.forEach(attr => {
+            if (!existingIds.has(attr.id)) {
+              com.attrs.push(attr);
+            }
+          });
+        }
       });
+
+      this.replaceData(newComList);
     },
     //初始化当前用户信息
     async initCurrentUser() {
@@ -3369,61 +3385,43 @@ export default {
 
     // 编辑/预览切换
     async onEdit(index, state) {
-      this.comIndex = index;
-      console.log(this.coms);
       try {
-        if (state === 2) {
-          // 切换到预览模式
+        if (this.isSaving) return;
           this.isSaving = true;
 
-          // 获取对应模块的组件引用
-          const componentRef = this.$refs.editor?.$refs[`component_${index}`];
-
-          // 添加防护检查
-          if (!componentRef?.[0]) {
-            console.warn(`组件引用不存在: component_${index},跳过保存操作`);
-            // 仍然允许状态切换,但跳过保存步骤
-            this.$set(this.coms[index], "isEdit", 2);
-            await this.$nextTick();
-            this.onRebuild(this.coms);
-            return;
-          }
+        // 保存当前组件的完整数据
+        const currentCom = JSON.parse(JSON.stringify(this.coms[index]));
 
-          try {
-            // 1. 保存组件内容
+        if (state === 2) {
+          // 从编辑切换到预览
+          const componentRef = this.$refs[`textArea${index}`];
+          if (componentRef && componentRef[0]) {
             await componentRef[0].save();
-
-            // 2. 更新编辑状态
-            this.$set(this.coms[index], "isEdit", 2);
-
-            // 3. 确保视图更新完成
-            await this.$nextTick();
-
-            // 4. 触发重建
-            this.onRebuild(this.coms);
-          } catch (error) {
-            console.error("保存过程中出错:", error);
-            this.$message.error("保存失败:" + error.message);
-            throw error;
           }
-        } else {
-          // 切换到编辑模式
-          // 更新所有组件的编辑状态
+        }
+
+        // 更新编辑状态
           this.coms = this.coms.map((it, idx) => {
-            it.isEdit = 2;
-            if (idx === index && state === 1) {
-              this.filterAttrs(it);
-              it.isEdit = 1;
-            }
-            return it;
-          });
+          if (idx === index) {
+            return {
+              ...currentCom, // 保持原有属性
+              isEdit: state,
+              attrs: currentCom.attrs || [], // 确保attrs不会丢失
+              dcb_attrs: currentCom.dcb_attrs || [] // 确保dcb_attrs不会丢失
+            };
+          }
+          return {
+            ...it,
+            isEdit: 2
+          };
+        });
 
-          // 更新当前选中的组件索引
+        // 更新当前索引
           this.comIndex = index;
 
           // 触发重建
-          this.onRebuild(this.coms);
-        }
+        await this.onRebuild(this.coms);
+
       } catch (error) {
         console.error("切换模式失败:", error);
         this.$message.error("切换模式失败:" + error.message);
@@ -3453,10 +3451,36 @@ export default {
 
     // 过滤属性
     filterAttrs(item) {
-      const contentIds = (item.dcb_nr.match(/{{([^}]+)}}/g) || []).map(
-        (match) => match.slice(2, -2).trim()
-      );
-      item.attrs = item.attrs.filter((attr) => contentIds.includes(attr.id));
+      if (!item.content && !item.dcb_nr) return;
+
+      // 保存原始属性
+      const originalAttrs = item.attrs || [];
+      const originalDcbAttrs = item.dcb_attrs || [];
+
+      // 从内容中提取ID
+      const contentIds = ((item.content || item.dcb_nr).match(/{{([^}]+)}}/g) || [])
+        .map(match => match.slice(2, -2).trim());
+
+      if (contentIds.length > 0) {
+        // 只过滤掉确实不存在的ID
+        item.attrs = originalAttrs.filter(attr => 
+          attr && (
+            contentIds.includes(attr.id) || 
+            attr.type === "ProductAttr" || // 保留特殊类型属性
+            attr.type === "variableNull"
+          )
+        );
+        
+        if (item.dcb_attrs) {
+          item.dcb_attrs = originalDcbAttrs.filter(attr =>
+            attr && (
+              contentIds.includes(attr.id) ||
+              attr.type === "ProductAttr" ||
+              attr.type === "variableNull"
+            )
+          );
+        }
+      }
     },
 
     handleDragStart(event, data) {