Skip to content

Commit

Permalink
🎨 Add template type column to Attribute View siyuan-note#8766
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Oct 1, 2023
1 parent e8359ed commit c0424ca
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 21 deletions.
47 changes: 28 additions & 19 deletions kernel/av/av.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,17 @@ type KeyValues struct {
type KeyType string

const (
KeyTypeBlock KeyType = "block"
KeyTypeText KeyType = "text"
KeyTypeNumber KeyType = "number"
KeyTypeDate KeyType = "date"
KeyTypeSelect KeyType = "select"
KeyTypeMSelect KeyType = "mSelect"
KeyTypeURL KeyType = "url"
KeyTypeEmail KeyType = "email"
KeyTypePhone KeyType = "phone"
KeyTypeMAsset KeyType = "mAsset"
KeyTypeBlock KeyType = "block"
KeyTypeText KeyType = "text"
KeyTypeNumber KeyType = "number"
KeyTypeDate KeyType = "date"
KeyTypeSelect KeyType = "select"
KeyTypeMSelect KeyType = "mSelect"
KeyTypeURL KeyType = "url"
KeyTypeEmail KeyType = "email"
KeyTypePhone KeyType = "phone"
KeyTypeMAsset KeyType = "mAsset"
KeyTypeTemplate KeyType = "template"
)

// Key 描述了属性视图属性列的基础结构。
Expand Down Expand Up @@ -100,15 +101,16 @@ type Value struct {
Type KeyType `json:"type,omitempty"`
IsDetached bool `json:"isDetached,omitempty"`

Block *ValueBlock `json:"block,omitempty"`
Text *ValueText `json:"text,omitempty"`
Number *ValueNumber `json:"number,omitempty"`
Date *ValueDate `json:"date,omitempty"`
MSelect []*ValueSelect `json:"mSelect,omitempty"`
URL *ValueURL `json:"url,omitempty"`
Email *ValueEmail `json:"email,omitempty"`
Phone *ValuePhone `json:"phone,omitempty"`
MAsset []*ValueAsset `json:"mAsset,omitempty"`
Block *ValueBlock `json:"block,omitempty"`
Text *ValueText `json:"text,omitempty"`
Number *ValueNumber `json:"number,omitempty"`
Date *ValueDate `json:"date,omitempty"`
MSelect []*ValueSelect `json:"mSelect,omitempty"`
URL *ValueURL `json:"url,omitempty"`
Email *ValueEmail `json:"email,omitempty"`
Phone *ValuePhone `json:"phone,omitempty"`
MAsset []*ValueAsset `json:"mAsset,omitempty"`
Template *ValueTemplate `json:"template,omitempty"`
}

func (value *Value) String() string {
Expand Down Expand Up @@ -139,6 +141,8 @@ func (value *Value) String() string {
ret = append(ret, v.Content)
}
return strings.Join(ret, " ")
case KeyTypeTemplate:
return value.Template.RenderedContent
default:
return ""
}
Expand Down Expand Up @@ -345,6 +349,11 @@ type ValueAsset struct {
Content string `json:"content"`
}

type ValueTemplate struct {
Content string `json:"content"`
RenderedContent string `json:"renderedContent"`
}

// View 描述了视图的结构。
type View struct {
ID string `json:"id"` // 视图 ID
Expand Down
65 changes: 65 additions & 0 deletions kernel/av/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,71 @@ func (table *Table) CalcCols() {
table.calcColPhone(col, i)
case KeyTypeMAsset:
table.calcColMAsset(col, i)
case KeyTypeTemplate:
table.calcColTemplate(col, i)
}
}
}

func (table *Table) calcColTemplate(col *TableColumn, colIndex int) {
switch col.Calc.Operator {
case CalcOperatorCountAll:
col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(len(table.Rows)), NumberFormatNone)}
case CalcOperatorCountValues:
countValues := 0
for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.RenderedContent {
countValues++
}
}
col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countValues), NumberFormatNone)}
case CalcOperatorCountUniqueValues:
countUniqueValues := 0
uniqueValues := map[string]bool{}
for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.RenderedContent {
if !uniqueValues[row.Cells[colIndex].Value.Template.RenderedContent] {
uniqueValues[row.Cells[colIndex].Value.Template.RenderedContent] = true
countUniqueValues++
}
}
}
col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countUniqueValues), NumberFormatNone)}
case CalcOperatorCountEmpty:
countEmpty := 0
for _, row := range table.Rows {
if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Template || "" == row.Cells[colIndex].Value.Template.RenderedContent {
countEmpty++
}
}
col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty), NumberFormatNone)}
case CalcOperatorCountNotEmpty:
countNotEmpty := 0
for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.RenderedContent {
countNotEmpty++
}
}
col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty), NumberFormatNone)}
case CalcOperatorPercentEmpty:
countEmpty := 0
for _, row := range table.Rows {
if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Template || "" == row.Cells[colIndex].Value.Template.RenderedContent {
countEmpty++
}
}
if 0 < len(table.Rows) {
col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
}
case CalcOperatorPercentNotEmpty:
countNotEmpty := 0
for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.RenderedContent {
countNotEmpty++
}
}
if 0 < len(table.Rows) {
col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions kernel/model/attribute_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ func addAttributeViewColumn(operation *Operation) (err error) {

keyType := av.KeyType(operation.Typ)
switch keyType {
case av.KeyTypeText, av.KeyTypeNumber, av.KeyTypeDate, av.KeyTypeSelect, av.KeyTypeMSelect, av.KeyTypeURL, av.KeyTypeEmail, av.KeyTypePhone, av.KeyTypeMAsset:
case av.KeyTypeText, av.KeyTypeNumber, av.KeyTypeDate, av.KeyTypeSelect, av.KeyTypeMSelect, av.KeyTypeURL, av.KeyTypeEmail, av.KeyTypePhone, av.KeyTypeMAsset, av.KeyTypeTemplate:
key := av.NewKey(operation.ID, operation.Name, keyType)
attrView.KeyValues = append(attrView.KeyValues, &av.KeyValues{Key: key})

Expand Down Expand Up @@ -847,7 +847,7 @@ func updateAttributeViewColumn(operation *Operation) (err error) {

colType := av.KeyType(operation.Typ)
switch colType {
case av.KeyTypeBlock, av.KeyTypeText, av.KeyTypeNumber, av.KeyTypeDate, av.KeyTypeSelect, av.KeyTypeMSelect, av.KeyTypeURL, av.KeyTypeEmail, av.KeyTypePhone, av.KeyTypeMAsset:
case av.KeyTypeBlock, av.KeyTypeText, av.KeyTypeNumber, av.KeyTypeDate, av.KeyTypeSelect, av.KeyTypeMSelect, av.KeyTypeURL, av.KeyTypeEmail, av.KeyTypePhone, av.KeyTypeMAsset, av.KeyTypeTemplate:
for _, keyValues := range attrView.KeyValues {
if keyValues.Key.ID == operation.ID {
keyValues.Key.Name = operation.Name
Expand Down

0 comments on commit c0424ca

Please sign in to comment.