From cf187dddcefe15814c8f02affca7959f813cbd1b Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Tue, 30 Apr 2024 21:33:50 +0800 Subject: [PATCH] :art: Add two shortcuts for block navigation https://github.com/siyuan-note/siyuan/issues/11193 --- kernel/api/block.go | 7 +++++-- kernel/api/router.go | 2 +- kernel/model/block.go | 36 ++++++++++++++++++------------------ 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/kernel/api/block.go b/kernel/api/block.go index 37611ce15..1e7ea0722 100644 --- a/kernel/api/block.go +++ b/kernel/api/block.go @@ -29,7 +29,7 @@ import ( "github.com/siyuan-note/siyuan/kernel/util" ) -func getParentNextChildID(c *gin.Context) { +func getBlockSiblingID(c *gin.Context) { ret := gulu.Ret.NewResult() defer c.JSON(http.StatusOK, ret) @@ -39,8 +39,11 @@ func getParentNextChildID(c *gin.Context) { } id := arg["id"].(string) + parent, previous, next := model.GetBlockSiblingID(id) ret.Data = map[string]string{ - "id": model.GetParentNextChildID(id), + "parent": parent, + "next": next, + "previous": previous, } } diff --git a/kernel/api/router.go b/kernel/api/router.go index 0b6e89593..6b5f53015 100644 --- a/kernel/api/router.go +++ b/kernel/api/router.go @@ -198,7 +198,7 @@ func ServeAPI(ginServer *gin.Engine) { ginServer.Handle("POST", "/api/block/getHeadingChildrenDOM", model.CheckAuth, getHeadingChildrenDOM) ginServer.Handle("POST", "/api/block/swapBlockRef", model.CheckAuth, model.CheckReadonly, swapBlockRef) ginServer.Handle("POST", "/api/block/transferBlockRef", model.CheckAuth, model.CheckReadonly, transferBlockRef) - ginServer.Handle("POST", "/api/block/getParentNextChildID", model.CheckAuth, getParentNextChildID) + ginServer.Handle("POST", "/api/block/getBlockSiblingID", model.CheckAuth, getBlockSiblingID) ginServer.Handle("POST", "/api/file/getFile", model.CheckAuth, getFile) ginServer.Handle("POST", "/api/file/putFile", model.CheckAuth, model.CheckReadonly, putFile) diff --git a/kernel/model/block.go b/kernel/model/block.go index 2fe6e7993..1c3774280 100644 --- a/kernel/model/block.go +++ b/kernel/model/block.go @@ -114,33 +114,33 @@ type Path struct { Created string `json:"created"` // 创建时间 } -func GetParentNextChildID(id string) string { +func GetBlockSiblingID(id string) (parent, previous, next string) { tree, err := LoadTreeByBlockID(id) if nil != err { - return "" + return } node := treenode.GetNodeInTree(tree, id) if nil == node { - return "" + return } - for p := node.Parent; nil != p; p = p.Parent { - if ast.NodeDocument == p.Type { - if nil != node.Next { - return node.Next.ID - } - return "" - } - - for f := p.Next; nil != f; f = f.Next { - // 遍历取下一个块级元素(比如跳过超级块 Close 节点) - if f.IsBlock() { - return f.ID - } - } + if !node.IsBlock() { + return } - return "" + + if nil != node.Parent && node.Parent.IsBlock() { + parent = node.Parent.ID + } + + if nil != node.Previous && node.Previous.IsBlock() { + previous = node.Previous.ID + } + + if nil != node.Next && node.Next.IsBlock() { + next = node.Next.ID + } + return } func IsBlockFolded(id string) bool {