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/go.mod b/kernel/go.mod index 2ed91ff82..de1217bb3 100644 --- a/kernel/go.mod +++ b/kernel/go.mod @@ -32,7 +32,7 @@ require ( github.com/goccy/go-json v0.10.2 github.com/gofrs/flock v0.8.1 github.com/gorilla/websocket v1.5.0 - github.com/imroc/req/v3 v3.37.1 + github.com/imroc/req/v3 v3.37.2 github.com/jinzhu/copier v0.3.5 github.com/json-iterator/go v1.1.12 github.com/mattn/go-sqlite3 v2.0.3+incompatible @@ -45,7 +45,7 @@ require ( github.com/radovskyb/watcher v1.0.7 github.com/sashabaranov/go-openai v1.12.0 github.com/shirou/gopsutil/v3 v3.23.5 - github.com/siyuan-note/dejavu v0.0.0-20230625034511-bd5deae72411 + github.com/siyuan-note/dejavu v0.0.0-20230630155628-d2a5008347d2 github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75 github.com/siyuan-note/eventbus v0.0.0-20230216103454-41885eac6c2b github.com/siyuan-note/filelock v0.0.0-20230615140405-d05a21d49524 @@ -70,7 +70,7 @@ require ( github.com/andybalholm/brotli v1.0.5 // indirect github.com/andybalholm/cascadia v1.3.2 // indirect github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef // indirect - github.com/aws/aws-sdk-go v1.44.289 // indirect + github.com/aws/aws-sdk-go v1.44.293 // indirect github.com/bytedance/sonic v1.9.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect @@ -132,7 +132,7 @@ require ( github.com/yusufpapurcu/wmi v1.2.3 // indirect golang.org/x/arch v0.3.0 // indirect golang.org/x/crypto v0.10.0 // indirect - golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect + golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect golang.org/x/net v0.11.0 // indirect golang.org/x/sync v0.3.0 // indirect golang.org/x/sys v0.9.0 // indirect diff --git a/kernel/go.sum b/kernel/go.sum index c2609454c..b908e8dc1 100644 --- a/kernel/go.sum +++ b/kernel/go.sum @@ -40,8 +40,8 @@ github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de h1:FxWPpzIjnTlhP github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de/go.mod h1:DCaWoUhZrYW9p1lxo/cm8EmUOOzAPSEZNGF2DK1dJgw= github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef h1:2JGTg6JapxP9/R33ZaagQtAM4EkkSYnIAlOG5EI8gkM= github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef/go.mod h1:JS7hed4L1fj0hXcyEejnW57/7LCetXggd+vwrRnYeII= -github.com/aws/aws-sdk-go v1.44.289 h1:5CVEjiHFvdiVlKPBzv0rjG4zH/21W/onT18R5AH/qx0= -github.com/aws/aws-sdk-go v1.44.289/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI= +github.com/aws/aws-sdk-go v1.44.293 h1:oBPrQqsyMYe61Sl/xKVvQFflXjPwYH11aKi8QR3Nhts= +github.com/aws/aws-sdk-go v1.44.293/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s= github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= @@ -168,8 +168,8 @@ github.com/huandu/xstrings v1.4.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4= github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY= -github.com/imroc/req/v3 v3.37.1 h1:HUs5/jazZWTlTGMs3PCV15vqQq/ha9fY1NV+RYACrxI= -github.com/imroc/req/v3 v3.37.1/go.mod h1:DECzjVIrj6jcUr5n6e+z0ygmCO93rx4Jy0RjOEe1YCI= +github.com/imroc/req/v3 v3.37.2 h1:vEemuA0cq9zJ6lhe+mSRhsZm951bT0CdiSH47+KTn6I= +github.com/imroc/req/v3 v3.37.2/go.mod h1:DECzjVIrj6jcUr5n6e+z0ygmCO93rx4Jy0RjOEe1YCI= github.com/jinzhu/copier v0.3.5 h1:GlvfUwHk62RokgqVNvYsku0TATCF7bAHVwEXoBh3iJg= github.com/jinzhu/copier v0.3.5/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg= github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= @@ -287,8 +287,8 @@ github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5g github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/shurcooL/gofontwoff v0.0.0-20181114050219-180f79e6909d h1:lvCTyBbr36+tqMccdGMwuEU+hjux/zL6xSmf5S9ITaA= github.com/shurcooL/gofontwoff v0.0.0-20181114050219-180f79e6909d/go.mod h1:05UtEgK5zq39gLST6uB0cf3NEHjETfB4Fgr3Gx5R9Vw= -github.com/siyuan-note/dejavu v0.0.0-20230625034511-bd5deae72411 h1:tLHMfMVnqLzHD0rkkxoDVSoPomOrW5G7uF3obg1x5Tc= -github.com/siyuan-note/dejavu v0.0.0-20230625034511-bd5deae72411/go.mod h1:iR6kmFYS3jz/yuDFg6atb0yZdDs2RvS5gf3NzTL4frI= +github.com/siyuan-note/dejavu v0.0.0-20230630155628-d2a5008347d2 h1:bG4a3HIUia37oFn6D/I/nWUIWQ92+941X+qKQZoxHyA= +github.com/siyuan-note/dejavu v0.0.0-20230630155628-d2a5008347d2/go.mod h1:iR6kmFYS3jz/yuDFg6atb0yZdDs2RvS5gf3NzTL4frI= github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75 h1:Bi7/7f29LW+Fm0cHc0J1NO1cZqyJwljSWVmfOqVZgaE= github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75/go.mod h1:H8fyqqAbp9XreANjeSbc72zEdFfKTXYN34tc1TjZwtw= github.com/siyuan-note/eventbus v0.0.0-20230216103454-41885eac6c2b h1:828lTUW2C0uNiolODqoACu7J8sDUzswD4Xo04mUombg= @@ -361,8 +361,8 @@ golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= golang.org/x/crypto v0.10.0 h1:LKqV2xt9+kDzSTfOhx4FrkEBcMrAgHSYgzywV9zcGmM= golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I= -golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc= -golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= +golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME= +golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM= golang.org/x/image v0.8.0 h1:agUcRXV/+w6L9ryntYYsF2x9fQTMd4T8fiiYXAVW6Jg= golang.org/x/image v0.8.0/go.mod h1:PwLxp3opCYg4WR2WO9P0L6ESnsD6bLTWcw8zanLMVFM= 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":