Эх сурвалжийг харах

完善目录排序使其能够正常排列

yangg 4 сар өмнө
parent
commit
6e79a66b38

+ 68 - 1
ui/src/views/system/document/com/editor.vue

@@ -149,6 +149,7 @@ export default {
     "onLoadArticle",
     "onRebuild",
     "onSetComs",
+    "onCatalogUpdate",
   ],
   components: {
     Table,
@@ -294,6 +295,7 @@ export default {
       type: "",
       isSaving: false, // 添加保存状态标志
       extractedAttrs: [], // 添加新的数据属性来存储提取的 attrs
+      catalogData: [],
     };
   },
   mounted() {
@@ -878,7 +880,72 @@ export default {
     // 添加回之前删除的 onSetActive 方法
     onSetActive(e) {
       this.$emit("onSetActiveIndex", e);
-    }
+    },
+    updateCatalog(coms) {
+      try {
+        if (!Array.isArray(coms) || coms.length === 0) {
+          this.catalogData = [];
+          return;
+        }
+
+        let catalogData = [];
+        let levelMap = new Map();
+
+        coms.forEach((item, moduleIndex) => {
+          if (!item || !item.attrs) return;
+
+          const directoryAttrs = item.attrs
+            .filter(attr => attr && attr.type === "Directory")
+            .sort((a, b) => {
+              if (a.level !== b.level) return a.level - b.level;
+              return this.compareNumbers(a.number, b.number);
+            });
+
+          directoryAttrs.forEach(attr => {
+            if (!attr) return;
+
+            // 确保内容格式正确
+            const contentWithoutNumber = attr.content.replace(/^\d+(\.\d+)*\s*/, '').trim();
+            const catalogItem = {
+              id: `${moduleIndex}-${attr.id || ""}`,
+              label: `${attr.number} ${contentWithoutNumber}`,
+              level: attr.level || 1,
+              moduleIndex: moduleIndex,
+              number: attr.number,
+              children: [],
+              content: attr.content
+            };
+
+            if (attr.level === 1) {
+              catalogData.push(catalogItem);
+              levelMap.set(1, catalogItem);
+            } else {
+              const parentLevel = attr.level - 1;
+              const parent = levelMap.get(parentLevel);
+              
+              if (parent) {
+                if (!parent.children) parent.children = [];
+                parent.children.push(catalogItem);
+              }
+            }
+
+            levelMap.set(attr.level, catalogItem);
+          });
+        });
+
+        this.catalogData = this.sortCatalogDataRecursive(catalogData);
+
+        // 强制更新视图
+        this.$nextTick(() => {
+          this.$forceUpdate();
+          // 通知父组件目录已更新
+          this.$emit("onCatalogUpdate", this.catalogData);
+        });
+      } catch (error) {
+        console.error("Error updating catalog:", error);
+        this.catalogData = [];
+      }
+    },
   },
 };
 </script>

+ 28 - 20
ui/src/views/system/document/create.vue

@@ -3350,21 +3350,16 @@ export default {
 
     // 添加目录编号更新方法
     updateDirectoryNumbers() {
-      this.coms.forEach((com, moduleIndex) => {
-        if (com.attrs) {
-          // 获取当前模块中的所有目录类型属性
-          const directoryAttrs = com.attrs.filter(
-            (attr) => attr.type === "Directory"
-          );
-
-          // 更新每个目录的编号
-          directoryAttrs.forEach((dir, index) => {
-            // 生成新的编号
-            const number = this.generateLevelPrefix(moduleIndex, dir.level);
-            // 更新目录编号
-            this.$set(dir, "number", number);
-          });
-        }
+      this.coms.forEach((module, moduleIndex) => {
+        const directoryAttrs = module.attrs.filter(
+          (attr) => attr.type === "Directory"
+        );
+        directoryAttrs.forEach((dir, index) => {
+          // 生成新的编号
+          const number = this.generateLevelPrefix(moduleIndex, dir.level);
+          // 更新目录编号
+          this.$set(dir, "number", number);
+        });
       });
     },
 
@@ -3850,7 +3845,7 @@ export default {
               moduleIndex: moduleIndex,
               number: attr.number,
               children: [],
-              content:content
+              content:attr.content
             };
 
             // 根据层级放置节点
@@ -3886,7 +3881,7 @@ export default {
       }
     },
 
-    // 添加新方法:递归排序目录数据
+    // 修改 sortCatalogDataRecursive 方法
     sortCatalogDataRecursive(data) {
       if (!Array.isArray(data)) {
         return [];
@@ -3921,8 +3916,19 @@ export default {
           item.label = newLabel;
           item.content = newContent;
           
-          // 更新对应的组件属性
+          // 更新对应的组件中的目录内容
           this.updateComponentDirectory(item.moduleIndex, item.id, newContent, chapterNumber);
+        } else {
+          // 处理子目录的内容
+          const currentNumber = item.number;
+          const newContent = `${currentNumber} ${item.content.split(' ').slice(1).join(' ')}`;
+          
+          // 更新目录树节点
+          item.label = newContent;
+          item.content = newContent;
+          
+          // 更新对应的组件中的目录内容
+          this.updateComponentDirectory(item.moduleIndex, item.id, newContent, currentNumber);
         }
 
         // 递归处理子目录
@@ -3934,7 +3940,7 @@ export default {
       });
     },
 
-    // 添加新方法:更新组件中的目录属性
+    // 添加/修改 updateComponentDirectory 方法
     updateComponentDirectory(moduleIndex, directoryId, newContent, newNumber) {
       const module = this.coms[moduleIndex];
       if (!module || !module.attrs) return;
@@ -3942,12 +3948,14 @@ export default {
       // 更新 attrs 中的目录
       module.attrs.forEach(attr => {
         if (attr.type === "Directory" && attr.id === directoryId) {
+          // 更新目录属性
           attr.content = newContent;
           attr.number = String(newNumber);
           
           // 更新组件内容中的目录引用
           if (module.content) {
-            const pattern = new RegExp(`{{${attr.id}}}`, 'g');
+            // 使用正则表达式替换目录引用
+            const pattern = new RegExp(`(\\d+(\\.\\d+)*\\s*)?{{${attr.id}}}`, 'g');
             module.content = module.content.replace(pattern, `${newNumber} {{${attr.id}}}`);
           }
         }