ソースを参照

:art: Add internal kernel API `/api/attr/batchSetBlockAttrs` https://github.com/siyuan-note/siyuan/issues/10337

Daniel 1 年間 前
コミット
11993cab32
3 ファイル変更86 行追加0 行削除
  1. 42 0
      kernel/api/attr.go
  2. 1 0
      kernel/api/router.go
  3. 43 0
      kernel/model/blockial.go

+ 42 - 0
kernel/api/attr.go

@@ -88,6 +88,48 @@ func setBlockAttrs(c *gin.Context) {
 	}
 	}
 }
 }
 
 
+func batchSetBlockAttrs(c *gin.Context) {
+	ret := gulu.Ret.NewResult()
+	defer c.JSON(http.StatusOK, ret)
+
+	arg, ok := util.JsonArg(c, ret)
+	if !ok {
+		return
+	}
+
+	blockAttrsArg := arg["blockAttrs"].([]interface{})
+	var blockAttrs []map[string]interface{}
+	for _, blockAttrArg := range blockAttrsArg {
+		blockAttr := blockAttrArg.(map[string]interface{})
+		id := blockAttr["id"].(string)
+		if util.InvalidIDPattern(id, ret) {
+			return
+		}
+
+		attrs := blockAttr["attrs"].(map[string]interface{})
+		nameValues := map[string]string{}
+		for name, value := range attrs {
+			if nil == value {
+				nameValues[name] = ""
+			} else {
+				nameValues[name] = value.(string)
+			}
+		}
+
+		blockAttrs = append(blockAttrs, map[string]interface{}{
+			"id":    id,
+			"attrs": nameValues,
+		})
+	}
+
+	err := model.BatchSetBlockAttrs(blockAttrs)
+	if nil != err {
+		ret.Code = -1
+		ret.Msg = err.Error()
+		return
+	}
+}
+
 func resetBlockAttrs(c *gin.Context) {
 func resetBlockAttrs(c *gin.Context) {
 	ret := gulu.Ret.NewResult()
 	ret := gulu.Ret.NewResult()
 	defer c.JSON(http.StatusOK, ret)
 	defer c.JSON(http.StatusOK, ret)

+ 1 - 0
kernel/api/router.go

@@ -211,6 +211,7 @@ func ServeAPI(ginServer *gin.Engine) {
 	ginServer.Handle("POST", "/api/attr/getBookmarkLabels", model.CheckAuth, getBookmarkLabels)
 	ginServer.Handle("POST", "/api/attr/getBookmarkLabels", model.CheckAuth, getBookmarkLabels)
 	ginServer.Handle("POST", "/api/attr/resetBlockAttrs", model.CheckAuth, model.CheckReadonly, resetBlockAttrs)
 	ginServer.Handle("POST", "/api/attr/resetBlockAttrs", model.CheckAuth, model.CheckReadonly, resetBlockAttrs)
 	ginServer.Handle("POST", "/api/attr/setBlockAttrs", model.CheckAuth, model.CheckReadonly, setBlockAttrs)
 	ginServer.Handle("POST", "/api/attr/setBlockAttrs", model.CheckAuth, model.CheckReadonly, setBlockAttrs)
+	ginServer.Handle("POST", "/api/attr/batchSetBlockAttrs", model.CheckAuth, model.CheckReadonly, batchSetBlockAttrs)
 	ginServer.Handle("POST", "/api/attr/getBlockAttrs", model.CheckAuth, getBlockAttrs)
 	ginServer.Handle("POST", "/api/attr/getBlockAttrs", model.CheckAuth, getBlockAttrs)
 
 
 	ginServer.Handle("POST", "/api/cloud/getCloudSpace", model.CheckAuth, getCloudSpace)
 	ginServer.Handle("POST", "/api/cloud/getCloudSpace", model.CheckAuth, getCloudSpace)

+ 43 - 0
kernel/model/blockial.go

@@ -95,6 +95,49 @@ func SetBlockReminder(id string, timed string) (err error) {
 	return
 	return
 }
 }
 
 
+func BatchSetBlockAttrs(blockAttrs []map[string]interface{}) (err error) {
+	if util.ReadOnly {
+		return
+	}
+
+	WaitForWritingFiles()
+	trees := map[string]*parse.Tree{}
+	for _, blockAttr := range blockAttrs {
+		id := blockAttr["id"].(string)
+		if nil == trees[id] {
+			tree, e := loadTreeByBlockID(id)
+			if nil != e {
+				return e
+			}
+			trees[id] = tree
+		}
+	}
+
+	var nodes []*ast.Node
+	for _, blockAttr := range blockAttrs {
+		id := blockAttr["id"].(string)
+		attrs := blockAttr["attrs"].(map[string]string)
+		tree := trees[id]
+		node := treenode.GetNodeInTree(tree, id)
+		if nil == node {
+			return errors.New(fmt.Sprintf(Conf.Language(15), id))
+		}
+
+		oldAttrs, e := setNodeAttrs0(node, attrs)
+		if nil != e {
+			return e
+		}
+
+		cache.PutBlockIAL(node.ID, parse.IAL2Map(node.KramdownIAL))
+		pushBroadcastAttrTransactions(oldAttrs, node)
+		nodes = append(nodes, node)
+	}
+
+	IncSync()
+	// 不做锚文本刷新
+	return
+}
+
 func SetBlockAttrs(id string, nameValues map[string]string) (err error) {
 func SetBlockAttrs(id string, nameValues map[string]string) (err error) {
 	if util.ReadOnly {
 	if util.ReadOnly {
 		return
 		return