🎨 The heading block update time is refreshed after editing the blocks under the heading https://github.com/siyuan-note/siyuan/issues/11374

This commit is contained in:
Daniel 2024-05-13 17:16:55 +08:00
parent b2b1089e3d
commit d9471c6a83
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
2 changed files with 32 additions and 12 deletions

View file

@ -1241,7 +1241,7 @@ func (tx *Transaction) doSetAttrs(operation *Operation) (ret *TxErr) {
func refreshUpdated(node *ast.Node) {
updated := util.CurrentTimeSecondsStr()
node.SetIALAttr("updated", updated)
parents := treenode.ParentNodes(node)
parents := treenode.ParentNodesWithHeadings(node)
for _, parent := range parents { // 更新所有父节点的更新时间字段
parent.SetIALAttr("updated", updated)
}
@ -1256,7 +1256,7 @@ func createdUpdated(node *ast.Node) {
if updated < created {
updated = created // 复制粘贴块后创建时间小于更新时间 https://github.com/siyuan-note/siyuan/issues/3624
}
parents := treenode.ParentNodes(node)
parents := treenode.ParentNodesWithHeadings(node)
for _, parent := range parents { // 更新所有父节点的更新时间字段
parent.SetIALAttr("updated", updated)
cache.PutBlockIAL(parent.ID, parse.IAL2Map(parent.KramdownIAL))

View file

@ -18,8 +18,6 @@ package treenode
import (
"bytes"
"github.com/siyuan-note/siyuan/kernel/av"
"github.com/siyuan-note/siyuan/kernel/cache"
"strings"
"sync"
"text/template"
@ -34,7 +32,10 @@ import (
"github.com/88250/lute/parse"
"github.com/88250/lute/render"
"github.com/88250/vitess-sqlparser/sqlparser"
"github.com/emirpasic/gods/sets/hashset"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/av"
"github.com/siyuan-note/siyuan/kernel/cache"
"github.com/siyuan-note/siyuan/kernel/util"
)
@ -357,19 +358,38 @@ func CountBlockNodes(node *ast.Node) (ret int) {
return
}
func ParentNodes(node *ast.Node) (parents []*ast.Node) {
const maxDepth = 256
// ParentNodesWithHeadings 返回所有父级节点。
// 注意:返回的父级节点包括了标题节点,并且不保证父级层次顺序。
func ParentNodesWithHeadings(node *ast.Node) (parents []*ast.Node) {
const maxDepth = 255
i := 0
for n := node.Parent; nil != n; n = n.Parent {
i++
parents = append(parents, n)
if ast.NodeDocument == n.Type {
return
}
headingIDs := hashset.New()
for n := node; nil != n; n = n.Parent {
parent := n.Parent
if maxDepth < i {
logging.LogWarnf("parent nodes of node [%s] is too deep", node.ID)
return
}
i++
if nil == parent {
return
}
// 标题下方块编辑后刷新标题块更新时间
// The heading block update time is refreshed after editing the blocks under the heading https://github.com/siyuan-note/siyuan/issues/11374
parentHeadingLevel := 7
for prev := n.Previous; nil != prev; prev = prev.Previous {
if ast.NodeHeading == prev.Type && prev.HeadingLevel < parentHeadingLevel && !headingIDs.Contains(prev.ID) {
parents = append(parents, prev)
headingIDs.Add(prev.ID)
}
}
parents = append(parents, parent)
if ast.NodeDocument == parent.Type {
return
}
}
return
}