Преглед изворни кода

:art: Add two shortcuts for block navigation https://github.com/siyuan-note/siyuan/issues/11193

Daniel пре 1 година
родитељ
комит
cf187dddce
3 измењених фајлова са 23 додато и 20 уклоњено
  1. 5 2
      kernel/api/block.go
  2. 1 1
      kernel/api/router.go
  3. 17 17
      kernel/model/block.go

+ 5 - 2
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,
 	}
 }
 

+ 1 - 1
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)

+ 17 - 17
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 ""
-		}
+	if !node.IsBlock() {
+		return
+	}
 
-		for f := p.Next; nil != f; f = f.Next {
-			// 遍历取下一个块级元素(比如跳过超级块 Close 节点)
-			if f.IsBlock() {
-				return f.ID
-			}
-		}
+	if nil != node.Parent && node.Parent.IsBlock() {
+		parent = node.Parent.ID
+	}
+
+	if nil != node.Previous && node.Previous.IsBlock() {
+		previous = node.Previous.ID
 	}
-	return ""
+
+	if nil != node.Next && node.Next.IsBlock() {
+		next = node.Next.ID
+	}
+	return
 }
 
 func IsBlockFolded(id string) bool {