mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 23:20:13 +01:00
🎨 插入块接口加入 nextID 参数 https://github.com/siyuan-note/siyuan/issues/6199
This commit is contained in:
parent
d15c7c5e4c
commit
2a4f533574
3 changed files with 33 additions and 5 deletions
|
|
@ -123,13 +123,16 @@ func insertBlock(c *gin.Context) {
|
||||||
|
|
||||||
data := arg["data"].(string)
|
data := arg["data"].(string)
|
||||||
dataType := arg["dataType"].(string)
|
dataType := arg["dataType"].(string)
|
||||||
var parentID, previousID string
|
var parentID, previousID, nextID string
|
||||||
if nil != arg["parentID"] {
|
if nil != arg["parentID"] {
|
||||||
parentID = arg["parentID"].(string)
|
parentID = arg["parentID"].(string)
|
||||||
}
|
}
|
||||||
if nil != arg["previousID"] {
|
if nil != arg["previousID"] {
|
||||||
previousID = arg["previousID"].(string)
|
previousID = arg["previousID"].(string)
|
||||||
}
|
}
|
||||||
|
if nil != arg["nextID"] {
|
||||||
|
nextID = arg["nextID"].(string)
|
||||||
|
}
|
||||||
|
|
||||||
if "markdown" == dataType {
|
if "markdown" == dataType {
|
||||||
luteEngine := model.NewLute()
|
luteEngine := model.NewLute()
|
||||||
|
|
@ -144,6 +147,7 @@ func insertBlock(c *gin.Context) {
|
||||||
Data: data,
|
Data: data,
|
||||||
ParentID: parentID,
|
ParentID: parentID,
|
||||||
PreviousID: previousID,
|
PreviousID: previousID,
|
||||||
|
NextID: nextID,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -708,11 +708,15 @@ func (tx *Transaction) doInsert(operation *Operation) (ret *TxErr) {
|
||||||
if nil == block {
|
if nil == block {
|
||||||
block = treenode.GetBlockTree(operation.PreviousID)
|
block = treenode.GetBlockTree(operation.PreviousID)
|
||||||
if nil == block {
|
if nil == block {
|
||||||
msg := fmt.Sprintf("not found previous block [id=%s]", operation.PreviousID)
|
block = treenode.GetBlockTree(operation.NextID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if nil == block {
|
||||||
|
msg := fmt.Sprintf("not found next block [id=%s]", operation.NextID)
|
||||||
logging.LogErrorf(msg)
|
logging.LogErrorf(msg)
|
||||||
return &TxErr{code: TxErrCodeBlockNotFound, id: operation.PreviousID}
|
return &TxErr{code: TxErrCodeBlockNotFound, id: operation.NextID}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tree, err := tx.loadTree(block.ID)
|
tree, err := tx.loadTree(block.ID)
|
||||||
if errors.Is(err, filelock.ErrUnableAccessFile) {
|
if errors.Is(err, filelock.ErrUnableAccessFile) {
|
||||||
return &TxErr{code: TxErrCodeUnableAccessFile, msg: err.Error(), id: block.ID}
|
return &TxErr{code: TxErrCodeUnableAccessFile, msg: err.Error(), id: block.ID}
|
||||||
|
|
@ -780,8 +784,20 @@ func (tx *Transaction) doInsert(operation *Operation) (ret *TxErr) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var node *ast.Node
|
var node *ast.Node
|
||||||
|
nextID := operation.NextID
|
||||||
previousID := operation.PreviousID
|
previousID := operation.PreviousID
|
||||||
if "" != previousID {
|
if "" != nextID {
|
||||||
|
node = treenode.GetNodeInTree(tree, nextID)
|
||||||
|
if nil == node {
|
||||||
|
logging.LogErrorf("get node [%s] in tree [%s] failed", nextID, tree.Root.ID)
|
||||||
|
return &TxErr{code: TxErrCodeBlockNotFound, id: nextID}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ast.NodeList == insertedNode.Type && nil != node.Parent && ast.NodeList == node.Parent.Type {
|
||||||
|
insertedNode = insertedNode.FirstChild
|
||||||
|
}
|
||||||
|
node.InsertBefore(insertedNode)
|
||||||
|
} else if "" != previousID {
|
||||||
node = treenode.GetNodeInTree(tree, previousID)
|
node = treenode.GetNodeInTree(tree, previousID)
|
||||||
if nil == node {
|
if nil == node {
|
||||||
logging.LogErrorf("get node [%s] in tree [%s] failed", previousID, tree.Root.ID)
|
logging.LogErrorf("get node [%s] in tree [%s] failed", previousID, tree.Root.ID)
|
||||||
|
|
@ -973,6 +989,7 @@ type Operation struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
ParentID string `json:"parentID"`
|
ParentID string `json:"parentID"`
|
||||||
PreviousID string `json:"previousID"`
|
PreviousID string `json:"previousID"`
|
||||||
|
NextID string `json:"nextID"`
|
||||||
RetData interface{} `json:"retData"`
|
RetData interface{} `json:"retData"`
|
||||||
|
|
||||||
discard bool // 用于标识是否在事务合并中丢弃
|
discard bool // 用于标识是否在事务合并中丢弃
|
||||||
|
|
|
||||||
|
|
@ -195,11 +195,18 @@ func CountBlockNodes(node *ast.Node) (ret int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParentNodes(node *ast.Node) (parents []*ast.Node) {
|
func ParentNodes(node *ast.Node) (parents []*ast.Node) {
|
||||||
|
const maxDepth = 256
|
||||||
|
i := 0
|
||||||
for n := node.Parent; nil != n; n = n.Parent {
|
for n := node.Parent; nil != n; n = n.Parent {
|
||||||
|
i++
|
||||||
parents = append(parents, n)
|
parents = append(parents, n)
|
||||||
if ast.NodeDocument == n.Type {
|
if ast.NodeDocument == n.Type {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if maxDepth < i {
|
||||||
|
logging.LogWarnf("parent nodes of node [%s] is too deep", node.ID)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue