From 06a5a59cae0d59fee92020fc5c99cf9b6a8d92d7 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sun, 3 Mar 2024 22:00:42 +0800 Subject: [PATCH] :art: 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 --- kernel/av/av.go | 2 -- kernel/av/table.go | 69 +++++++++++++++++++++++++---------- kernel/av/value.go | 89 ++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 137 insertions(+), 23 deletions(-) diff --git a/kernel/av/av.go b/kernel/av/av.go index 1e9c663c1..0f3c7577e 100644 --- a/kernel/av/av.go +++ b/kernel/av/av.go @@ -321,8 +321,6 @@ func SaveAttributeView(av *AttributeView) (err error) { } } - // 数据订正 - // 值去重 blockValues := av.GetBlockKeyValues() blockIDs := map[string]bool{} diff --git a/kernel/av/table.go b/kernel/av/table.go index 71cdf2182..deba919c3 100644 --- a/kernel/av/table.go +++ b/kernel/av/table.go @@ -83,22 +83,6 @@ const ( ) func (value *Value) Compare(other *Value) int { - if nil == value { - return 1 - } - if nil == other { - return -1 - } - - if !value.IsEdited() { - if other.IsEdited() { - return 1 - } - return int(value.CreatedAt - other.CreatedAt) - } else if !other.IsEdited() { - return -1 - } - switch value.Type { case KeyTypeBlock: if nil != value.Block && nil != other.Block { @@ -273,7 +257,7 @@ func (value *Value) Compare(other *Value) int { } func (value *Value) CompareOperator(filter *ViewFilter, attrView *AttributeView, rowID string) bool { - if nil != value.Rollup && nil != filter.Value.Rollup { + if nil != value.Rollup && KeyTypeRollup == filter.Value.Type { rollupKey, _ := attrView.GetKey(value.KeyID) if nil == rollupKey { return false @@ -904,9 +888,53 @@ func (table *Table) SortRows() { } } - sort.Slice(table.Rows, func(i, j int) bool { + includeUneditedRows := map[string]bool{} + for i, row := range table.Rows { for _, colIndexSort := range colIndexSorts { - result := table.Rows[i].Cells[colIndexSort.Index].Value.Compare(table.Rows[j].Cells[colIndexSort.Index].Value) + val := table.Rows[i].Cells[colIndexSort.Index].Value + if !val.IsEdited() { + // 如果该行的某个列的值是未编辑的,则该行不参与排序 + includeUneditedRows[row.ID] = true + break + } + } + } + + // 将包含未编辑的行和全部已编辑的行分开排序 + var uneditedRows, editedRows []*TableRow + for _, row := range table.Rows { + if _, ok := includeUneditedRows[row.ID]; ok { + uneditedRows = append(uneditedRows, row) + } else { + editedRows = append(editedRows, row) + } + } + + sort.Slice(uneditedRows, func(i, j int) bool { + val1 := uneditedRows[i].GetBlockValue() + if nil == val1 { + return true + } + val2 := uneditedRows[j].GetBlockValue() + if nil == val2 { + return false + } + return val1.CreatedAt < val2.CreatedAt + }) + + sort.Slice(editedRows, func(i, j int) bool { + for _, colIndexSort := range colIndexSorts { + val1 := editedRows[i].Cells[colIndexSort.Index].Value + if nil == val1 { + return colIndexSort.Order == SortOrderAsc + } + + val2 := editedRows[j].Cells[colIndexSort.Index].Value + if nil == val2 { + return colIndexSort.Order != SortOrderAsc + } + + result := val1.Compare(val2) if 0 == result { continue } @@ -918,6 +946,9 @@ func (table *Table) SortRows() { } return false }) + + // 将包含未编辑的行放在最后 + table.Rows = append(editedRows, uneditedRows...) } func (table *Table) FilterRows(attrView *AttributeView) { diff --git a/kernel/av/value.go b/kernel/av/value.go index 958c65a0f..57aaa8e67 100644 --- a/kernel/av/value.go +++ b/kernel/av/value.go @@ -189,10 +189,95 @@ func (value *Value) IsEdited() bool { return true } - if value.CreatedAt == value.UpdatedAt { + if value.IsGenerated() { + // 所有生成的数据都认为是编辑过的 + return true + } + + if value.IsEmpty() { + // 空数据认为是未编辑过的 return false } - return true + return value.CreatedAt != value.UpdatedAt +} + +func (value *Value) IsGenerated() bool { + return KeyTypeTemplate == value.Type || KeyTypeRollup == value.Type || KeyTypeUpdated == value.Type || KeyTypeCreated == value.Type +} + +func (value *Value) IsEmpty() bool { + switch value.Type { + case KeyTypeBlock: + if nil == value.Block { + return true + } + return "" == value.Block.Content + case KeyTypeText: + if nil == value.Text { + return true + } + return "" == value.Text.Content + case KeyTypeNumber: + if nil == value.Number { + return true + } + return !value.Number.IsNotEmpty + case KeyTypeDate: + if nil == value.Date { + return true + } + return !value.Date.IsNotEmpty + case KeyTypeSelect: + if 1 > len(value.MSelect) { + return true + } + return "" == value.MSelect[0].Content + case KeyTypeMSelect: + return 1 > len(value.MSelect) + case KeyTypeURL: + if nil == value.URL { + return true + } + return "" == value.URL.Content + case KeyTypeEmail: + if nil == value.Email { + return true + } + return "" == value.Email.Content + case KeyTypePhone: + if nil == value.Phone { + return true + } + return "" == value.Phone.Content + case KeyTypeMAsset: + return 1 > len(value.MAsset) + case KeyTypeTemplate: + if nil == value.Template { + return true + } + return "" == value.Template.Content + case KeyTypeCreated: + if nil == value.Created { + return true + } + return !value.Created.IsNotEmpty + case KeyTypeUpdated: + if nil == value.Updated { + return true + } + return !value.Updated.IsNotEmpty + case KeyTypeCheckbox: + if nil == value.Checkbox { + return true + } + return !value.Checkbox.Checked + case KeyTypeRelation: + return 1 > len(value.Relation.Contents) + case KeyTypeRollup: + return 1 > len(value.Rollup.Contents) + } + + return false } type ValueBlock struct {