瀏覽代碼

:art: Update av

Daniel 2 年之前
父節點
當前提交
ee583e3cec
共有 4 個文件被更改,包括 74 次插入33 次删除
  1. 23 16
      kernel/av/cell.go
  2. 1 0
      kernel/av/column.go
  3. 48 17
      kernel/model/attribute_view.go
  4. 2 0
      kernel/model/transaction.go

+ 23 - 16
kernel/av/cell.go

@@ -16,32 +16,43 @@
 
 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"`
 }
 
-func NewCellBlock(blockID, blockContent string) *Cell {
-	return &Cell{
-		ID:          ast.NewNodeID(),
-		Value:       blockID,
-		ValueType:   ColumnTypeBlock,
-		RenderValue: &RenderValueBlock{ID: blockID, Content: blockContent},
+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 NewCellText(text string) *Cell {
+func NewCellBlock(blockID, blockContent string) *Cell {
 	return &Cell{
 		ID:          ast.NewNodeID(),
-		Value:       text,
-		ValueType:   ColumnTypeText,
-		RenderValue: &RenderValueText{Content: text},
+		Value:       &Value{Block: blockID},
+		ValueType:   ColumnTypeBlock,
+		RenderValue: &RenderValueBlock{ID: blockID, Content: blockContent},
 	}
 }
 
@@ -56,7 +67,3 @@ type RenderValueBlock struct {
 	ID      string `json:"id"`
 	Content string `json:"content"`
 }
-
-type RenderValueText struct {
-	Content string `json:"content"`
-}

+ 1 - 0
kernel/av/column.go

@@ -27,6 +27,7 @@ const (
 	ColumnTypeRelation ColumnType = "relation"
 	ColumnTypeRollup   ColumnType = "rollup"
 	ColumnTypeSelect   ColumnType = "select"
+	ColumnTypeMSelect  ColumnType = "mSelect"
 	ColumnTypeText     ColumnType = "text"
 )
 

+ 48 - 17
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-"

+ 2 - 0
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":