mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 23:20:13 +01:00
🎨 Database table view cell value adds createdAt and updatedAt fields https://github.com/siyuan-note/siyuan/issues/10492
Adding rows after setting the sort field in the database table view no longer fills in the default value https://github.com/siyuan-note/siyuan/issues/10486
This commit is contained in:
parent
49031aaca7
commit
01744ae69e
4 changed files with 71 additions and 72 deletions
|
|
@ -269,7 +269,7 @@ func SaveAttributeView(av *AttributeView) (err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if 0 == v.Block.Updated {
|
if 0 == v.Block.Updated {
|
||||||
v.Block.Updated = now
|
v.Block.Updated = v.Block.Created
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case KeyTypeNumber:
|
case KeyTypeNumber:
|
||||||
|
|
@ -302,6 +302,19 @@ func SaveAttributeView(av *AttributeView) (err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 补全值的创建时间和更新时间
|
||||||
|
createdStr := v.ID[:len("20060102150405")]
|
||||||
|
created, parseErr := time.ParseInLocation("20060102150405", createdStr, time.Local)
|
||||||
|
if nil == parseErr {
|
||||||
|
v.CreatedAt = created.UnixMilli()
|
||||||
|
} else {
|
||||||
|
v.CreatedAt = now
|
||||||
|
}
|
||||||
|
|
||||||
|
if 0 == v.UpdatedAt {
|
||||||
|
v.UpdatedAt = v.CreatedAt
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -84,12 +84,20 @@ const (
|
||||||
|
|
||||||
func (value *Value) Compare(other *Value) int {
|
func (value *Value) Compare(other *Value) int {
|
||||||
if nil == value {
|
if nil == value {
|
||||||
return -1
|
return 1
|
||||||
}
|
}
|
||||||
if nil == other {
|
if nil == other {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
if !value.IsEdited() {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !other.IsEdited() {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
switch value.Type {
|
switch value.Type {
|
||||||
case KeyTypeBlock:
|
case KeyTypeBlock:
|
||||||
if nil != value.Block && nil != other.Block {
|
if nil != value.Block && nil != other.Block {
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,9 @@ type Value struct {
|
||||||
Type KeyType `json:"type,omitempty"`
|
Type KeyType `json:"type,omitempty"`
|
||||||
IsDetached bool `json:"isDetached,omitempty"`
|
IsDetached bool `json:"isDetached,omitempty"`
|
||||||
|
|
||||||
|
CreatedAt int64 `json:"createdAt,omitempty"`
|
||||||
|
UpdatedAt int64 `json:"updatedAt,omitempty"`
|
||||||
|
|
||||||
Block *ValueBlock `json:"block,omitempty"`
|
Block *ValueBlock `json:"block,omitempty"`
|
||||||
Text *ValueText `json:"text,omitempty"`
|
Text *ValueText `json:"text,omitempty"`
|
||||||
Number *ValueNumber `json:"number,omitempty"`
|
Number *ValueNumber `json:"number,omitempty"`
|
||||||
|
|
@ -180,6 +183,14 @@ func (value *Value) Clone() (ret *Value) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (value *Value) IsEdited() bool {
|
||||||
|
if value.CreatedAt == value.UpdatedAt {
|
||||||
|
// 说明是刚刚创建的块
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
type ValueBlock struct {
|
type ValueBlock struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
|
|
|
||||||
|
|
@ -605,10 +605,25 @@ func renderAttributeView(attrView *av.AttributeView, viewID string, page, pageSi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if 0 == v.Block.Updated {
|
if 0 == v.Block.Updated {
|
||||||
v.Block.Updated = currentTimeMillis
|
v.Block.Updated = v.Block.Created
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 补全值的创建时间和更新时间
|
||||||
|
for _, v := range kv.Values {
|
||||||
|
createdStr := v.ID[:len("20060102150405")]
|
||||||
|
created, parseErr := time.ParseInLocation("20060102150405", createdStr, time.Local)
|
||||||
|
if nil == parseErr {
|
||||||
|
v.CreatedAt = created.UnixMilli()
|
||||||
|
} else {
|
||||||
|
v.CreatedAt = currentTimeMillis
|
||||||
|
}
|
||||||
|
|
||||||
|
if 0 == v.UpdatedAt {
|
||||||
|
v.UpdatedAt = v.CreatedAt
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch view.LayoutType {
|
switch view.LayoutType {
|
||||||
|
|
@ -1698,68 +1713,24 @@ func addAttributeViewBlock(avID, previousBlockID, blockID string, isDetached boo
|
||||||
content = getNodeRefText(node)
|
content = getNodeRefText(node)
|
||||||
}
|
}
|
||||||
now := time.Now().UnixMilli()
|
now := time.Now().UnixMilli()
|
||||||
blockValue := &av.Value{ID: ast.NewNodeID(), KeyID: blockValues.Key.ID, BlockID: blockID, Type: av.KeyTypeBlock, IsDetached: isDetached, Block: &av.ValueBlock{ID: blockID, Content: content, Created: now, Updated: now}}
|
blockValue := &av.Value{
|
||||||
|
ID: ast.NewNodeID(),
|
||||||
|
KeyID: blockValues.Key.ID,
|
||||||
|
BlockID: blockID,
|
||||||
|
Type: av.KeyTypeBlock,
|
||||||
|
IsDetached: isDetached,
|
||||||
|
CreatedAt: now,
|
||||||
|
UpdatedAt: now,
|
||||||
|
Block: &av.ValueBlock{ID: blockID, Content: content, Created: now, Updated: now}}
|
||||||
blockValues.Values = append(blockValues.Values, blockValue)
|
blockValues.Values = append(blockValues.Values, blockValue)
|
||||||
|
|
||||||
// 如果存在排序和过滤条件,则将排序和过滤条件应用到新添加的块上
|
// 如果存在过滤条件,则将过滤条件应用到新添加的块上
|
||||||
view, _ := attrView.GetCurrentView()
|
view, _ := attrView.GetCurrentView()
|
||||||
if nil != view && (0 < len(view.Table.Filters) || 0 < len(view.Table.Sorts)) {
|
if nil != view && (0 < len(view.Table.Filters) || 0 < len(view.Table.Sorts)) {
|
||||||
viewable, _ := renderAttributeViewTable(attrView, view)
|
viewable, _ := renderAttributeViewTable(attrView, view)
|
||||||
viewable.FilterRows(attrView)
|
viewable.FilterRows(attrView)
|
||||||
viewable.SortRows()
|
|
||||||
|
|
||||||
affectKeyIDs := map[string]bool{}
|
|
||||||
for _, f := range view.Table.Filters {
|
|
||||||
affectKeyIDs[f.Column] = true
|
|
||||||
}
|
|
||||||
for _, s := range view.Table.Sorts {
|
|
||||||
affectKeyIDs[s.Column] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
addedValues := map[string]bool{}
|
|
||||||
if 0 < len(viewable.Rows) {
|
|
||||||
row := GetLastSortRow(viewable.Rows)
|
|
||||||
if nil != row {
|
|
||||||
for affectKeyID := range affectKeyIDs {
|
|
||||||
for _, cell := range row.Cells {
|
|
||||||
if nil != cell.Value && cell.Value.KeyID == affectKeyID {
|
|
||||||
if av.KeyTypeBlock == cell.ValueType {
|
|
||||||
blockValue.Block.Content = cell.Value.Block.Content
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if av.KeyTypeRollup == cell.ValueType || av.KeyTypeRelation == cell.ValueType || av.KeyTypeCreated == cell.ValueType || av.KeyTypeUpdated == cell.ValueType || av.KeyTypeTemplate == cell.ValueType {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
newValue := cell.Value.Clone()
|
|
||||||
newValue.ID = ast.NewNodeID()
|
|
||||||
newValue.BlockID = blockID
|
|
||||||
newValue.IsDetached = isDetached
|
|
||||||
values, _ := attrView.GetKeyValues(affectKeyID)
|
|
||||||
values.Values = append(values.Values, newValue)
|
|
||||||
addedValues[affectKeyID] = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
notAddedValues := map[string]bool{}
|
|
||||||
for affectKeyID := range affectKeyIDs {
|
|
||||||
if !addedValues[affectKeyID] {
|
|
||||||
notAddedValues[affectKeyID] = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if 0 < len(notAddedValues) {
|
|
||||||
for _, filter := range view.Table.Filters {
|
for _, filter := range view.Table.Filters {
|
||||||
if !notAddedValues[filter.Column] {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, keyValues := range attrView.KeyValues {
|
for _, keyValues := range attrView.KeyValues {
|
||||||
if keyValues.Key.ID == filter.Column {
|
if keyValues.Key.ID == filter.Column {
|
||||||
newValue := filter.GetAffectValue(keyValues.Key)
|
newValue := filter.GetAffectValue(keyValues.Key)
|
||||||
|
|
@ -1772,9 +1743,6 @@ func addAttributeViewBlock(avID, previousBlockID, blockID string, isDetached boo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 仅使用上面的过滤条件计算受影响的值并插入兜底,受影响的排序条件不进行计算值插入
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !isDetached {
|
if !isDetached {
|
||||||
|
|
@ -1830,8 +1798,7 @@ func GetLastSortRow(rows []*av.TableRow) *av.TableRow {
|
||||||
row := rows[i]
|
row := rows[i]
|
||||||
blockVal := row.GetBlockValue()
|
blockVal := row.GetBlockValue()
|
||||||
if nil != blockVal {
|
if nil != blockVal {
|
||||||
if nil != blockVal.Block && blockVal.Block.Created == blockVal.Block.Updated {
|
if !blockVal.IsEdited() {
|
||||||
// 说明是刚刚创建的块,跳过
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
return row
|
return row
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue