From f15361bdd76702398051186fd969e81db6b6ac8a Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 9 Sep 2022 11:03:37 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E8=B0=83=E7=94=A8=20API=20`setBlockAtt?= =?UTF-8?q?rs`=20=E5=90=8E=E6=8E=A8=E9=80=81=20Transaction=20Fix=20https:/?= =?UTF-8?q?/github.com/siyuan-note/siyuan/issues/5847=20=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E5=B1=9E=E6=80=A7=E6=9B=B4=E6=94=B9=E4=B8=8E=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=BC=82=E5=B8=B8=20Fix=20https://github.com/siyuan-n?= =?UTF-8?q?ote/siyuan/issues/5841?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/blockial.go | 22 ++++++++++++++++++++++ kernel/model/transaction.go | 11 ----------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/kernel/model/blockial.go b/kernel/model/blockial.go index 17570221c..42e98bc81 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" @@ -105,6 +106,9 @@ func SetBlockAttrs(id string, nameValues map[string]string) (err error) { return errors.New(fmt.Sprintf(Conf.Language(15), id)) } + luteEngine := NewLute() + oldDom := lute.RenderNodeBlockDOM(node, luteEngine.ParseOptions, luteEngine.RenderOptions) + for name, _ := range nameValues { for i := 0; i < len(name); i++ { if !lex.IsASCIILetterNumHyphen(name[i]) { @@ -126,9 +130,27 @@ func SetBlockAttrs(id string, nameValues map[string]string) (err error) { } IncSync() cache.PutBlockIAL(id, parse.IAL2Map(node.KramdownIAL)) + + dom := lute.RenderNodeBlockDOM(node, luteEngine.ParseOptions, luteEngine.RenderOptions) + if oldDom == dom { + return + } + doOp := &Operation{Action: "update", Data: dom, ID: id} + undoOp := &Operation{Action: "update", Data: oldDom, ID: id} + trans := []*Transaction{{ + DoOperations: []*Operation{doOp}, + UndoOperations: []*Operation{undoOp}, + }} + pushBroadcastTransactions(trans) return } +func pushBroadcastTransactions(transactions []*Transaction) { + evt := util.NewCmdResult("transactions", 0, util.PushModeBroadcast, util.PushModeBroadcast) + evt.Data = transactions + util.PushEvent(evt) +} + func ResetBlockAttrs(id string, nameValues map[string]string) (err error) { tree, err := loadTreeByBlockID(id) if nil != err { diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go index 8bc11b866..54e182a8d 100644 --- a/kernel/model/transaction.go +++ b/kernel/model/transaction.go @@ -904,17 +904,6 @@ func (tx *Transaction) doUpdate(operation *Operation) (ret *TxErr) { treenode.MoveFoldHeading(updatedNode, oldNode) } - // 挂件移动或设置大小后属性丢失 https://github.com/siyuan-note/siyuan/issues/4885 - // 这里需要把旧节点的属性复制到新节点上,避免属性丢失 - oldIAL := parse.IAL2Map(oldNode.KramdownIAL) - newIAL := parse.IAL2Map(updatedNode.KramdownIAL) - for oldIALKey, oldIALVal := range oldIAL { - if strings.HasPrefix(oldIALKey, "custom-") { - newIAL[oldIALKey] = oldIALVal - } - } - updatedNode.KramdownIAL = parse.Map2IAL(newIAL) - cache.PutBlockIAL(updatedNode.ID, parse.IAL2Map(updatedNode.KramdownIAL)) // 替换为新节点