mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-24 02:20:13 +01:00
🎨 Update av
This commit is contained in:
parent
30bfa6e5d8
commit
ee583e3cec
4 changed files with 74 additions and 33 deletions
|
|
@ -16,35 +16,46 @@
|
||||||
|
|
||||||
package av
|
package av
|
||||||
|
|
||||||
import "github.com/88250/lute/ast"
|
import (
|
||||||
|
"github.com/88250/gulu"
|
||||||
|
"github.com/88250/lute/ast"
|
||||||
|
)
|
||||||
|
|
||||||
type Cell struct {
|
type Cell struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Value string `json:"value"`
|
Value *Value `json:"value"`
|
||||||
ValueType ColumnType `json:"valueType"`
|
ValueType ColumnType `json:"valueType"`
|
||||||
RenderValue interface{} `json:"renderValue"`
|
RenderValue interface{} `json:"renderValue"`
|
||||||
Color string `json:"color"`
|
Color string `json:"color"`
|
||||||
BgColor string `json:"bgColor"`
|
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 {
|
func NewCellBlock(blockID, blockContent string) *Cell {
|
||||||
return &Cell{
|
return &Cell{
|
||||||
ID: ast.NewNodeID(),
|
ID: ast.NewNodeID(),
|
||||||
Value: blockID,
|
Value: &Value{Block: blockID},
|
||||||
ValueType: ColumnTypeBlock,
|
ValueType: ColumnTypeBlock,
|
||||||
RenderValue: &RenderValueBlock{ID: blockID, Content: blockContent},
|
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 {
|
func NewCell(valueType ColumnType) *Cell {
|
||||||
return &Cell{
|
return &Cell{
|
||||||
ID: ast.NewNodeID(),
|
ID: ast.NewNodeID(),
|
||||||
|
|
@ -56,7 +67,3 @@ type RenderValueBlock struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type RenderValueText struct {
|
|
||||||
Content string `json:"content"`
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ const (
|
||||||
ColumnTypeRelation ColumnType = "relation"
|
ColumnTypeRelation ColumnType = "relation"
|
||||||
ColumnTypeRollup ColumnType = "rollup"
|
ColumnTypeRollup ColumnType = "rollup"
|
||||||
ColumnTypeSelect ColumnType = "select"
|
ColumnTypeSelect ColumnType = "select"
|
||||||
|
ColumnTypeMSelect ColumnType = "mSelect"
|
||||||
ColumnTypeText ColumnType = "text"
|
ColumnTypeText ColumnType = "text"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -89,7 +89,7 @@ func (tx *Transaction) doUpdateAttrViewCell(operation *Operation) (ret *TxErr) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
blockID = row.Cells[0].Value
|
blockID = row.Cells[0].Value.Block
|
||||||
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
|
||||||
|
|
@ -113,9 +113,17 @@ func (tx *Transaction) doUpdateAttrViewCell(operation *Operation) (ret *TxErr) {
|
||||||
return
|
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 := 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 {
|
if err = setNodeAttrsWithTx(tx, node, tree, attrs); nil != err {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -197,6 +205,14 @@ func (tx *Transaction) doAddAttrViewColumn(operation *Operation) (ret *TxErr) {
|
||||||
return
|
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) {
|
func (tx *Transaction) doRemoveAttrViewColumn(operation *Operation) (ret *TxErr) {
|
||||||
err := removeAttributeViewColumn(operation.ID, operation.ParentID)
|
err := removeAttributeViewColumn(operation.ID, operation.ParentID)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
|
|
@ -230,6 +246,33 @@ func addAttributeViewColumn(name string, typ string, avID string) (err error) {
|
||||||
return
|
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) {
|
func removeAttributeViewColumn(columnID string, avID string) (err error) {
|
||||||
attrView, err := av.ParseAttributeView(avID)
|
attrView, err := av.ParseAttributeView(avID)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
|
|
@ -267,7 +310,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 == blockID {
|
if row.Cells[0].Value.Block == blockID {
|
||||||
// 从行中移除,但是不移除属性
|
// 从行中移除,但是不移除属性
|
||||||
ret.Rows = append(ret.Rows[:i], ret.Rows[i+1:]...)
|
ret.Rows = append(ret.Rows[:i], ret.Rows[i+1:]...)
|
||||||
break
|
break
|
||||||
|
|
@ -303,7 +346,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 == blockID {
|
if row.Cells[0].Value.Block == blockID {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -346,18 +389,6 @@ func addAttributeViewBlock(blockID, previousRowID, avID string, tree *parse.Tree
|
||||||
return
|
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 (
|
const (
|
||||||
NodeAttrNameAVs = "avs"
|
NodeAttrNameAVs = "avs"
|
||||||
NodeAttrNamePrefixAvCol = "av-col-"
|
NodeAttrNamePrefixAvCol = "av-col-"
|
||||||
|
|
|
||||||
|
|
@ -222,6 +222,8 @@ func performTx(tx *Transaction) (ret *TxErr) {
|
||||||
ret = tx.doRemoveAttrViewBlock(op)
|
ret = tx.doRemoveAttrViewBlock(op)
|
||||||
case "addAttrViewCol":
|
case "addAttrViewCol":
|
||||||
ret = tx.doAddAttrViewColumn(op)
|
ret = tx.doAddAttrViewColumn(op)
|
||||||
|
case "updateAttrViewCol":
|
||||||
|
ret = tx.doUpdateAttrViewColumn(op)
|
||||||
case "removeAttrViewCol":
|
case "removeAttrViewCol":
|
||||||
ret = tx.doRemoveAttrViewColumn(op)
|
ret = tx.doRemoveAttrViewColumn(op)
|
||||||
case "updateAttrViewCell":
|
case "updateAttrViewCell":
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue