From 3e9bd1547c7d7a37561010175356955959d0baa0 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Tue, 16 Dec 2025 11:36:41 +0800 Subject: [PATCH] :art: Improve the behavior of `Jump to parent block` in lists https://github.com/siyuan-note/siyuan/issues/16516 Signed-off-by: Daniel <845765@qq.com> --- kernel/model/block.go | 110 ++++++++++++++++++++++++------------------ 1 file changed, 62 insertions(+), 48 deletions(-) diff --git a/kernel/model/block.go b/kernel/model/block.go index d8e650f5f..9bb8e35cf 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 { - 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 !current.ParentIs(ast.NodeList) { // 当前块不在列表内的情况 + parentBlock := treenode.ParentBlock(current) + if nil != parentBlock { + parent = parentBlock.ID + if nil != parentBlock.Previous { + previous = parentBlock.Previous.ID } - } - - if nil != current.Next && current.Next.IsBlock() { - next = current.Next.ID - if flb := treenode.FirstChildBlock(current.Next); nil != flb { - next = flb.ID + if nil != parentBlock.Next { + next = parentBlock.Next.ID } } 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 && 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 1 == listParentCount { // 列表只有一层的情况 + if nil != listParent { + parent = listParent.ID + previous, next = getPreNext(listParent) + } + return + } + + parent = listParent2.ID + previous, next = getPreNext(listParent) + return + } + + if ast.NodeListItem == current.Type { + // 当前块是列表项的情况 + parentBlock := treenode.ParentBlock(current) + if nil != parentBlock { parentBlock = treenode.ParentBlock(parentBlock) } - break - } - if ast.NodeDocument == parentBlock.Type { - parentBlock = treenode.FirstLeafBlock(parentBlock) - parent = parentBlock.ID - } else { - parent = parentBlock.ID + if nil != parentBlock { + parent = parentBlock.ID + previous, next = getPreNext(current) + } + return } - if nil != parentBlock.Previous { - previous = parentBlock.Previous.ID + // 当前块是列表的情况 + parentBlock := treenode.ParentBlock(current) + if nil != parentBlock { + parent = parentBlock.ID + previous, next = getPreNext(current) + return } - if nil != parentBlock.Next { - next = parentBlock.Next.ID + return +} + +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 }