From 3a7e0cd014b208c59884eb4be6161bdb230313bf Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sat, 15 Jul 2023 21:36:35 +0800 Subject: [PATCH] :art: Attribute View columns calculate https://github.com/siyuan-note/siyuan/issues/8699 --- kernel/model/attribute_view.go | 44 ++++++++++++++++++++++++++++++++++ kernel/model/transaction.go | 2 ++ 2 files changed, 46 insertions(+) diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index 1a6fe83ab..1027c6650 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -258,6 +258,50 @@ func setAttributeViewSorts(operation *Operation) (err error) { return } +func (tx *Transaction) doSetAttrViewColCalc(operation *Operation) (ret *TxErr) { + err := setAttributeViewColumnCalc(operation) + if nil != err { + return &TxErr{code: TxErrWriteAttributeView, id: operation.AvID, msg: err.Error()} + } + return +} + +func setAttributeViewColumnCalc(operation *Operation) (err error) { + attrView, err := av.ParseAttributeView(operation.AvID) + if nil != err { + return + } + + view, err := attrView.GetView() + if nil != err { + return + } + + operationData := operation.Data.([]interface{}) + data, err := gulu.JSON.MarshalJSON(operationData) + if nil != err { + return + } + + calc := &av.ColumnCalc{} + switch view.LayoutType { + case av.LayoutTypeTable: + if err = gulu.JSON.UnmarshalJSON(data, calc); nil != err { + return + } + + for _, column := range view.Table.Columns { + if column.ID == operation.ID { + column.Calc = calc + break + } + } + } + + err = av.SaveAttributeView(attrView) + return +} + func (tx *Transaction) doInsertAttrViewBlock(operation *Operation) (ret *TxErr) { firstSrcID := operation.SrcIDs[0] tree, err := tx.loadTree(firstSrcID) diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go index aa4637284..bca88c1ee 100644 --- a/kernel/model/transaction.go +++ b/kernel/model/transaction.go @@ -251,6 +251,8 @@ func performTx(tx *Transaction) (ret *TxErr) { ret = tx.doRemoveAttrViewColOption(op) case "updateAttrViewColOption": ret = tx.doUpdateAttrViewColOption(op) + case "setAttrViewColCalc": + ret = tx.doSetAttrViewColCalc(op) } if nil != ret {