diff --git a/kernel/av/cell.go b/kernel/av/cell.go index c63b4dc8c..e97859722 100644 --- a/kernel/av/cell.go +++ b/kernel/av/cell.go @@ -16,35 +16,46 @@ package av -import "github.com/88250/lute/ast" +import ( + "github.com/88250/gulu" + "github.com/88250/lute/ast" +) type Cell struct { ID string `json:"id"` - Value string `json:"value"` + Value *Value `json:"value"` ValueType ColumnType `json:"valueType"` RenderValue interface{} `json:"renderValue"` Color string `json:"color"` BgColor string `json:"bgColor"` } +type Value struct { + Block string `json:"block"` + Text string `json:"text"` + Number float64 `json:"number"` + Date string `json:"date"` + Select string `json:"select"` + MSelect []string `json:"mSelect"` +} + +func (value *Value) ToJSONString() string { + data, err := gulu.JSON.MarshalJSON(value) + if nil != err { + return "" + } + return string(data) +} + func NewCellBlock(blockID, blockContent string) *Cell { return &Cell{ ID: ast.NewNodeID(), - Value: blockID, + Value: &Value{Block: blockID}, ValueType: ColumnTypeBlock, RenderValue: &RenderValueBlock{ID: blockID, Content: blockContent}, } } -func NewCellText(text string) *Cell { - return &Cell{ - ID: ast.NewNodeID(), - Value: text, - ValueType: ColumnTypeText, - RenderValue: &RenderValueText{Content: text}, - } -} - func NewCell(valueType ColumnType) *Cell { return &Cell{ ID: ast.NewNodeID(), @@ -56,7 +67,3 @@ type RenderValueBlock struct { ID string `json:"id"` Content string `json:"content"` } - -type RenderValueText struct { - Content string `json:"content"` -} diff --git a/kernel/av/column.go b/kernel/av/column.go index f95c59f3f..3d49a2c9d 100644 --- a/kernel/av/column.go +++ b/kernel/av/column.go @@ -27,6 +27,7 @@ const ( ColumnTypeRelation ColumnType = "relation" ColumnTypeRollup ColumnType = "rollup" ColumnTypeSelect ColumnType = "select" + ColumnTypeMSelect ColumnType = "mSelect" ColumnTypeText ColumnType = "text" ) diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index c0ca0a622..ccb773bb3 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -89,7 +89,7 @@ func (tx *Transaction) doUpdateAttrViewCell(operation *Operation) (ret *TxErr) { continue } - blockID = row.Cells[0].Value + blockID = row.Cells[0].Value.Block for _, cell := range row.Cells[1:] { if cell.ID == operation.ID { c = cell @@ -113,9 +113,17 @@ func (tx *Transaction) doUpdateAttrViewCell(operation *Operation) (ret *TxErr) { return } - c.Value, c.RenderValue = parseCellData(operation) + data, err := gulu.JSON.MarshalJSON(operation.Data) + if nil != err { + return + } + if err = gulu.JSON.UnmarshalJSON(data, &c.Value); nil != err { + return + } + + c.RenderValue = operation.Data attrs := parse.IAL2Map(node.KramdownIAL) - attrs[NodeAttrNamePrefixAvCol+avID+"-"+c.ID] = c.Value + attrs[NodeAttrNamePrefixAvCol+avID+"-"+c.ID] = c.Value.ToJSONString() if err = setNodeAttrsWithTx(tx, node, tree, attrs); nil != err { return } @@ -197,6 +205,14 @@ func (tx *Transaction) doAddAttrViewColumn(operation *Operation) (ret *TxErr) { return } +func (tx *Transaction) doUpdateAttrViewColumn(operation *Operation) (ret *TxErr) { + err := updateAttributeViewColumn(operation.ID, operation.Name, operation.Typ, operation.ParentID) + if nil != err { + return &TxErr{code: TxErrWriteAttributeView, id: operation.ParentID, msg: err.Error()} + } + return +} + func (tx *Transaction) doRemoveAttrViewColumn(operation *Operation) (ret *TxErr) { err := removeAttributeViewColumn(operation.ID, operation.ParentID) if nil != err { @@ -230,6 +246,33 @@ func addAttributeViewColumn(name string, typ string, avID string) (err error) { return } +func updateAttributeViewColumn(id, name string, typ string, avID string) (err error) { + attrView, err := av.ParseAttributeView(avID) + if nil != err { + return + } + + colType := av.ColumnType(typ) + switch colType { + case av.ColumnTypeText: + for _, col := range attrView.Columns { + if col.ID == id { + col.Name = name + col.Type = colType + break + } + } + default: + msg := fmt.Sprintf("invalid column type [%s]", typ) + logging.LogErrorf(msg) + err = errors.New(msg) + return + } + + err = av.SaveAttributeView(attrView) + return +} + func removeAttributeViewColumn(columnID string, avID string) (err error) { attrView, err := av.ParseAttributeView(avID) if nil != err { @@ -267,7 +310,7 @@ func removeAttributeViewBlock(blockID, avID string, tree *parse.Tree) (ret *av.A } for i, row := range ret.Rows { - if row.Cells[0].Value == blockID { + if row.Cells[0].Value.Block == blockID { // 从行中移除,但是不移除属性 ret.Rows = append(ret.Rows[:i], ret.Rows[i+1:]...) break @@ -303,7 +346,7 @@ func addAttributeViewBlock(blockID, previousRowID, avID string, tree *parse.Tree // 不允许重复添加相同的块到属性视图中 for _, row := range ret.Rows { - if row.Cells[0].Value == blockID { + if row.Cells[0].Value.Block == blockID { return } } @@ -346,18 +389,6 @@ func addAttributeViewBlock(blockID, previousRowID, avID string, tree *parse.Tree return } -func parseCellData(operation *Operation) (val, renderVal string) { - data := operation.Data - colType := av.ColumnType(operation.Typ) - switch colType { - case av.ColumnTypeText: - val = data.(string) - renderVal = val - return - } - return -} - const ( NodeAttrNameAVs = "avs" NodeAttrNamePrefixAvCol = "av-col-" diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go index 6414e6b92..d88b0c20e 100644 --- a/kernel/model/transaction.go +++ b/kernel/model/transaction.go @@ -222,6 +222,8 @@ func performTx(tx *Transaction) (ret *TxErr) { ret = tx.doRemoveAttrViewBlock(op) case "addAttrViewCol": ret = tx.doAddAttrViewColumn(op) + case "updateAttrViewCol": + ret = tx.doUpdateAttrViewColumn(op) case "removeAttrViewCol": ret = tx.doRemoveAttrViewColumn(op) case "updateAttrViewCell":