Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Improve the behavior of `Jump to parent block` in lists https://github.com/siyuan-note/siyuan/issues/16516

Daniel3e9bd1547c7d
diff --git a/kernel/model/block.go b/kernel/model/block.go
index d8e650f5f2..9bb8e35cf1 100644
--- a/kernel/model/block.go
+++ b/kernel/model/block.go
@@ -206,66 +206,80 @@ func GetBlockSiblingID(id string) (parent, previous, next string) {
return
}
- if nil != current.Parent && ast.NodeListItem == current.Parent.Type {
- current = current.Parent
- }
-
- parentBlock := treenode.ParentBlock(current)
- if nil == parentBlock {
+ if !current.ParentIs(ast.NodeList) { // 当前块不在列表内的情况
+ parentBlock := treenode.ParentBlock(current)
+ if nil != parentBlock {
+ parent = parentBlock.ID
+ if nil != parentBlock.Previous {
+ previous = parentBlock.Previous.ID
+ }
+ if nil != parentBlock.Next {
+ next = parentBlock.Next.ID
+ }
+ }
return
}
- parent = parentBlock.ID
- if ast.NodeDocument == parentBlock.Type {
- parent = treenode.FirstLeafBlock(parentBlock).ID
-
- if nil != current.Previous && current.Previous.IsBlock() {
- previous = current.Previous.ID
- if flb := treenode.FirstChildBlock(current.Previous); nil != flb {
- previous = flb.ID
+ if ast.NodeListItem != current.Type && ast.NodeList != current.Parent.Type { // 当前块是列表内的块,但不是列表项或列表的情况
+ var listParent, listParent2 *ast.Node
+ listParentCount := 0
+ for parentBlock := treenode.ParentBlock(current); nil != parentBlock; parentBlock = treenode.ParentBlock(parentBlock) {
+ if ast.NodeListItem == parentBlock.Type {
+ listParentCount++
+ if 1 < listParentCount {
+ listParent2 = parentBlock
+ break
+ }
+ listParent = parentBlock
+ continue
}
}
- if nil != current.Next && current.Next.IsBlock() {
- next = current.Next.ID
- if flb := treenode.FirstChildBlock(current.Next); nil != flb {
- next = flb.ID
+ if 1 == listParentCount { // 列表只有一层的情况
+ if nil != listParent {
+ parent = listParent.ID
+ previous, next = getPreNext(listParent)
}
+ return
}
+
+ parent = listParent2.ID
+ previous, next = getPreNext(listParent)
return
}
- parentCount := 0
- for ; nil != parentBlock; parentBlock = treenode.ParentBlock(parentBlock) {
- if ast.NodeDocument == parentBlock.Type {
- break
- }
-
- if ast.NodeList == parentBlock.Type || ast.NodeBlockquote == parentBlock.Type || ast.NodeSuperBlock == parentBlock.Type || ast.NodeCallout == parentBlock.Type {
- parentCount++
- continue
- }
-
- if ast.NodeListItem == parentBlock.Type {
- if 1 > parentCount {
- parentBlock = treenode.ParentBlock(parentBlock)
- }
+ if ast.NodeListItem == current.Type {
+ // 当前块是列表项的情况
+ parentBlock := treenode.ParentBlock(current)
+ if nil != parentBlock {
parentBlock = treenode.ParentBlock(parentBlock)
}
- break
+ if nil != parentBlock {
+ parent = parentBlock.ID
+ previous, next = getPreNext(current)
+ }
+ return
}
- if ast.NodeDocument == parentBlock.Type {
- parentBlock = treenode.FirstLeafBlock(parentBlock)
- parent = parentBlock.ID
- } else {
+
+ // 当前块是列表的情况
+ parentBlock := treenode.ParentBlock(current)
+ if nil != parentBlock {
parent = parentBlock.ID
+ previous, next = getPreNext(current)
+ return
}
+ return
+}
- if nil != parentBlock.Previous {
- previous = parentBlock.Previous.ID
- }
- if nil != parentBlock.Next {
- next = parentBlock.Next.ID
+func getPreNext(parent *ast.Node) (previous, next string) {
+ if nil != parent {
+ if nil != parent.Previous {
+ previous = parent.Previous.ID
+ }
+ if nil != parent.Next {
+ next = parent.Next.ID
+ }
+ return
}
return
}