mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-16 14:40:12 +01:00
🧑💻 Add some kernel batch insert blocks APIs https://github.com/siyuan-note/siyuan/issues/15321
This commit is contained in:
parent
b1a04d67d9
commit
b2d003aeff
2 changed files with 165 additions and 0 deletions
|
|
@ -427,6 +427,54 @@ func appendBlock(c *gin.Context) {
|
||||||
broadcastTransactions(transactions)
|
broadcastTransactions(transactions)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func batchAppendBlock(c *gin.Context) {
|
||||||
|
ret := gulu.Ret.NewResult()
|
||||||
|
defer c.JSON(http.StatusOK, ret)
|
||||||
|
|
||||||
|
arg, ok := util.JsonArg(c, ret)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
blocksArg := arg["blocks"].([]interface{})
|
||||||
|
var transactions []*model.Transaction
|
||||||
|
luteEngine := util.NewLute()
|
||||||
|
for _, blockArg := range blocksArg {
|
||||||
|
blockMap := blockArg.(map[string]interface{})
|
||||||
|
data := blockMap["data"].(string)
|
||||||
|
dataType := blockMap["dataType"].(string)
|
||||||
|
parentID := blockMap["parentID"].(string)
|
||||||
|
if util.InvalidIDPattern(parentID, ret) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if "markdown" == dataType {
|
||||||
|
var err error
|
||||||
|
data, err = dataBlockDOM(data, luteEngine)
|
||||||
|
if err != nil {
|
||||||
|
ret.Code = -1
|
||||||
|
ret.Msg = "data block DOM failed: " + err.Error()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
transactions = append(transactions, &model.Transaction{
|
||||||
|
DoOperations: []*model.Operation{
|
||||||
|
{
|
||||||
|
Action: "appendInsert",
|
||||||
|
Data: data,
|
||||||
|
ParentID: parentID,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
model.PerformTransactions(&transactions)
|
||||||
|
model.FlushTxQueue()
|
||||||
|
|
||||||
|
ret.Data = transactions
|
||||||
|
broadcastTransactions(transactions)
|
||||||
|
}
|
||||||
|
|
||||||
func prependBlock(c *gin.Context) {
|
func prependBlock(c *gin.Context) {
|
||||||
ret := gulu.Ret.NewResult()
|
ret := gulu.Ret.NewResult()
|
||||||
defer c.JSON(http.StatusOK, ret)
|
defer c.JSON(http.StatusOK, ret)
|
||||||
|
|
@ -472,6 +520,54 @@ func prependBlock(c *gin.Context) {
|
||||||
broadcastTransactions(transactions)
|
broadcastTransactions(transactions)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func batchPrependBlock(c *gin.Context) {
|
||||||
|
ret := gulu.Ret.NewResult()
|
||||||
|
defer c.JSON(http.StatusOK, ret)
|
||||||
|
|
||||||
|
arg, ok := util.JsonArg(c, ret)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
blocksArg := arg["blocks"].([]interface{})
|
||||||
|
var transactions []*model.Transaction
|
||||||
|
luteEngine := util.NewLute()
|
||||||
|
for _, blockArg := range blocksArg {
|
||||||
|
blockMap := blockArg.(map[string]interface{})
|
||||||
|
data := blockMap["data"].(string)
|
||||||
|
dataType := blockMap["dataType"].(string)
|
||||||
|
parentID := blockMap["parentID"].(string)
|
||||||
|
if util.InvalidIDPattern(parentID, ret) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if "markdown" == dataType {
|
||||||
|
var err error
|
||||||
|
data, err = dataBlockDOM(data, luteEngine)
|
||||||
|
if err != nil {
|
||||||
|
ret.Code = -1
|
||||||
|
ret.Msg = "data block DOM failed: " + err.Error()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
transactions = append(transactions, &model.Transaction{
|
||||||
|
DoOperations: []*model.Operation{
|
||||||
|
{
|
||||||
|
Action: "prependInsert",
|
||||||
|
Data: data,
|
||||||
|
ParentID: parentID,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
model.PerformTransactions(&transactions)
|
||||||
|
model.FlushTxQueue()
|
||||||
|
|
||||||
|
ret.Data = transactions
|
||||||
|
broadcastTransactions(transactions)
|
||||||
|
}
|
||||||
|
|
||||||
func insertBlock(c *gin.Context) {
|
func insertBlock(c *gin.Context) {
|
||||||
ret := gulu.Ret.NewResult()
|
ret := gulu.Ret.NewResult()
|
||||||
defer c.JSON(http.StatusOK, ret)
|
defer c.JSON(http.StatusOK, ret)
|
||||||
|
|
@ -628,6 +724,72 @@ func updateBlock(c *gin.Context) {
|
||||||
broadcastTransactions(transactions)
|
broadcastTransactions(transactions)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func batchInsertBlock(c *gin.Context) {
|
||||||
|
ret := gulu.Ret.NewResult()
|
||||||
|
defer c.JSON(http.StatusOK, ret)
|
||||||
|
|
||||||
|
arg, ok := util.JsonArg(c, ret)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
blocksArg := arg["blocks"].([]interface{})
|
||||||
|
var transactions []*model.Transaction
|
||||||
|
luteEngine := util.NewLute()
|
||||||
|
for _, blockArg := range blocksArg {
|
||||||
|
blockMap := blockArg.(map[string]interface{})
|
||||||
|
data := blockMap["data"].(string)
|
||||||
|
dataType := blockMap["dataType"].(string)
|
||||||
|
var parentID, previousID, nextID string
|
||||||
|
if nil != blockMap["parentID"] {
|
||||||
|
parentID = blockMap["parentID"].(string)
|
||||||
|
if "" != parentID && util.InvalidIDPattern(parentID, ret) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if nil != blockMap["previousID"] {
|
||||||
|
previousID = blockMap["previousID"].(string)
|
||||||
|
if "" != previousID && util.InvalidIDPattern(previousID, ret) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if nil != blockMap["nextID"] {
|
||||||
|
nextID = blockMap["nextID"].(string)
|
||||||
|
if "" != nextID && util.InvalidIDPattern(nextID, ret) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if "markdown" == dataType {
|
||||||
|
var err error
|
||||||
|
data, err = dataBlockDOM(data, luteEngine)
|
||||||
|
if err != nil {
|
||||||
|
ret.Code = -1
|
||||||
|
ret.Msg = "data block DOM failed: " + err.Error()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
transactions = append(transactions, &model.Transaction{
|
||||||
|
DoOperations: []*model.Operation{
|
||||||
|
{
|
||||||
|
Action: "insert",
|
||||||
|
Data: data,
|
||||||
|
ParentID: parentID,
|
||||||
|
PreviousID: previousID,
|
||||||
|
NextID: nextID,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
model.PerformTransactions(&transactions)
|
||||||
|
model.FlushTxQueue()
|
||||||
|
|
||||||
|
ret.Data = transactions
|
||||||
|
broadcastTransactions(transactions)
|
||||||
|
}
|
||||||
|
|
||||||
func batchUpdateBlock(c *gin.Context) {
|
func batchUpdateBlock(c *gin.Context) {
|
||||||
ret := gulu.Ret.NewResult()
|
ret := gulu.Ret.NewResult()
|
||||||
defer c.JSON(http.StatusOK, ret)
|
defer c.JSON(http.StatusOK, ret)
|
||||||
|
|
|
||||||
|
|
@ -195,8 +195,11 @@ func ServeAPI(ginServer *gin.Engine) {
|
||||||
ginServer.Handle("POST", "/api/block/getUnfoldedParentID", model.CheckAuth, getUnfoldedParentID)
|
ginServer.Handle("POST", "/api/block/getUnfoldedParentID", model.CheckAuth, getUnfoldedParentID)
|
||||||
ginServer.Handle("POST", "/api/block/checkBlockFold", model.CheckAuth, checkBlockFold)
|
ginServer.Handle("POST", "/api/block/checkBlockFold", model.CheckAuth, checkBlockFold)
|
||||||
ginServer.Handle("POST", "/api/block/insertBlock", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, insertBlock)
|
ginServer.Handle("POST", "/api/block/insertBlock", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, insertBlock)
|
||||||
|
ginServer.Handle("POST", "/api/block/batchInsertBlock", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, batchInsertBlock)
|
||||||
ginServer.Handle("POST", "/api/block/prependBlock", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, prependBlock)
|
ginServer.Handle("POST", "/api/block/prependBlock", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, prependBlock)
|
||||||
|
ginServer.Handle("POST", "/api/block/batchPrependBlock", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, batchPrependBlock)
|
||||||
ginServer.Handle("POST", "/api/block/appendBlock", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, appendBlock)
|
ginServer.Handle("POST", "/api/block/appendBlock", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, appendBlock)
|
||||||
|
ginServer.Handle("POST", "/api/block/batchAppendBlock", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, batchAppendBlock)
|
||||||
ginServer.Handle("POST", "/api/block/appendDailyNoteBlock", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, appendDailyNoteBlock)
|
ginServer.Handle("POST", "/api/block/appendDailyNoteBlock", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, appendDailyNoteBlock)
|
||||||
ginServer.Handle("POST", "/api/block/prependDailyNoteBlock", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, prependDailyNoteBlock)
|
ginServer.Handle("POST", "/api/block/prependDailyNoteBlock", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, prependDailyNoteBlock)
|
||||||
ginServer.Handle("POST", "/api/block/updateBlock", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, updateBlock)
|
ginServer.Handle("POST", "/api/block/updateBlock", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, updateBlock)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue