mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 15:10: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)
|
||||
}
|
||||
|
||||
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) {
|
||||
ret := gulu.Ret.NewResult()
|
||||
defer c.JSON(http.StatusOK, ret)
|
||||
|
|
@ -472,6 +520,54 @@ func prependBlock(c *gin.Context) {
|
|||
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) {
|
||||
ret := gulu.Ret.NewResult()
|
||||
defer c.JSON(http.StatusOK, ret)
|
||||
|
|
@ -628,6 +724,72 @@ func updateBlock(c *gin.Context) {
|
|||
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) {
|
||||
ret := gulu.Ret.NewResult()
|
||||
defer c.JSON(http.StatusOK, ret)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue