diff --git a/kernel/api/block.go b/kernel/api/block.go index 8fb6288c6..160223a4c 100644 --- a/kernel/api/block.go +++ b/kernel/api/block.go @@ -31,6 +31,20 @@ import ( "github.com/siyuan-note/siyuan/kernel/util" ) +func getHeadingChildrenDOM(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) + dom := model.GetHeadingChildrenDOM(id) + ret.Data = dom +} + func getHeadingLevelTransaction(c *gin.Context) { ret := gulu.Ret.NewResult() defer c.JSON(http.StatusOK, ret) diff --git a/kernel/api/router.go b/kernel/api/router.go index 08f3ce0f1..ddebc65ac 100644 --- a/kernel/api/router.go +++ b/kernel/api/router.go @@ -148,6 +148,7 @@ func ServeAPI(ginServer *gin.Engine) { ginServer.Handle("POST", "/api/block/deleteBlock", model.CheckAuth, deleteBlock) ginServer.Handle("POST", "/api/block/setBlockReminder", model.CheckAuth, setBlockReminder) ginServer.Handle("POST", "/api/block/getHeadingLevelTransaction", model.CheckAuth, getHeadingLevelTransaction) + ginServer.Handle("POST", "/api/block/getHeadingChildrenDOM", model.CheckAuth, getHeadingChildrenDOM) ginServer.Handle("POST", "/api/file/getFile", model.CheckAuth, getFile) ginServer.Handle("POST", "/api/file/putFile", model.CheckAuth, putFile) diff --git a/kernel/model/block.go b/kernel/model/block.go index 1d8b8f7fc..90108d10b 100644 --- a/kernel/model/block.go +++ b/kernel/model/block.go @@ -17,6 +17,9 @@ package model import ( + "errors" + "fmt" + "github.com/88250/lute" "github.com/88250/lute/ast" "github.com/siyuan-note/siyuan/kernel/sql" @@ -85,6 +88,80 @@ func RecentUpdatedBlocks() (ret []*Block) { return } +func GetHeadingChildrenDOM(id string) (ret string) { + tree, err := loadTreeByBlockID(id) + if nil != err { + return + } + heading := treenode.GetNodeInTree(tree, id) + if nil == heading || ast.NodeHeading != heading.Type { + return + } + + nodes := append([]*ast.Node{}, heading) + children := treenode.HeadingChildren(heading) + nodes = append(nodes, children...) + luteEngine := NewLute() + ret = renderBlockDOMByNodes(nodes, luteEngine) + return +} + +func GetHeadingLevelTransaction(id string, level int) (transaction *Transaction, err error) { + tree, err := loadTreeByBlockID(id) + if nil != err { + return + } + + node := treenode.GetNodeInTree(tree, id) + if nil == node { + err = errors.New(fmt.Sprintf(Conf.Language(15), id)) + return + } + + if ast.NodeHeading != node.Type { + return + } + + hLevel := node.HeadingLevel + if hLevel == level { + return + } + + diff := level - hLevel + var children, childrenHeadings []*ast.Node + children = append(children, node) + children = append(children, treenode.HeadingChildren(node)...) + for _, c := range children { + if ast.NodeHeading == c.Type { + childrenHeadings = append(childrenHeadings, c) + } + } + + transaction = &Transaction{} + luteEngine := NewLute() + for _, c := range childrenHeadings { + op := &Operation{} + op.ID = c.ID + op.Action = "update" + op.Data = lute.RenderNodeBlockDOM(c, luteEngine.ParseOptions, luteEngine.RenderOptions) + transaction.UndoOperations = append(transaction.UndoOperations, op) + + c.HeadingLevel += diff + if 6 < c.HeadingLevel { + c.HeadingLevel = 6 + } else if 1 > c.HeadingLevel { + c.HeadingLevel = 1 + } + + op = &Operation{} + op.ID = c.ID + op.Action = "update" + op.Data = lute.RenderNodeBlockDOM(c, luteEngine.ParseOptions, luteEngine.RenderOptions) + transaction.DoOperations = append(transaction.DoOperations, op) + } + return +} + func GetBlockDOM(id string) (ret string) { if "" == id { return diff --git a/kernel/model/blockial.go b/kernel/model/blockial.go index af7a51ee4..be98e505a 100644 --- a/kernel/model/blockial.go +++ b/kernel/model/blockial.go @@ -22,7 +22,6 @@ import ( "time" "github.com/88250/gulu" - "github.com/88250/lute" "github.com/88250/lute/ast" "github.com/88250/lute/html" "github.com/88250/lute/lex" @@ -33,62 +32,6 @@ import ( "github.com/siyuan-note/siyuan/kernel/util" ) -func GetHeadingLevelTransaction(id string, level int) (transaction *Transaction, err error) { - tree, err := loadTreeByBlockID(id) - if nil != err { - return - } - - node := treenode.GetNodeInTree(tree, id) - if nil == node { - err = errors.New(fmt.Sprintf(Conf.Language(15), id)) - return - } - - if ast.NodeHeading != node.Type { - return - } - - hLevel := node.HeadingLevel - if hLevel == level { - return - } - - diff := level - hLevel - var children, childrenHeadings []*ast.Node - children = append(children, node) - children = append(children, treenode.HeadingChildren(node)...) - for _, c := range children { - if ast.NodeHeading == c.Type { - childrenHeadings = append(childrenHeadings, c) - } - } - - transaction = &Transaction{} - luteEngine := NewLute() - for _, c := range childrenHeadings { - op := &Operation{} - op.ID = c.ID - op.Action = "update" - op.Data = lute.RenderNodeBlockDOM(c, luteEngine.ParseOptions, luteEngine.RenderOptions) - transaction.UndoOperations = append(transaction.UndoOperations, op) - - c.HeadingLevel += diff - if 6 < c.HeadingLevel { - c.HeadingLevel = 6 - } else if 1 > c.HeadingLevel { - c.HeadingLevel = 1 - } - - op = &Operation{} - op.ID = c.ID - op.Action = "update" - op.Data = lute.RenderNodeBlockDOM(c, luteEngine.ParseOptions, luteEngine.RenderOptions) - transaction.DoOperations = append(transaction.DoOperations, op) - } - return -} - func SetBlockReminder(id string, timed string) (err error) { if !IsSubscriber() { if "ios" == util.Container {