From 7fe80bc1dc244d2f9d60619794a4ede45bd19450 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sun, 25 Sep 2022 19:04:25 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E5=B8=A6=E5=AD=90=E7=BA=A7=E5=8D=87?= =?UTF-8?q?=E9=99=8D=E6=A0=87=E9=A2=98=E5=B1=82=E7=BA=A7=20https://github.?= =?UTF-8?q?com/siyuan-note/siyuan/issues/2860?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/api/block.go | 23 +++++++++++++++++ kernel/model/blockial.go | 56 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/kernel/api/block.go b/kernel/api/block.go index 4db72e98c..8fb6288c6 100644 --- a/kernel/api/block.go +++ b/kernel/api/block.go @@ -31,6 +31,29 @@ import ( "github.com/siyuan-note/siyuan/kernel/util" ) +func getHeadingLevelTransaction(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) + level := int(arg["level"].(float64)) + + transaction, err := model.GetHeadingLevelTransaction(id, level) + if nil != err { + ret.Code = -1 + ret.Msg = err.Error() + ret.Data = map[string]interface{}{"closeTimeout": 7000} + return + } + + ret.Data = transaction +} + func setBlockReminder(c *gin.Context) { ret := gulu.Ret.NewResult() defer c.JSON(http.StatusOK, ret) diff --git a/kernel/model/blockial.go b/kernel/model/blockial.go index be98e505a..34cbd7aff 100644 --- a/kernel/model/blockial.go +++ b/kernel/model/blockial.go @@ -22,6 +22,7 @@ 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" @@ -32,6 +33,61 @@ 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 + children := treenode.HeadingChildren(node) + var childrenHeadings []*ast.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 {