Browse Source

处理目录问题

yangg 8 months ago
parent
commit
30342f03e3

File diff suppressed because it is too large
+ 0 - 0
dist/index.html


+ 0 - 0
dist/static/css/chunk-c5eeb98e.360612b2.css → dist/static/css/chunk-36bc9124.360612b2.css


File diff suppressed because it is too large
+ 0 - 0
dist/static/css/chunk-d64de3d6.9733ac4d.css


File diff suppressed because it is too large
+ 0 - 0
dist/static/js/app.bad222d9.js


File diff suppressed because it is too large
+ 0 - 0
dist/static/js/chunk-36bc9124.4d8834ba.js


File diff suppressed because it is too large
+ 0 - 0
dist/static/js/chunk-76923785.a196589a.js


File diff suppressed because it is too large
+ 0 - 0
dist/static/js/chunk-c5eeb98e.125d1a72.js


File diff suppressed because it is too large
+ 0 - 0
dist/static/js/chunk-d64de3d6.b020cd1d.js


+ 1 - 1
src/views/document/com/components/TextArea/index.vue

@@ -376,7 +376,7 @@ export default {
           let result = await _this.getRemote1(_this.com.attrs[i].formula);
           let result = await _this.getRemote1(_this.com.attrs[i].formula);
           data = data.replace("{{" + attrId + "}}", result);
           data = data.replace("{{" + attrId + "}}", result);
         } else if (_this.com.attrs[i].type == "Directory") {
         } else if (_this.com.attrs[i].type == "Directory") {
-          const directoryContent = _this.com.attrs[i].content;
+          const directoryContent = _this.com.attrs[i].number+'. '+_this.com.attrs[i].content;
           const level = _this.com.attrs[i].level || 1; // 默认为1级目录
           const level = _this.com.attrs[i].level || 1; // 默认为1级目录
           const attrId = _this.com.attrs[i].id;
           const attrId = _this.com.attrs[i].id;
 
 

+ 1 - 1
src/views/document/com/components/TextView/index.vue

@@ -186,7 +186,7 @@ export default {
           let result = await _this.getRemote1(_this.com.attrs[i].formula);
           let result = await _this.getRemote1(_this.com.attrs[i].formula);
           data = data.replace("{{" + attrId + "}}", result);
           data = data.replace("{{" + attrId + "}}", result);
         } else if (_this.com.attrs[i].type == "Directory") {
         } else if (_this.com.attrs[i].type == "Directory") {
-          const directoryContent = _this.com.attrs[i].content;
+          const directoryContent = _this.com.attrs[i].number+'. '+_this.com.attrs[i].content;;
           const level = _this.com.attrs[i].level || 1; // 默认为1级目录
           const level = _this.com.attrs[i].level || 1; // 默认为1级目录
           const attrId = _this.com.attrs[i].id;
           const attrId = _this.com.attrs[i].id;
 
 

+ 87 - 9
src/views/document/com/editor.vue

@@ -4,6 +4,7 @@
       v-model="comList"
       v-model="comList"
       @add="onAdd"
       @add="onAdd"
       @update="onDragEnd"
       @update="onDragEnd"
+      @end="onDragComplete"
       group="itxst"
       group="itxst"
       animation="300"
       animation="300"
       :style="draggableStyle"
       :style="draggableStyle"
@@ -260,7 +261,7 @@ export default {
       handler(val) {
       handler(val) {
         if (val == null) return;
         if (val == null) return;
         const newComList = JSON.parse(JSON.stringify(val));
         const newComList = JSON.parse(JSON.stringify(val));
-        
+
         // 比较新旧值,只有在真正变化时才更新
         // 比较新旧值,只有在真正变化时才更新
         if (JSON.stringify(this.comList) !== JSON.stringify(newComList)) {
         if (JSON.stringify(this.comList) !== JSON.stringify(newComList)) {
           this.comList = newComList;
           this.comList = newComList;
@@ -268,7 +269,7 @@ export default {
         /*  console.log("val", val);
         /*  console.log("val", val);
         this.comList = JSON.parse(JSON.stringify(val));
         this.comList = JSON.parse(JSON.stringify(val));
         console.log("comList", this.comList); */
         console.log("comList", this.comList); */
-      /*   console.log("val", val);
+        /*   console.log("val", val);
         this.comList = this.carefulCopy(val);
         this.comList = this.carefulCopy(val);
         console.log("comList", this.comList); */
         console.log("comList", this.comList); */
       },
       },
@@ -324,26 +325,85 @@ export default {
     this.initCategoryList();
     this.initCategoryList();
     this.type = this.$route.query.type;
     this.type = this.$route.query.type;
     this.templateId = this.$route.query.templateId;
     this.templateId = this.$route.query.templateId;
+    this.$nextTick(() => {
+      this.updateDirectoryNumbers(); // 初始化目录序号
+    });
   },
   },
   methods: {
   methods: {
+    /* 拖拽 修改目录序号*/
+    onDragComplete() {
+      console.log("拖动结束");
+      this.$nextTick(() => {
+        this.updateDirectoryNumbers();
+        this.$emit("onRebuild", this.comList);
+      });
+    },
+
+    updateDirectoryNumbers() {
+      let levelCounters = {};
+      this.comList.forEach((module, moduleIndex) => {
+        if (module.attrs) {
+          module.attrs.forEach((attr) => {
+            if (attr.type === "Directory") {
+              const newNumber = this.generateNumberedContent(
+                moduleIndex,
+                attr.level,
+                levelCounters
+              );
+
+              // 只更新 number 属性,完全不触及 content
+              if (!attr.hasOwnProperty("number")) {
+                this.$set(attr, "number", newNumber);
+              } else {
+                attr.number = newNumber;
+              }
+            }
+          });
+        }
+      });
+    },
+
+    generateNumberedContent(moduleIndex, level, levelCounters) {
+      return this.generateLevelPrefix(moduleIndex, level, levelCounters);
+    },
+
+    generateLevelPrefix(moduleIndex, level, levelCounters) {
+      let prefix = [];
+
+      for (let i = 1; i <= level; i++) {
+        if (i === 1) {
+          prefix.push(moduleIndex + 1);
+        } else {
+          if (!levelCounters[i]) {
+            levelCounters[i] = 1;
+          } else {
+            levelCounters[i]++;
+          }
+          prefix.push(levelCounters[i]);
+        }
+      }
+
+      return prefix.join(".");
+    },
+
     carefulCopy(items) {
     carefulCopy(items) {
-      return items.map(item => {
+      return items.map((item) => {
         const newItem = { ...item };
         const newItem = { ...item };
-        
+
         // 深拷贝 attrs 数组
         // 深拷贝 attrs 数组
         if (Array.isArray(newItem.attrs)) {
         if (Array.isArray(newItem.attrs)) {
-          newItem.attrs = newItem.attrs.map(attr => ({...attr}));
+          newItem.attrs = newItem.attrs.map((attr) => ({ ...attr }));
         }
         }
 
 
         // 深拷贝 content,如果它是一个对象或数组
         // 深拷贝 content,如果它是一个对象或数组
-        if (typeof newItem.content === 'object' && newItem.content !== null) {
+        if (typeof newItem.content === "object" && newItem.content !== null) {
           newItem.content = JSON.parse(JSON.stringify(newItem.content));
           newItem.content = JSON.parse(JSON.stringify(newItem.content));
         }
         }
 
 
         // 处理特殊的 type,如 Table
         // 处理特殊的 type,如 Table
-        if (newItem.type === 'Table') {
+        if (newItem.type === "Table") {
           newItem.tableHeader = [...newItem.tableHeader];
           newItem.tableHeader = [...newItem.tableHeader];
-          newItem.tableData = newItem.tableData.map(row => ({...row}));
+          newItem.tableData = newItem.tableData.map((row) => ({ ...row }));
         }
         }
 
 
         // 保留其他属性的引用
         // 保留其他属性的引用
@@ -574,10 +634,17 @@ export default {
       this.$emit("onDelete", index);
       this.$emit("onDelete", index);
     },
     },
     onEdit(e, state) {
     onEdit(e, state) {
-      this.comList = this.comList.map((it) => {
+      this.comList = this.comList.map((it, index) => {
         it.isEdit = 2;
         it.isEdit = 2;
+
+        // 当切换到编辑状态时,对当前项进行 attrs 过滤
+        if (index === e && state === 1) {
+          this.filterAttrs(it);
+        }
+
         return it;
         return it;
       });
       });
+
       if (state == 1) {
       if (state == 1) {
         this.comList[e].isEdit = 1;
         this.comList[e].isEdit = 1;
         this.onSetActive(e);
         this.onSetActive(e);
@@ -585,6 +652,17 @@ export default {
 
 
       this.$emit("onRebuild", this.comList);
       this.$emit("onRebuild", this.comList);
     },
     },
+
+    // 新增 filterAttrs 方法
+    filterAttrs(item) {
+      // 从 content 中提取所有 {{}} 包裹的 ID
+      const contentIds = (item.content.match(/{{([^}]+)}}/g) || []).map(
+        (match) => match.slice(2, -2).trim()
+      );
+
+      // 过滤 attrs,只保留在 content 中出现的 ID
+      item.attrs = item.attrs.filter((attr) => contentIds.includes(attr.id));
+    },
     onAdd(e) {
     onAdd(e) {
       e.preventDefault();
       e.preventDefault();
       e.stopPropagation();
       e.stopPropagation();

+ 19 - 17
src/views/document/create.vue

@@ -1784,7 +1784,7 @@ export default {
       let currentLevel = e.content.level || 1;
       let currentLevel = e.content.level || 1;
 
 
       // 生成带有模块索引和层级的内容
       // 生成带有模块索引和层级的内容
-      let numberedContent = this.generateNumberedContent(
+      let { number, content } = this.generateNumberedContent(
         _this.comIndex,
         _this.comIndex,
         currentLevel
         currentLevel
       );
       );
@@ -1794,7 +1794,8 @@ export default {
         id: uniqueId,
         id: uniqueId,
         name: "目录信息",
         name: "目录信息",
         intro: "目录信息",
         intro: "目录信息",
-        content: numberedContent,
+        number: number,
+        content: content,
         level: currentLevel,
         level: currentLevel,
         moduleIndex: _this.comIndex,
         moduleIndex: _this.comIndex,
       };
       };
@@ -1808,11 +1809,7 @@ export default {
       // 使用 nextTick 确保 DOM 更新后再检查
       // 使用 nextTick 确保 DOM 更新后再检查
       this.$nextTick(() => {
       this.$nextTick(() => {
         if (!com.attrs[com.attrs.length - 1].content) {
         if (!com.attrs[com.attrs.length - 1].content) {
-          this.$set(
-            com.attrs[com.attrs.length - 1],
-            "content",
-            numberedContent
-          );
+          this.$set(com.attrs[com.attrs.length - 1], "content", content);
           this.$forceUpdate();
           this.$forceUpdate();
         }
         }
       });
       });
@@ -1824,14 +1821,11 @@ export default {
     },
     },
 
 
     generateNumberedContent(moduleIndex, level) {
     generateNumberedContent(moduleIndex, level) {
-      let numberedContent = "";
-      let prefix = this.generateLevelPrefix(moduleIndex, level);
-
-      if (this.coms[moduleIndex].type === "TextArea") {
-        numberedContent = `${prefix}\n`;
-      }
+      let number = this.generateLevelPrefix(moduleIndex, level);
+      let content =
+        this.coms[moduleIndex].type === "TextArea" ? "" : "";
 
 
-      return numberedContent.trim();
+      return { number, content };
     },
     },
 
 
     generateLevelPrefix(moduleIndex, level) {
     generateLevelPrefix(moduleIndex, level) {
@@ -1858,9 +1852,7 @@ export default {
           let parentLevel = existingDirectories
           let parentLevel = existingDirectories
             .filter((dir) => dir.level === i - 1)
             .filter((dir) => dir.level === i - 1)
             .pop();
             .pop();
-          prefix.push(
-            parentLevel ? parentLevel.content.split(" ")[0].split(".").pop() : 1
-          );
+          prefix.push(parentLevel ? parentLevel.number.split(".")[i - 1] : 1);
         }
         }
       }
       }
 
 
@@ -2157,7 +2149,17 @@ export default {
       if (res.status != 200) return [];
       if (res.status != 200) return [];
 
 
       let dataList = res.data.dataList.map((item) => {
       let dataList = res.data.dataList.map((item) => {
+        // 解析 attrs
         item.attrs = JSON.parse(item.attrs);
         item.attrs = JSON.parse(item.attrs);
+
+        // 从 content 中提取所有 {{}} 包裹的 ID
+        const contentIds = (item.content.match(/{{([^}]+)}}/g) || []).map(
+          (match) => match.slice(2, -2).trim()
+        );
+
+        // 过滤 attrs,只保留在 content 中出现的 ID
+        item.attrs = item.attrs.filter((attr) => contentIds.includes(attr.id));
+
         return item;
         return item;
       });
       });
       return dataList;
       return dataList;

Some files were not shown because too many files changed in this diff