瀏覽代碼

Merge remote-tracking branch 'origin/dev' into dev

Vanessa 2 年之前
父節點
當前提交
65dc60a9fe
共有 6 個文件被更改,包括 149 次插入2 次删除
  1. 40 1
      API.md
  2. 39 0
      API_zh_CN.md
  3. 17 0
      kernel/api/block.go
  4. 1 0
      kernel/api/router.go
  5. 0 1
      kernel/model/bazzar.go
  6. 52 0
      kernel/model/block.go

+ 40 - 1
API.md

@@ -29,6 +29,7 @@
     * [Delete a block](#Delete-a-block)
     * [Move a block](#Move-a-block)
     * [Get a block kramdown](#Get-a-block-kramdown)
+    * [Get child blocks](#get-child-blocks)
 * [Attributes](#Attributes)
     * [Set block attributes](#Set-block-attributes)
     * [Get block attributes](#Get-block-attributes)
@@ -769,6 +770,44 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
   }
   ```
 
+### Get child blocks
+
+* `/api/block/getChildBlocks`
+* Parameters
+
+  ```json
+  {
+    "id": "20230506212712-vt9ajwj"
+  }
+  ```
+
+  * `id`: Parent block ID
+  * The blocks below a heading are also counted as child blocks
+* Return value
+
+  ```json
+  {
+    "code": 0,
+    "msg": "",
+    "data": [
+      {
+        "id": "20230512083858-mjdwkbn",
+        "type": "h",
+        "subType": "h1"
+      },
+      {
+        "id": "20230513213727-thswvfd",
+        "type": "s"
+      },
+      {
+        "id": "20230513213633-9lsj4ew",
+        "type": "l",
+        "subType": "u"
+      }
+    ]
+  }
+  ```
+
 ## Attributes
 
 ### Set block attributes
@@ -880,7 +919,7 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
   }
   ```
 
-### 渲染 Sprig
+### Render Sprig
 
 * `/api/template/renderSprig`
 * Parameters

+ 39 - 0
API_zh_CN.md

@@ -29,6 +29,7 @@
     * [删除块](#删除块)
     * [移动块](#移动块)
     * [获取块 kramdown 源码](#获取块-kramdown-源码)
+    * [获取子块](#获取子块)
 * [属性](#属性)
     * [设置块属性](#设置块属性)
     * [获取块属性](#获取块属性)
@@ -762,6 +763,44 @@
   }
   ```
 
+### 获取子块
+
+* `/api/block/getChildBlocks`
+* 参数
+
+  ```json
+  {
+    "id": "20230506212712-vt9ajwj"
+  }
+  ```
+
+  * `id`:父块 ID
+  * 标题下方块也算作子块
+* 返回值
+
+  ```json
+  {
+    "code": 0,
+    "msg": "",
+    "data": [
+      {
+        "id": "20230512083858-mjdwkbn",
+        "type": "h",
+        "subType": "h1"
+      },
+      {
+        "id": "20230513213727-thswvfd",
+        "type": "s"
+      },
+      {
+        "id": "20230513213633-9lsj4ew",
+        "type": "l",
+        "subType": "u"
+      }
+    ]
+  }
+  ```
+
 ## 属性
 
 ### 设置块属性

+ 17 - 0
kernel/api/block.go

@@ -481,3 +481,20 @@ func getBlockKramdown(c *gin.Context) {
 		"kramdown": kramdown,
 	}
 }
+
+func getChildBlocks(c *gin.Context) {
+	ret := gulu.Ret.NewResult()
+	defer c.JSON(http.StatusOK, ret)
+
+	arg, ok := util.JsonArg(c, ret)
+	if !ok {
+		return
+	}
+
+	id := arg["id"].(string)
+	if util.InvalidIDPattern(id, ret) {
+		return
+	}
+
+	ret.Data = model.GetChildBlocks(id)
+}

+ 1 - 0
kernel/api/router.go

@@ -147,6 +147,7 @@ func ServeAPI(ginServer *gin.Engine) {
 	ginServer.Handle("POST", "/api/block/getBlockInfo", model.CheckAuth, getBlockInfo)
 	ginServer.Handle("POST", "/api/block/getBlockDOM", model.CheckAuth, getBlockDOM)
 	ginServer.Handle("POST", "/api/block/getBlockKramdown", model.CheckAuth, getBlockKramdown)
+	ginServer.Handle("POST", "/api/block/getChildBlocks", model.CheckAuth, getChildBlocks)
 	ginServer.Handle("POST", "/api/block/getBlockBreadcrumb", model.CheckAuth, getBlockBreadcrumb)
 	ginServer.Handle("POST", "/api/block/getBlockIndex", model.CheckAuth, getBlockIndex)
 	ginServer.Handle("POST", "/api/block/getRefIDs", model.CheckAuth, getRefIDs)

+ 0 - 1
kernel/model/bazzar.go

@@ -57,7 +57,6 @@ func InstalledPlugins() (plugins []*bazaar.Plugin) {
 		petal := getPetalByName(plugin.Name, petals)
 		if nil != petal {
 			plugin.Enabled = petal.Enabled
-			break
 		}
 	}
 	return

+ 52 - 0
kernel/model/block.go

@@ -445,6 +445,58 @@ func GetBlockKramdown(id string) (ret string) {
 	return
 }
 
+type ChildBlock struct {
+	ID      string `json:"id"`
+	Type    string `json:"type"`
+	SubType string `json:"subType,omitempty"`
+}
+
+func GetChildBlocks(id string) (ret []*ChildBlock) {
+	ret = []*ChildBlock{}
+	if "" == id {
+		return
+	}
+
+	tree, err := loadTreeByBlockID(id)
+	if nil != err {
+		return
+	}
+
+	node := treenode.GetNodeInTree(tree, id)
+	if nil == node {
+		return
+	}
+
+	if ast.NodeHeading == node.Type {
+		children := treenode.HeadingChildren(node)
+		for _, c := range children {
+			ret = append(ret, &ChildBlock{
+				ID:      c.ID,
+				Type:    treenode.TypeAbbr(c.Type.String()),
+				SubType: treenode.SubTypeAbbr(c),
+			})
+		}
+		return
+	}
+
+	if !node.IsContainerBlock() {
+		return
+	}
+
+	for c := node.FirstChild; nil != c; c = c.Next {
+		if !c.IsBlock() {
+			continue
+		}
+
+		ret = append(ret, &ChildBlock{
+			ID:      c.ID,
+			Type:    treenode.TypeAbbr(c.Type.String()),
+			SubType: treenode.SubTypeAbbr(c),
+		})
+	}
+	return
+}
+
 func GetBlock(id string, tree *parse.Tree) (ret *Block, err error) {
 	ret, err = getBlock(id, tree)
 	return