mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-26 19:38:48 +01:00
🎨 Database grouping by field https://github.com/siyuan-note/siyuan/issues/10964
This commit is contained in:
parent
2b753ac16f
commit
0341a1cb03
13 changed files with 2183 additions and 1939 deletions
|
|
@ -18,6 +18,7 @@ package av
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
|
@ -37,6 +38,122 @@ const (
|
|||
SortOrderDesc SortOrder = "DESC"
|
||||
)
|
||||
|
||||
func Sort(viewable Viewable, attrView *AttributeView) {
|
||||
collection := viewable.(Collection)
|
||||
sorts := collection.GetSorts()
|
||||
if 1 > len(sorts) {
|
||||
return
|
||||
}
|
||||
|
||||
type FieldIndexSort struct {
|
||||
Index int
|
||||
Order SortOrder
|
||||
}
|
||||
|
||||
var fieldIndexSorts []*FieldIndexSort
|
||||
for _, s := range sorts {
|
||||
for i, c := range collection.GetFields() {
|
||||
if c.GetID() == s.Column {
|
||||
fieldIndexSorts = append(fieldIndexSorts, &FieldIndexSort{Index: i, Order: s.Order})
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
items := collection.GetItems()
|
||||
editedValItems := map[string]bool{}
|
||||
for i, item := range items {
|
||||
for _, fieldIndexSort := range fieldIndexSorts {
|
||||
val := items[i].GetValues()[fieldIndexSort.Index]
|
||||
if KeyTypeCheckbox == val.Type {
|
||||
if block := item.GetBlockValue(); nil != block && block.IsEdited() {
|
||||
// 如果主键编辑过,则勾选框也算作编辑过,参与排序 https://github.com/siyuan-note/siyuan/issues/11016
|
||||
editedValItems[item.GetID()] = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if val.IsEdited() {
|
||||
// 如果该卡片某字段的值已经编辑过,则该卡片可参与排序
|
||||
editedValItems[item.GetID()] = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 将未编辑的卡片和已编辑的卡片分开排序
|
||||
var uneditedItems, editedItems []Item
|
||||
for _, item := range items {
|
||||
if _, ok := editedValItems[item.GetID()]; ok {
|
||||
editedItems = append(editedItems, item)
|
||||
} else {
|
||||
uneditedItems = append(uneditedItems, item)
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(uneditedItems, func(i, j int) bool {
|
||||
val1 := uneditedItems[i].GetBlockValue()
|
||||
if nil == val1 {
|
||||
return true
|
||||
}
|
||||
val2 := uneditedItems[j].GetBlockValue()
|
||||
if nil == val2 {
|
||||
return false
|
||||
}
|
||||
return val1.CreatedAt < val2.CreatedAt
|
||||
})
|
||||
|
||||
sort.Slice(editedItems, func(i, j int) bool {
|
||||
sorted := true
|
||||
for _, fieldIndexSort := range fieldIndexSorts {
|
||||
val1 := editedItems[i].GetValues()[fieldIndexSort.Index]
|
||||
val2 := editedItems[j].GetValues()[fieldIndexSort.Index]
|
||||
if nil == val1 || val1.IsEmpty() {
|
||||
if nil != val2 && !val2.IsEmpty() {
|
||||
return false
|
||||
}
|
||||
sorted = false
|
||||
continue
|
||||
} else {
|
||||
if nil == val2 || val2.IsEmpty() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
result := val1.Compare(val2, attrView)
|
||||
if 0 == result {
|
||||
sorted = false
|
||||
continue
|
||||
}
|
||||
sorted = true
|
||||
|
||||
if fieldIndexSort.Order == SortOrderAsc {
|
||||
return 0 > result
|
||||
}
|
||||
return 0 < result
|
||||
}
|
||||
|
||||
if !sorted {
|
||||
key1 := editedItems[i].GetBlockValue()
|
||||
if nil == key1 {
|
||||
return false
|
||||
}
|
||||
key2 := editedItems[j].GetBlockValue()
|
||||
if nil == key2 {
|
||||
return false
|
||||
}
|
||||
return key1.CreatedAt < key2.CreatedAt
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
// 将包含未编辑的卡片放在最后
|
||||
collection.SetItems(append(editedItems, uneditedItems...))
|
||||
if 1 > len(collection.GetItems()) {
|
||||
collection.SetItems([]Item{})
|
||||
}
|
||||
}
|
||||
|
||||
func (value *Value) Compare(other *Value, attrView *AttributeView) int {
|
||||
switch value.Type {
|
||||
case KeyTypeBlock:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue