mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-20 00:20:12 +01:00
🎨 Improve rollback doc to avoid ID duplication https://github.com/siyuan-note/siyuan/issues/14358
This commit is contained in:
parent
57657b59ea
commit
d348ad0383
1 changed files with 48 additions and 13 deletions
|
|
@ -237,8 +237,8 @@ func RollbackDocHistory(boxID, historyPath string) (err error) {
|
||||||
|
|
||||||
srcPath := historyPath
|
srcPath := historyPath
|
||||||
var destPath, parentHPath string
|
var destPath, parentHPath string
|
||||||
id := util.GetTreeID(historyPath)
|
rootID := util.GetTreeID(historyPath)
|
||||||
workingDoc := treenode.GetBlockTree(id)
|
workingDoc := treenode.GetBlockTree(rootID)
|
||||||
if nil != workingDoc {
|
if nil != workingDoc {
|
||||||
if err = filelock.Remove(filepath.Join(util.DataDir, boxID, workingDoc.Path)); err != nil {
|
if err = filelock.Remove(filepath.Join(util.DataDir, boxID, workingDoc.Path)); err != nil {
|
||||||
return
|
return
|
||||||
|
|
@ -250,10 +250,6 @@ func RollbackDocHistory(boxID, historyPath string) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = filelock.CopyNewtimes(srcPath, destPath); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var avIDs []string
|
var avIDs []string
|
||||||
tree, _ := loadTree(srcPath, util.NewLute())
|
tree, _ := loadTree(srcPath, util.NewLute())
|
||||||
if nil != tree {
|
if nil != tree {
|
||||||
|
|
@ -283,14 +279,53 @@ func RollbackDocHistory(boxID, historyPath string) (err error) {
|
||||||
tree.Path = filepath.ToSlash(strings.TrimPrefix(destPath, util.DataDir+string(os.PathSeparator)+boxID))
|
tree.Path = filepath.ToSlash(strings.TrimPrefix(destPath, util.DataDir+string(os.PathSeparator)+boxID))
|
||||||
tree.HPath = parentHPath + "/" + tree.Root.IALAttr("title")
|
tree.HPath = parentHPath + "/" + tree.Root.IALAttr("title")
|
||||||
|
|
||||||
|
// 重置重复的块 ID https://github.com/siyuan-note/siyuan/issues/14358
|
||||||
|
if nil != workingDoc {
|
||||||
|
treenode.RemoveBlockTreesByRootID(rootID)
|
||||||
|
}
|
||||||
|
nodes := map[string]*ast.Node{}
|
||||||
|
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
||||||
|
if !entering || !n.IsBlock() {
|
||||||
|
return ast.WalkContinue
|
||||||
|
}
|
||||||
|
|
||||||
|
nodes[n.ID] = n
|
||||||
|
return ast.WalkContinue
|
||||||
|
})
|
||||||
|
var ids []string
|
||||||
|
for nodeID, _ := range nodes {
|
||||||
|
ids = append(ids, nodeID)
|
||||||
|
}
|
||||||
|
idMap := treenode.ExistBlockTrees(ids)
|
||||||
|
var duplicatedIDs []string
|
||||||
|
for nodeID, exist := range idMap {
|
||||||
|
if exist {
|
||||||
|
duplicatedIDs = append(duplicatedIDs, nodeID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, nodeID := range duplicatedIDs {
|
||||||
|
node := nodes[nodeID]
|
||||||
|
node.ID = ast.NewNodeID()
|
||||||
|
node.SetIALAttr("id", node.ID)
|
||||||
|
created := util.TimeFromID(node.ID)
|
||||||
|
updated := node.IALAttr("updated")
|
||||||
|
if "" == updated {
|
||||||
|
updated = created
|
||||||
|
}
|
||||||
|
if updated < created {
|
||||||
|
updated = created
|
||||||
|
}
|
||||||
|
node.SetIALAttr("updated", updated)
|
||||||
|
}
|
||||||
|
|
||||||
// 仅重新索引该文档,不进行全量索引
|
// 仅重新索引该文档,不进行全量索引
|
||||||
// Reindex only the current document after rolling back the document https://github.com/siyuan-note/siyuan/issues/12320
|
// Reindex only the current document after rolling back the document https://github.com/siyuan-note/siyuan/issues/12320
|
||||||
treenode.RemoveBlockTree(id)
|
sql.RemoveTreeQueue(rootID)
|
||||||
treenode.IndexBlockTree(tree)
|
if writeErr := indexWriteTreeIndexQueue(tree); nil != writeErr {
|
||||||
sql.RemoveTreeQueue(id)
|
return
|
||||||
sql.IndexTreeQueue(tree)
|
}
|
||||||
util.PushReloadFiletree()
|
util.PushReloadFiletree()
|
||||||
util.PushReloadProtyle(id)
|
util.PushReloadProtyle(rootID)
|
||||||
util.PushMsg(Conf.Language(102), 3000)
|
util.PushMsg(Conf.Language(102), 3000)
|
||||||
|
|
||||||
IncSync()
|
IncSync()
|
||||||
|
|
@ -303,12 +338,12 @@ func RollbackDocHistory(boxID, historyPath string) (err error) {
|
||||||
go func() {
|
go func() {
|
||||||
sql.FlushQueue()
|
sql.FlushQueue()
|
||||||
|
|
||||||
tree, _ = LoadTreeByBlockID(id)
|
tree, _ = LoadTreeByBlockID(rootID)
|
||||||
if nil == tree {
|
if nil == tree {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
refreshProtyle(id)
|
refreshProtyle(rootID)
|
||||||
|
|
||||||
// 刷新页签名
|
// 刷新页签名
|
||||||
refText := getNodeRefText(tree.Root)
|
refText := getNodeRefText(tree.Root)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue