|
@@ -183,18 +183,15 @@ export default {
|
|
|
watch: {
|
|
|
coms: {
|
|
|
handler(val) {
|
|
|
- // 如果正在保存或没有值,则跳过处理
|
|
|
if (this.isSaving || !val || !Array.isArray(val)) return;
|
|
|
|
|
|
try {
|
|
|
- // 检查数组长度变化或内容更新
|
|
|
const needsUpdate =
|
|
|
- val.length !== this.comList.length || // 检查长度变化
|
|
|
+ val.length !== this.comList.length ||
|
|
|
val.some((item, index) => {
|
|
|
const currentCom = this.comList[index];
|
|
|
if (!currentCom) return true;
|
|
|
|
|
|
- // 比较关键属性是否发生变化
|
|
|
return (
|
|
|
item.content !== currentCom.content ||
|
|
|
item.dcb_nr !== currentCom.dcb_nr ||
|
|
@@ -205,9 +202,20 @@ export default {
|
|
|
);
|
|
|
});
|
|
|
|
|
|
- // 如果不需要更新,直接返回
|
|
|
if (!needsUpdate) return;
|
|
|
- // 处理数据,确保属性正确转换
|
|
|
+
|
|
|
+ // 修改提取逻辑,只保留 type 为 "Directory" 的属性
|
|
|
+ const allDirectoryAttrs = val.reduce((acc, item) => {
|
|
|
+ if (item.attrs && Array.isArray(item.attrs)) {
|
|
|
+ const directoryAttrs = item.attrs.filter(attr => attr.type === "Directory");
|
|
|
+ acc.push(...directoryAttrs);
|
|
|
+ }
|
|
|
+ return acc;
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ // 更新提取的目录属性
|
|
|
+ this.$set(this, 'extractedAttrs', allDirectoryAttrs);
|
|
|
+ console.log("extractedAttrs:",this.extractedAttrs);
|
|
|
const processedComs = val.map((item) => ({
|
|
|
...item,
|
|
|
attrs: Array.isArray(item.attrs) ? item.attrs : [],
|
|
@@ -222,21 +230,13 @@ export default {
|
|
|
name: item.dcb_name || item.name,
|
|
|
}));
|
|
|
|
|
|
- // 使用 Vue.set 更新整个数组
|
|
|
this.$set(this, 'comList', processedComs);
|
|
|
-
|
|
|
- // 触发父组件更新
|
|
|
this.$emit("onSetComs", this.comList);
|
|
|
-
|
|
|
- // 如果长度减少,可能需要更新其他相关状态
|
|
|
- if (val.length < this.comList.length) {
|
|
|
- this.$nextTick(() => {
|
|
|
- // 更新目录序号
|
|
|
- this.updateDirectoryNumbers();
|
|
|
- // 触发重建
|
|
|
- this.$emit("onRebuild", this.comList);
|
|
|
- });
|
|
|
- }
|
|
|
+
|
|
|
+ // 在提取完目录属性后更新编号
|
|
|
+ this.$nextTick(() => {
|
|
|
+ this.updateDirectoryNumbers();
|
|
|
+ });
|
|
|
|
|
|
} catch (error) {
|
|
|
console.error('处理组件数据时出错:', error);
|
|
@@ -292,6 +292,7 @@ export default {
|
|
|
},
|
|
|
type: "",
|
|
|
isSaving: false, // 添加保存状态标志
|
|
|
+ extractedAttrs: [], // 添加新的数据属性来存储提取的 attrs
|
|
|
};
|
|
|
},
|
|
|
mounted() {
|
|
@@ -308,56 +309,127 @@ export default {
|
|
|
onDragComplete() {
|
|
|
console.log("拖动结束");
|
|
|
this.$nextTick(() => {
|
|
|
- this.updateDirectoryNumbers();
|
|
|
- this.$emit("onRebuild", this.comList);
|
|
|
+ // 确保在 DOM 更新后再更新目录编号
|
|
|
+ setTimeout(() => {
|
|
|
+ this.updateDirectoryNumbers();
|
|
|
+ this.$emit("onRebuild", this.comList);
|
|
|
+ }, 0);
|
|
|
});
|
|
|
},
|
|
|
|
|
|
updateDirectoryNumbers() {
|
|
|
- let levelCounters = {};
|
|
|
- this.comList.forEach((module, moduleIndex) => {
|
|
|
- if (module.attrs) {
|
|
|
- module.attrs.forEach((attr) => {
|
|
|
+ // 按层级分组
|
|
|
+ const levelGroups = {};
|
|
|
+
|
|
|
+ // 第一遍遍历:收集并按层级分组
|
|
|
+ this.extractedAttrs.forEach((attr, index) => {
|
|
|
+ if (attr.type === "Directory") {
|
|
|
+ if (!levelGroups[attr.level]) {
|
|
|
+ levelGroups[attr.level] = [];
|
|
|
+ }
|
|
|
+ attr.originalIndex = index;
|
|
|
+ attr.originalContent = attr.content.replace(/^[\d.]+\s*/, '').trim();
|
|
|
+ levelGroups[attr.level] = [...levelGroups[attr.level], attr];
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 处理所有层级的目录
|
|
|
+ Object.keys(levelGroups)
|
|
|
+ .sort((a, b) => Number(a) - Number(b))
|
|
|
+ .forEach(level => {
|
|
|
+ const attrs = levelGroups[level];
|
|
|
+
|
|
|
+ if (level === '1') {
|
|
|
+ // 处理一级目录
|
|
|
+ attrs.forEach((attr, index) => {
|
|
|
+ attr.number = (index + 1).toString();
|
|
|
+ attr.content = `${attr.number} ${attr.originalContent}`;
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ // 处理其他层级目录
|
|
|
+ attrs.forEach((attr) => {
|
|
|
+ // 找到该目录前面最近的上级目录
|
|
|
+ const parentLevel = (parseInt(level) - 1).toString();
|
|
|
+ let previousParent = null;
|
|
|
+
|
|
|
+ for (const parentAttr of levelGroups[parentLevel] || []) {
|
|
|
+ if (parentAttr.originalIndex < attr.originalIndex) {
|
|
|
+ previousParent = parentAttr;
|
|
|
+ } else {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (previousParent) {
|
|
|
+ // 找到同属于这个上级目录下的所有同级目录
|
|
|
+ const siblings = attrs.filter(a => {
|
|
|
+ // 找到下一个同级别的父级目录
|
|
|
+ const nextParent = levelGroups[parentLevel].find(p =>
|
|
|
+ p.originalIndex > previousParent.originalIndex
|
|
|
+ );
|
|
|
+ const nextParentIndex = nextParent ? nextParent.originalIndex : Infinity;
|
|
|
+
|
|
|
+ return a.originalIndex > previousParent.originalIndex
|
|
|
+ && a.originalIndex < nextParentIndex;
|
|
|
+ });
|
|
|
+
|
|
|
+ // 计算在当前层级中的位置
|
|
|
+ const siblingIndex = siblings.indexOf(attr) + 1;
|
|
|
+
|
|
|
+ // 构建编号
|
|
|
+ attr.number = `${previousParent.number}.${siblingIndex}`;
|
|
|
+ attr.content = `${attr.number} ${attr.originalContent}`;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 更新原始数据中的目录内容
|
|
|
+ this.comList.forEach(com => {
|
|
|
+ if (com.attrs) {
|
|
|
+ com.attrs.forEach(attr => {
|
|
|
if (attr.type === "Directory") {
|
|
|
- const newNumber = this.generateNumberedContent(
|
|
|
- moduleIndex,
|
|
|
- attr.level,
|
|
|
- levelCounters
|
|
|
- );
|
|
|
+ const updatedAttr = this.extractedAttrs.find(a => {
|
|
|
+ const cleanAttrContent = attr.content.replace(/^[\d.]+\s*/, '').trim();
|
|
|
+ const cleanUpdatedContent = a.originalContent;
|
|
|
+ return a.level === attr.level && cleanAttrContent === cleanUpdatedContent;
|
|
|
+ });
|
|
|
|
|
|
- // 只更新 number 属性,完全不触及 content
|
|
|
- if (!attr.hasOwnProperty("number")) {
|
|
|
- this.$set(attr, "number", newNumber);
|
|
|
- } else {
|
|
|
- attr.number = newNumber;
|
|
|
+ if (updatedAttr) {
|
|
|
+ attr.number = updatedAttr.number;
|
|
|
+ attr.content = updatedAttr.content;
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
- },
|
|
|
|
|
|
- generateNumberedContent(moduleIndex, level, levelCounters) {
|
|
|
- return this.generateLevelPrefix(moduleIndex, level, levelCounters);
|
|
|
+ // 清理临时属性
|
|
|
+ this.extractedAttrs.forEach(attr => {
|
|
|
+ delete attr.originalIndex;
|
|
|
+ delete attr.originalContent;
|
|
|
+ });
|
|
|
},
|
|
|
|
|
|
- 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(".");
|
|
|
+ // 根据内容查找父级目录
|
|
|
+ findParentByContent(currentAttr, parentAttrs) {
|
|
|
+ // 移除当前目录内容中的编号和前缀空格
|
|
|
+ const currentContent = currentAttr.originalContent.trim();
|
|
|
+
|
|
|
+ // 获取当前目录的编号前缀(如果存在)
|
|
|
+ const currentNumberPrefix = currentAttr.number ? currentAttr.number.split('.')[0] : '';
|
|
|
+
|
|
|
+ // 查找最匹配的父级目录
|
|
|
+ return parentAttrs.find(parent => {
|
|
|
+ // 移除父级目录内容中的编号和前缀空格
|
|
|
+ const parentContent = parent.originalContent.trim();
|
|
|
+ const parentNumberPrefix = parent.number ? parent.number.split('.')[0] : '';
|
|
|
+
|
|
|
+ // 检查编号前缀是否匹配
|
|
|
+ // 只有当当前目录的编号前缀与父级目录的编号相同时,才认为是父子关系
|
|
|
+ return currentContent.startsWith(parentContent + '.') &&
|
|
|
+ currentNumberPrefix === parentNumberPrefix;
|
|
|
+ });
|
|
|
},
|
|
|
|
|
|
carefulCopy(items) {
|
|
@@ -867,3 +939,4 @@ export default {
|
|
|
width: 200px;
|
|
|
}
|
|
|
</style>
|
|
|
+
|