From b0dc75b3c4bbbf30f596a7b0ec5e9dcdd71441ff Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sat, 10 Jun 2023 16:42:07 +0800 Subject: [PATCH 1/5] :art: Update av --- kernel/av/av.go | 10 ---------- kernel/model/attribute_view.go | 24 +++++++++++++++++++----- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/kernel/av/av.go b/kernel/av/av.go index 72970b8d0..f131890c1 100644 --- a/kernel/av/av.go +++ b/kernel/av/av.go @@ -72,16 +72,6 @@ func (av *AttributeView) GetColumnNames() (ret []string) { return } -func (av *AttributeView) InsertColumn(index int, column *Column) { - if 0 > index || len(av.Columns) == index { - av.Columns = append(av.Columns, column) - return - } - - av.Columns = append(av.Columns[:index+1], av.Columns[index:]...) - av.Columns[index] = column -} - type AttributeViewFilter struct { Column string `json:"column"` Operator FilterOperator `json:"operator"` diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index 8696a5180..2dd101e5a 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -36,6 +36,8 @@ func RenderAttributeView(avID string) (ret *av.AttributeView, err error) { logging.LogErrorf("parse attribute view [%s] failed: %s", avID, err) return } + + // TODO render value return } @@ -154,7 +156,7 @@ func (tx *Transaction) doRemoveAttrViewBlock(operation *Operation) (ret *TxErr) } func (tx *Transaction) doAddAttrViewColumn(operation *Operation) (ret *TxErr) { - err := addAttributeViewColumn(operation.Name, operation.Typ, -1, operation.ParentID) + err := addAttributeViewColumn(operation.Name, operation.Typ, operation.ParentID) if nil != err { return &TxErr{code: TxErrWriteAttributeView, id: operation.ParentID, msg: err.Error()} } @@ -169,15 +171,20 @@ func (tx *Transaction) doRemoveAttrViewColumn(operation *Operation) (ret *TxErr) return } -func addAttributeViewColumn(name string, typ string, columnIndex int, avID string) (err error) { +func addAttributeViewColumn(name string, typ string, avID string) (err error) { attrView, err := av.ParseAttributeView(avID) if nil != err { return } - switch av.ColumnType(typ) { + colType := av.ColumnType(typ) + switch colType { case av.ColumnTypeText: - attrView.InsertColumn(columnIndex, &av.Column{ID: "av" + ast.NewNodeID(), Name: name, Type: av.ColumnTypeText}) + col := &av.Column{ID: "av" + ast.NewNodeID(), Name: name, Type: colType} + attrView.Columns = append(attrView.Columns, col) + for _, row := range attrView.Rows { + row.Cells = append(row.Cells, &av.Cell{ID: ast.NewNodeID()}) + } default: msg := fmt.Sprintf("invalid column type [%s]", typ) logging.LogErrorf(msg) @@ -195,9 +202,16 @@ func removeAttributeViewColumn(columnID string, avID string) (err error) { return } - for i, column := range attrView.Columns[1:] { + for i, column := range attrView.Columns { if column.ID == columnID { attrView.Columns = append(attrView.Columns[:i], attrView.Columns[i+1:]...) + for _, row := range attrView.Rows { + if len(row.Cells) <= i { + continue + } + + row.Cells = append(row.Cells[:i], row.Cells[i+1:]...) + } break } } From 4bc46841632af72c818ba6d84866c1a33131279c Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sat, 10 Jun 2023 16:52:54 +0800 Subject: [PATCH 2/5] :art: Update av --- kernel/av/av.go | 2 ++ kernel/model/attribute_view.go | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/kernel/av/av.go b/kernel/av/av.go index f131890c1..050f09ec8 100644 --- a/kernel/av/av.go +++ b/kernel/av/av.go @@ -35,6 +35,7 @@ import ( type AttributeView struct { Spec int `json:"spec"` ID string `json:"id"` // 属性视图 ID + Name string `json:"name"` // 属性视图名称 Columns []*Column `json:"columns"` // 表格列名 Rows []*Row `json:"rows"` // 表格行记录 @@ -55,6 +56,7 @@ func NewAttributeView(id string) *AttributeView { return &AttributeView{ Spec: 0, ID: id, + Name: "Table", Columns: []*Column{{ID: ast.NewNodeID(), Name: "Block", Type: ColumnTypeBlock}}, Rows: []*Row{}, Type: AttributeViewTypeTable, diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index 2dd101e5a..a5896847c 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -82,7 +82,7 @@ func (tx *Transaction) doUpdateAttrViewCell(operation *Operation) (ret *TxErr) { c.Value = parseCellData(operation.Data, av.ColumnType(operation.Typ)) attrs := parse.IAL2Map(node.KramdownIAL) - attrs["av"+c.ID] = c.Value + attrs[NodeAttrNamePrefixAvCol+c.ID] = c.Value if err = setNodeAttrsWithTx(tx, node, tree, attrs); nil != err { return } @@ -279,8 +279,8 @@ func addAttributeViewBlock(blockID, avID string, tree *parse.Tree, tx *Transacti if 1 < len(ret.Columns) { attrs := parse.IAL2Map(node.KramdownIAL) for _, col := range ret.Columns[1:] { - attrs["av"+col.ID] = "" // 将列作为属性添加到块中 - row.Cells = append(row.Cells, &av.Cell{ID: ast.NewNodeID(), Value: ""}) + attrs[NodeAttrNamePrefixAvCol+col.ID] = "" // 将列作为属性添加到块中 + row.Cells = append(row.Cells, &av.Cell{ID: ast.NewNodeID()}) } if err = setNodeAttrsWithTx(tx, node, tree, attrs); nil != err { @@ -300,3 +300,5 @@ func parseCellData(data interface{}, colType av.ColumnType) string { } return "" } + +const NodeAttrNamePrefixAvCol = "av-col-" From 639f083d9a2e440de3b499c99146e439443772ab Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sat, 10 Jun 2023 16:56:20 +0800 Subject: [PATCH 3/5] :art: Update av --- kernel/av/cell.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/kernel/av/cell.go b/kernel/av/cell.go index c7c06c567..9a2533488 100644 --- a/kernel/av/cell.go +++ b/kernel/av/cell.go @@ -18,8 +18,13 @@ package av type Cell struct { ID string `json:"id"` - Value string `json:"value"` + Value *CellValue `json:"value"` RenderValue interface{} `json:"renderValue"` Color string `json:"color"` BgColor string `json:"bgColor"` } + +type CellValue struct { + Type ColumnType `json:"type"` + Value interface{} `json:"value"` +} From 42f9c6e65749147a82e38ca6f51e2a376c2f1bbd Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sat, 10 Jun 2023 17:07:50 +0800 Subject: [PATCH 4/5] :art: Update av --- kernel/av/cell.go | 21 ++++++++++++++------- kernel/model/attribute_view.go | 16 ++++++++-------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/kernel/av/cell.go b/kernel/av/cell.go index 9a2533488..8eac479f0 100644 --- a/kernel/av/cell.go +++ b/kernel/av/cell.go @@ -17,14 +17,21 @@ package av type Cell struct { - ID string `json:"id"` - Value *CellValue `json:"value"` - RenderValue interface{} `json:"renderValue"` - Color string `json:"color"` - BgColor string `json:"bgColor"` + ID string `json:"id"` + Value *CellValue `json:"value"` + Color string `json:"color"` + BgColor string `json:"bgColor"` } type CellValue struct { - Type ColumnType `json:"type"` - Value interface{} `json:"value"` + Type ColumnType `json:"type"` + Data interface{} `json:"data"` +} + +func (v *CellValue) String() string { + switch v.Type { + case ColumnTypeText: + return v.Data.(string) + } + return v.Data.(string) } diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index a5896847c..1737d1a71 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -56,7 +56,7 @@ func (tx *Transaction) doUpdateAttrViewCell(operation *Operation) (ret *TxErr) { continue } - blockID = row.Cells[0].Value + blockID = row.Cells[0].Value.String() for _, cell := range row.Cells[1:] { if cell.ID == operation.ID { c = cell @@ -80,9 +80,9 @@ func (tx *Transaction) doUpdateAttrViewCell(operation *Operation) (ret *TxErr) { return } - c.Value = parseCellData(operation.Data, av.ColumnType(operation.Typ)) + c.Value.Data = operation.Data attrs := parse.IAL2Map(node.KramdownIAL) - attrs[NodeAttrNamePrefixAvCol+c.ID] = c.Value + attrs[NodeAttrNamePrefixAvCol+c.ID] = c.Value.String() if err = setNodeAttrsWithTx(tx, node, tree, attrs); nil != err { return } @@ -183,7 +183,7 @@ func addAttributeViewColumn(name string, typ string, avID string) (err error) { col := &av.Column{ID: "av" + ast.NewNodeID(), Name: name, Type: colType} attrView.Columns = append(attrView.Columns, col) for _, row := range attrView.Rows { - row.Cells = append(row.Cells, &av.Cell{ID: ast.NewNodeID()}) + row.Cells = append(row.Cells, &av.Cell{ID: ast.NewNodeID(), Value: &av.CellValue{Type: colType}}) } default: msg := fmt.Sprintf("invalid column type [%s]", typ) @@ -233,7 +233,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.String() == blockID { // 从行中移除,但是不移除属性 ret.Rows = append(ret.Rows[:i], ret.Rows[i+1:]...) break @@ -269,18 +269,18 @@ func addAttributeViewBlock(blockID, avID string, tree *parse.Tree, tx *Transacti // 不允许重复添加相同的块到属性视图中 for _, row := range ret.Rows { - if row.Cells[0].Value == blockID { + if row.Cells[0].Value.String() == blockID { return } } row := av.NewRow() - row.Cells = append(row.Cells, &av.Cell{ID: ast.NewNodeID(), Value: blockID}) + row.Cells = append(row.Cells, &av.Cell{ID: ast.NewNodeID(), Value: &av.CellValue{Type: av.ColumnTypeBlock, Data: blockID}}) if 1 < len(ret.Columns) { attrs := parse.IAL2Map(node.KramdownIAL) for _, col := range ret.Columns[1:] { attrs[NodeAttrNamePrefixAvCol+col.ID] = "" // 将列作为属性添加到块中 - row.Cells = append(row.Cells, &av.Cell{ID: ast.NewNodeID()}) + row.Cells = append(row.Cells, &av.Cell{ID: ast.NewNodeID(), Value: &av.CellValue{Type: col.Type}}) } if err = setNodeAttrsWithTx(tx, node, tree, attrs); nil != err { From 461a438df0935d51b58b8c0efb7b64a3bc330801 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sat, 10 Jun 2023 17:20:51 +0800 Subject: [PATCH 5/5] :art: Update av --- kernel/av/cell.go | 22 +++++----------------- kernel/model/attribute_view.go | 16 ++++++++-------- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/kernel/av/cell.go b/kernel/av/cell.go index 8eac479f0..c7c06c567 100644 --- a/kernel/av/cell.go +++ b/kernel/av/cell.go @@ -17,21 +17,9 @@ package av type Cell struct { - ID string `json:"id"` - Value *CellValue `json:"value"` - Color string `json:"color"` - BgColor string `json:"bgColor"` -} - -type CellValue struct { - Type ColumnType `json:"type"` - Data interface{} `json:"data"` -} - -func (v *CellValue) String() string { - switch v.Type { - case ColumnTypeText: - return v.Data.(string) - } - return v.Data.(string) + ID string `json:"id"` + Value string `json:"value"` + RenderValue interface{} `json:"renderValue"` + Color string `json:"color"` + BgColor string `json:"bgColor"` } diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index 1737d1a71..a5896847c 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -56,7 +56,7 @@ func (tx *Transaction) doUpdateAttrViewCell(operation *Operation) (ret *TxErr) { continue } - blockID = row.Cells[0].Value.String() + blockID = row.Cells[0].Value for _, cell := range row.Cells[1:] { if cell.ID == operation.ID { c = cell @@ -80,9 +80,9 @@ func (tx *Transaction) doUpdateAttrViewCell(operation *Operation) (ret *TxErr) { return } - c.Value.Data = operation.Data + c.Value = parseCellData(operation.Data, av.ColumnType(operation.Typ)) attrs := parse.IAL2Map(node.KramdownIAL) - attrs[NodeAttrNamePrefixAvCol+c.ID] = c.Value.String() + attrs[NodeAttrNamePrefixAvCol+c.ID] = c.Value if err = setNodeAttrsWithTx(tx, node, tree, attrs); nil != err { return } @@ -183,7 +183,7 @@ func addAttributeViewColumn(name string, typ string, avID string) (err error) { col := &av.Column{ID: "av" + ast.NewNodeID(), Name: name, Type: colType} attrView.Columns = append(attrView.Columns, col) for _, row := range attrView.Rows { - row.Cells = append(row.Cells, &av.Cell{ID: ast.NewNodeID(), Value: &av.CellValue{Type: colType}}) + row.Cells = append(row.Cells, &av.Cell{ID: ast.NewNodeID()}) } default: msg := fmt.Sprintf("invalid column type [%s]", typ) @@ -233,7 +233,7 @@ func removeAttributeViewBlock(blockID, avID string, tree *parse.Tree) (ret *av.A } for i, row := range ret.Rows { - if row.Cells[0].Value.String() == blockID { + if row.Cells[0].Value == blockID { // 从行中移除,但是不移除属性 ret.Rows = append(ret.Rows[:i], ret.Rows[i+1:]...) break @@ -269,18 +269,18 @@ func addAttributeViewBlock(blockID, avID string, tree *parse.Tree, tx *Transacti // 不允许重复添加相同的块到属性视图中 for _, row := range ret.Rows { - if row.Cells[0].Value.String() == blockID { + if row.Cells[0].Value == blockID { return } } row := av.NewRow() - row.Cells = append(row.Cells, &av.Cell{ID: ast.NewNodeID(), Value: &av.CellValue{Type: av.ColumnTypeBlock, Data: blockID}}) + row.Cells = append(row.Cells, &av.Cell{ID: ast.NewNodeID(), Value: blockID}) if 1 < len(ret.Columns) { attrs := parse.IAL2Map(node.KramdownIAL) for _, col := range ret.Columns[1:] { attrs[NodeAttrNamePrefixAvCol+col.ID] = "" // 将列作为属性添加到块中 - row.Cells = append(row.Cells, &av.Cell{ID: ast.NewNodeID(), Value: &av.CellValue{Type: col.Type}}) + row.Cells = append(row.Cells, &av.Cell{ID: ast.NewNodeID()}) } if err = setNodeAttrsWithTx(tx, node, tree, attrs); nil != err {