🎨 Update av

This commit is contained in:
Daniel 2023-07-01 11:19:45 +08:00
parent ef362685cb
commit 18390572b5
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
3 changed files with 74 additions and 21 deletions

View file

@ -19,24 +19,24 @@ package av
import ( import (
"github.com/88250/gulu" "github.com/88250/gulu"
"github.com/88250/lute/ast" "github.com/88250/lute/ast"
"time"
) )
type Cell struct { type Cell struct {
ID string `json:"id"` ID string `json:"id"`
Value *Value `json:"value"` Value *Value `json:"value"`
ValueType ColumnType `json:"valueType"` ValueType ColumnType `json:"valueType"`
RenderValue interface{} `json:"renderValue"` Color string `json:"color"`
Color string `json:"color"` BgColor string `json:"bgColor"`
BgColor string `json:"bgColor"`
} }
type Value struct { type Value struct {
Block string `json:"block"` Block *ValueBlock `json:"block"`
Text string `json:"text"` Text *ValueText `json:"text"`
Number float64 `json:"number"` Number *ValueNumber `json:"number"`
Date string `json:"date"` Date *ValueDate `json:"date"`
Select string `json:"select"` Select *ValueSelect `json:"select"`
MSelect []string `json:"mSelect"` MSelect []*ValueSelect `json:"mSelect"`
} }
func (value *Value) ToJSONString() string { func (value *Value) ToJSONString() string {
@ -49,10 +49,9 @@ func (value *Value) ToJSONString() string {
func NewCellBlock(blockID, blockContent string) *Cell { func NewCellBlock(blockID, blockContent string) *Cell {
return &Cell{ return &Cell{
ID: ast.NewNodeID(), ID: ast.NewNodeID(),
Value: &Value{Block: blockID}, Value: &Value{Block: &ValueBlock{ID: blockID, Content: blockContent}},
ValueType: ColumnTypeBlock, ValueType: ColumnTypeBlock,
RenderValue: &RenderValueBlock{ID: blockID, Content: blockContent},
} }
} }
@ -63,7 +62,23 @@ func NewCell(valueType ColumnType) *Cell {
} }
} }
type RenderValueBlock struct { type ValueBlock struct {
ID string `json:"id"` ID string `json:"id"`
Content string `json:"content"` Content string `json:"content"`
} }
type ValueText struct {
Content string `json:"content"`
}
type ValueNumber struct {
Content float64 `json:"content"`
}
type ValueDate struct {
Content time.Time `json:"content"`
}
type ValueSelect struct {
Content string `json:"content"`
}

View file

@ -89,7 +89,7 @@ func (tx *Transaction) doUpdateAttrViewCell(operation *Operation) (ret *TxErr) {
continue continue
} }
blockID = row.Cells[0].Value.Block blockID = row.Cells[0].Value.Block.ID
for _, cell := range row.Cells[1:] { for _, cell := range row.Cells[1:] {
if cell.ID == operation.ID { if cell.ID == operation.ID {
c = cell c = cell
@ -121,7 +121,6 @@ func (tx *Transaction) doUpdateAttrViewCell(operation *Operation) (ret *TxErr) {
return return
} }
c.RenderValue = operation.Data
attrs := parse.IAL2Map(node.KramdownIAL) attrs := parse.IAL2Map(node.KramdownIAL)
attrs[NodeAttrNamePrefixAvCol+avID+"-"+c.ID] = c.Value.ToJSONString() attrs[NodeAttrNamePrefixAvCol+avID+"-"+c.ID] = c.Value.ToJSONString()
if err = setNodeAttrsWithTx(tx, node, tree, attrs); nil != err { if err = setNodeAttrsWithTx(tx, node, tree, attrs); nil != err {
@ -229,6 +228,14 @@ func (tx *Transaction) doSortAttrViewColumn(operation *Operation) (ret *TxErr) {
return return
} }
func (tx *Transaction) doSortAttrViewRow(operation *Operation) (ret *TxErr) {
err := sortAttributeViewRow(operation.ID, operation.PreviousID, operation.ParentID)
if nil != err {
return &TxErr{code: TxErrWriteAttributeView, id: operation.ParentID, msg: err.Error()}
}
return
}
func addAttributeViewColumn(name string, typ string, avID string) (err error) { func addAttributeViewColumn(name string, typ string, avID string) (err error) {
attrView, err := av.ParseAttributeView(avID) attrView, err := av.ParseAttributeView(avID)
if nil != err { if nil != err {
@ -340,6 +347,35 @@ func sortAttributeViewColumn(columnID, previousColumnID, avID string) (err error
return return
} }
func sortAttributeViewRow(rowID, previousRowID, avID string) (err error) {
attrView, err := av.ParseAttributeView(avID)
if nil != err {
return
}
var row *av.Row
var index, previousIndex int
for i, r := range attrView.Rows {
if r.ID == rowID {
row = r
index = i
break
}
if r.ID == previousRowID {
previousIndex = i
}
}
if nil == row {
return
}
attrView.Rows = append(attrView.Rows[:index], attrView.Rows[index+1:]...)
attrView.Rows = append(attrView.Rows[:previousIndex], append([]*av.Row{row}, attrView.Rows[previousIndex:]...)...)
err = av.SaveAttributeView(attrView)
return
}
func removeAttributeViewBlock(blockID, avID string, tree *parse.Tree) (ret *av.AttributeView, err error) { func removeAttributeViewBlock(blockID, avID string, tree *parse.Tree) (ret *av.AttributeView, err error) {
node := treenode.GetNodeInTree(tree, blockID) node := treenode.GetNodeInTree(tree, blockID)
if nil == node { if nil == node {
@ -353,7 +389,7 @@ func removeAttributeViewBlock(blockID, avID string, tree *parse.Tree) (ret *av.A
} }
for i, row := range ret.Rows { for i, row := range ret.Rows {
if row.Cells[0].Value.Block == blockID { if row.Cells[0].Value.Block.ID == blockID {
// 从行中移除,但是不移除属性 // 从行中移除,但是不移除属性
ret.Rows = append(ret.Rows[:i], ret.Rows[i+1:]...) ret.Rows = append(ret.Rows[:i], ret.Rows[i+1:]...)
break break
@ -389,7 +425,7 @@ func addAttributeViewBlock(blockID, previousRowID, avID string, tree *parse.Tree
// 不允许重复添加相同的块到属性视图中 // 不允许重复添加相同的块到属性视图中
for _, row := range ret.Rows { for _, row := range ret.Rows {
if row.Cells[0].Value.Block == blockID { if row.Cells[0].Value.Block.ID == blockID {
return return
} }
} }

View file

@ -230,6 +230,8 @@ func performTx(tx *Transaction) (ret *TxErr) {
ret = tx.doSortAttrViewColumn(op) ret = tx.doSortAttrViewColumn(op)
case "updateAttrViewCell": case "updateAttrViewCell":
ret = tx.doUpdateAttrViewCell(op) ret = tx.doUpdateAttrViewCell(op)
case "sortAttrViewRow":
ret = tx.doSortAttrViewRow(op)
} }
if nil != ret { if nil != ret {