Преглед изворни кода

:art: 导出导入 .sy.zip 保持自定义排序 Fix https://github.com/siyuan-note/siyuan/issues/5939

Liang Ding пре 2 година
родитељ
комит
11aa06038b
1 измењених фајлова са 45 додато и 0 уклоњено
  1. 45 0
      kernel/model/import.go

+ 45 - 0
kernel/model/import.go

@@ -174,6 +174,51 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
 		}
 		}
 	}
 	}
 
 
+	// 合并 sort.json
+	fullSortIDs := map[string]int{}
+	sortIDs := map[string]int{}
+	var sortData []byte
+	var sortErr error
+	sortPath := filepath.Join(unzipRootPath, ".siyuan", "sort.json")
+	if gulu.File.IsExist(sortPath) {
+		sortData, sortErr = filelock.NoLockFileRead(sortPath)
+		if nil != sortErr {
+			logging.LogErrorf("read import sort conf failed: %s", sortErr)
+		}
+
+		if sortErr = gulu.JSON.UnmarshalJSON(sortData, &sortIDs); nil != sortErr {
+			logging.LogErrorf("unmarshal sort conf failed: %s", sortErr)
+		}
+
+		sortPath = filepath.Join(util.DataDir, boxID, ".siyuan", "sort.json")
+		if gulu.File.IsExist(sortPath) {
+			sortData, sortErr = filelock.NoLockFileRead(sortPath)
+			if nil != sortErr {
+				logging.LogErrorf("read box sort conf failed: %s", sortErr)
+			}
+
+			if sortErr = gulu.JSON.UnmarshalJSON(sortData, &fullSortIDs); nil != sortErr {
+				logging.LogErrorf("unmarshal box sort conf failed: %s", sortErr)
+			}
+		}
+
+		for oldID, sort := range sortIDs {
+			if newID := blockIDs[oldID]; "" != newID {
+				fullSortIDs[newID] = sort
+			}
+		}
+
+		sortData, sortErr = gulu.JSON.MarshalJSON(fullSortIDs)
+		if nil != sortErr {
+			logging.LogErrorf("marshal box sort conf failed: %s", sortErr)
+		} else {
+			sortErr = filelock.NoLockFileWrite(sortPath, sortData)
+			if nil != sortErr {
+				logging.LogErrorf("write box sort conf failed: %s", sortErr)
+			}
+		}
+	}
+
 	// 重命名文件路径
 	// 重命名文件路径
 	renamePaths := map[string]string{}
 	renamePaths := map[string]string{}
 	filepath.Walk(unzipRootPath, func(path string, info fs.FileInfo, err error) error {
 	filepath.Walk(unzipRootPath, func(path string, info fs.FileInfo, err error) error {