2023-07-11 19:45:27 +08:00
|
|
|
|
// SiYuan - Refactor your thinking
|
|
|
|
|
|
// Copyright (c) 2020-present, b3log.org
|
|
|
|
|
|
//
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
//
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
//
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
package av
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"sort"
|
2025-06-10 12:23:07 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/88250/lute/ast"
|
2023-07-11 19:45:27 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-07-12 10:35:17 +08:00
|
|
|
|
// LayoutTable 描述了表格布局的结构。
|
|
|
|
|
|
type LayoutTable struct {
|
2025-06-08 16:54:27 +08:00
|
|
|
|
*BaseLayout
|
2023-07-11 19:45:27 +08:00
|
|
|
|
|
2025-06-08 16:54:27 +08:00
|
|
|
|
Columns []*ViewTableColumn `json:"columns"` // 表格列
|
|
|
|
|
|
RowIDs []string `json:"rowIds"` // 行 ID,用于自定义排序
|
2023-07-11 23:40:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-12 17:14:54 +08:00
|
|
|
|
func (layoutTable *LayoutTable) GetItemIDs() (ret []string) {
|
|
|
|
|
|
return layoutTable.RowIDs
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-10 12:23:07 +08:00
|
|
|
|
func NewLayoutTable() *LayoutTable {
|
|
|
|
|
|
return &LayoutTable{
|
|
|
|
|
|
BaseLayout: &BaseLayout{
|
|
|
|
|
|
Spec: 0,
|
|
|
|
|
|
ID: ast.NewNodeID(),
|
|
|
|
|
|
Filters: []*ViewFilter{},
|
|
|
|
|
|
Sorts: []*ViewSort{},
|
|
|
|
|
|
PageSize: TableViewDefaultPageSize,
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-05 17:18:50 +08:00
|
|
|
|
// ViewTableColumn 描述了表格列的结构。
|
2023-07-12 19:10:05 +08:00
|
|
|
|
type ViewTableColumn struct {
|
|
|
|
|
|
ID string `json:"id"` // 列 ID
|
2023-07-11 23:40:05 +08:00
|
|
|
|
|
2023-07-12 23:52:25 +08:00
|
|
|
|
Wrap bool `json:"wrap"` // 是否换行
|
|
|
|
|
|
Hidden bool `json:"hidden"` // 是否隐藏
|
2023-11-10 10:22:19 +08:00
|
|
|
|
Pin bool `json:"pin"` // 是否固定
|
2023-07-12 23:52:25 +08:00
|
|
|
|
Width string `json:"width"` // 列宽度
|
2024-11-09 14:09:40 +08:00
|
|
|
|
Desc string `json:"desc,omitempty"` // 列描述
|
2023-07-12 23:52:25 +08:00
|
|
|
|
Calc *ColumnCalc `json:"calc,omitempty"` // 计算
|
2023-07-11 23:40:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-12 10:35:17 +08:00
|
|
|
|
// Table 描述了表格实例的结构。
|
|
|
|
|
|
type Table struct {
|
2025-06-08 17:22:03 +08:00
|
|
|
|
*BaseInstance
|
|
|
|
|
|
|
|
|
|
|
|
Columns []*TableColumn `json:"columns"` // 表格列
|
|
|
|
|
|
Rows []*TableRow `json:"rows"` // 表格行
|
|
|
|
|
|
RowCount int `json:"rowCount"` // 表格总行数
|
2023-07-12 10:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-07 21:44:36 +08:00
|
|
|
|
// TableColumn 描述了表格实例列的结构。
|
2023-07-12 19:10:05 +08:00
|
|
|
|
type TableColumn struct {
|
2025-06-08 17:22:03 +08:00
|
|
|
|
*BaseInstanceField
|
|
|
|
|
|
|
|
|
|
|
|
Wrap bool `json:"wrap"` // 是否换行
|
|
|
|
|
|
Pin bool `json:"pin"` // 是否固定
|
|
|
|
|
|
Width string `json:"width"` // 列宽度
|
|
|
|
|
|
Calc *ColumnCalc `json:"calc"` // 计算
|
2023-07-12 19:10:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-07 21:44:36 +08:00
|
|
|
|
// TableRow 描述了表格实例行的结构。
|
2025-06-05 17:18:50 +08:00
|
|
|
|
type TableRow struct {
|
|
|
|
|
|
ID string `json:"id"` // 行 ID
|
|
|
|
|
|
Cells []*TableCell `json:"cells"` // 行单元格
|
2023-10-13 10:44:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-07 21:44:36 +08:00
|
|
|
|
// TableCell 描述了表格实例单元格的结构。
|
2025-06-05 17:18:50 +08:00
|
|
|
|
type TableCell struct {
|
2025-06-08 17:22:03 +08:00
|
|
|
|
*BaseValue
|
|
|
|
|
|
|
|
|
|
|
|
Color string `json:"color"` // 单元格颜色
|
|
|
|
|
|
BgColor string `json:"bgColor"` // 单元格背景颜色
|
2023-07-12 19:10:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-12 17:14:54 +08:00
|
|
|
|
func (table *Table) GetColumn(id string) *TableColumn {
|
|
|
|
|
|
for _, column := range table.Columns {
|
|
|
|
|
|
if column.ID == id {
|
|
|
|
|
|
return column
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-11 17:39:32 +08:00
|
|
|
|
func (row *TableRow) GetID() string {
|
|
|
|
|
|
return row.ID
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-11 10:48:29 +08:00
|
|
|
|
func (row *TableRow) GetBlockValue() (ret *Value) {
|
|
|
|
|
|
for _, cell := range row.Cells {
|
|
|
|
|
|
if KeyTypeBlock == cell.ValueType {
|
|
|
|
|
|
ret = cell.Value
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-12 17:14:54 +08:00
|
|
|
|
func (row *TableRow) GetValues() (ret []*Value) {
|
|
|
|
|
|
ret = []*Value{}
|
2024-03-06 11:20:20 +08:00
|
|
|
|
for _, cell := range row.Cells {
|
2025-06-12 17:14:54 +08:00
|
|
|
|
if nil != cell.Value {
|
|
|
|
|
|
ret = append(ret, cell.Value)
|
2024-03-06 11:20:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-12 22:21:07 +08:00
|
|
|
|
func (row *TableRow) GetValue(keyID string) (ret *Value) {
|
|
|
|
|
|
for _, cell := range row.Cells {
|
|
|
|
|
|
if nil != cell.Value && keyID == cell.Value.KeyID {
|
|
|
|
|
|
ret = cell.Value
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-12 17:14:54 +08:00
|
|
|
|
func (table *Table) GetItems() (ret []Item) {
|
|
|
|
|
|
ret = []Item{}
|
|
|
|
|
|
for _, row := range table.Rows {
|
|
|
|
|
|
if nil != row {
|
|
|
|
|
|
ret = append(ret, row)
|
2024-07-16 23:44:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-12 17:14:54 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (table *Table) SetItems(items []Item) {
|
|
|
|
|
|
table.Rows = []*TableRow{}
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
|
|
table.Rows = append(table.Rows, item.(*TableRow))
|
|
|
|
|
|
}
|
2024-07-16 23:44:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-13 10:39:04 +08:00
|
|
|
|
func (table *Table) GetFields() []Field {
|
|
|
|
|
|
fields := []Field{}
|
|
|
|
|
|
for _, column := range table.Columns {
|
|
|
|
|
|
fields = append(fields, column)
|
|
|
|
|
|
}
|
|
|
|
|
|
return fields
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-08 12:08:31 +08:00
|
|
|
|
func (*Table) GetType() LayoutType {
|
2023-07-12 10:35:17 +08:00
|
|
|
|
return LayoutTypeTable
|
2023-07-11 19:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-11 21:31:19 +08:00
|
|
|
|
func (table *Table) GetID() string {
|
|
|
|
|
|
return table.ID
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-08 12:08:31 +08:00
|
|
|
|
func (table *Table) Sort(attrView *AttributeView) {
|
2023-07-11 19:45:27 +08:00
|
|
|
|
if 1 > len(table.Sorts) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type ColIndexSort struct {
|
|
|
|
|
|
Index int
|
|
|
|
|
|
Order SortOrder
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var colIndexSorts []*ColIndexSort
|
|
|
|
|
|
for _, s := range table.Sorts {
|
|
|
|
|
|
for i, c := range table.Columns {
|
|
|
|
|
|
if c.ID == s.Column {
|
|
|
|
|
|
colIndexSorts = append(colIndexSorts, &ColIndexSort{Index: i, Order: s.Order})
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-27 23:04:42 +08:00
|
|
|
|
editedValRows := map[string]bool{}
|
2024-03-03 22:00:42 +08:00
|
|
|
|
for i, row := range table.Rows {
|
2023-07-11 19:45:27 +08:00
|
|
|
|
for _, colIndexSort := range colIndexSorts {
|
2024-03-03 22:00:42 +08:00
|
|
|
|
val := table.Rows[i].Cells[colIndexSort.Index].Value
|
2024-04-15 15:36:38 +08:00
|
|
|
|
if KeyTypeCheckbox == val.Type {
|
|
|
|
|
|
if block := row.GetBlockValue(); nil != block && block.IsEdited() {
|
|
|
|
|
|
// 如果主键编辑过,则勾选框也算作编辑过,参与排序 https://github.com/siyuan-note/siyuan/issues/11016
|
|
|
|
|
|
editedValRows[row.ID] = true
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-27 23:04:42 +08:00
|
|
|
|
if val.IsEdited() {
|
|
|
|
|
|
// 如果该行某列的值已经编辑过,则该行可参与排序
|
|
|
|
|
|
editedValRows[row.ID] = true
|
2024-03-03 22:00:42 +08:00
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-27 23:04:42 +08:00
|
|
|
|
// 将未编辑的行和已编辑的行分开排序
|
2024-03-03 22:00:42 +08:00
|
|
|
|
var uneditedRows, editedRows []*TableRow
|
|
|
|
|
|
for _, row := range table.Rows {
|
2024-03-27 23:04:42 +08:00
|
|
|
|
if _, ok := editedValRows[row.ID]; ok {
|
2024-03-03 22:00:42 +08:00
|
|
|
|
editedRows = append(editedRows, row)
|
2024-03-27 23:04:42 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
uneditedRows = append(uneditedRows, row)
|
2024-03-03 22:00:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 {
|
2024-03-15 19:14:57 +08:00
|
|
|
|
sorted := true
|
2024-03-03 22:00:42 +08:00
|
|
|
|
for _, colIndexSort := range colIndexSorts {
|
|
|
|
|
|
val1 := editedRows[i].Cells[colIndexSort.Index].Value
|
|
|
|
|
|
val2 := editedRows[j].Cells[colIndexSort.Index].Value
|
2024-04-15 13:27:19 +08:00
|
|
|
|
if nil == val1 || val1.IsEmpty() {
|
|
|
|
|
|
if nil != val2 && !val2.IsEmpty() {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
sorted = false
|
|
|
|
|
|
continue
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if nil == val2 || val2.IsEmpty() {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
2024-03-03 22:00:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-21 10:03:08 +08:00
|
|
|
|
result := val1.Compare(val2, attrView)
|
2023-07-11 19:45:27 +08:00
|
|
|
|
if 0 == result {
|
2024-03-15 19:14:57 +08:00
|
|
|
|
sorted = false
|
2023-07-11 19:45:27 +08:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
2024-03-15 19:14:57 +08:00
|
|
|
|
sorted = true
|
2023-07-11 19:45:27 +08:00
|
|
|
|
|
|
|
|
|
|
if colIndexSort.Order == SortOrderAsc {
|
|
|
|
|
|
return 0 > result
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0 < result
|
|
|
|
|
|
}
|
2024-03-15 19:14:57 +08:00
|
|
|
|
|
|
|
|
|
|
if !sorted {
|
|
|
|
|
|
key1 := editedRows[i].GetBlockValue()
|
|
|
|
|
|
if nil == key1 {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
key2 := editedRows[j].GetBlockValue()
|
|
|
|
|
|
if nil == key2 {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
return key1.CreatedAt < key2.CreatedAt
|
|
|
|
|
|
}
|
2023-07-11 19:45:27 +08:00
|
|
|
|
return false
|
|
|
|
|
|
})
|
2024-03-03 22:00:42 +08:00
|
|
|
|
|
|
|
|
|
|
// 将包含未编辑的行放在最后
|
|
|
|
|
|
table.Rows = append(editedRows, uneditedRows...)
|
2024-03-04 13:54:17 +08:00
|
|
|
|
if 1 > len(table.Rows) {
|
|
|
|
|
|
table.Rows = []*TableRow{}
|
|
|
|
|
|
}
|
2023-07-11 19:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-08 12:08:31 +08:00
|
|
|
|
func (table *Table) Filter(attrView *AttributeView) {
|
2025-06-13 10:39:04 +08:00
|
|
|
|
filter(table, attrView)
|
2023-07-11 19:45:27 +08:00
|
|
|
|
}
|