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 (
|
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"` // 表格列
|
2023-07-11 23:40:05 +08:00
|
|
|
|
|
2025-07-02 17:02:40 +08:00
|
|
|
|
// TODO RowIDs 字段已经废弃,计划于 2026 年 6 月 30 日后删除 https://github.com/siyuan-note/siyuan/issues/15194
|
|
|
|
|
|
//Deprecated
|
|
|
|
|
|
RowIDs []string `json:"rowIds"` // 行 ID,用于自定义排序
|
2025-06-12 17:14:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-10 12:23:07 +08:00
|
|
|
|
func NewLayoutTable() *LayoutTable {
|
|
|
|
|
|
return &LayoutTable{
|
|
|
|
|
|
BaseLayout: &BaseLayout{
|
2025-06-30 12:50:50 +08:00
|
|
|
|
Spec: 0,
|
|
|
|
|
|
ID: ast.NewNodeID(),
|
|
|
|
|
|
ShowIcon: true,
|
2025-06-10 12:23:07 +08:00
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-05 17:18:50 +08:00
|
|
|
|
// ViewTableColumn 描述了表格列的结构。
|
2023-07-12 19:10:05 +08:00
|
|
|
|
type ViewTableColumn struct {
|
2025-06-30 15:06:43 +08:00
|
|
|
|
*BaseField
|
2023-07-11 23:40:05 +08:00
|
|
|
|
|
2025-07-04 16:42:37 +08:00
|
|
|
|
Pin bool `json:"pin"` // 是否固定
|
|
|
|
|
|
Width string `json:"width"` // 列宽度
|
|
|
|
|
|
Calc *FieldCalc `json:"calc,omitempty"` // 计算规则
|
2023-07-11 23:40:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-04 12:00:47 +08:00
|
|
|
|
// Table 描述了表格视图实例的结构。
|
2023-07-12 10:35:17 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
2025-07-05 12:11:12 +08:00
|
|
|
|
Pin bool `json:"pin"` // 是否固定
|
|
|
|
|
|
Width string `json:"width"` // 列宽度
|
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-08-09 17:10:22 +08:00
|
|
|
|
func (table *Table) CountItems() int {
|
|
|
|
|
|
return len(table.Rows)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-13 10:49:09 +08:00
|
|
|
|
func (table *Table) GetFields() (ret []Field) {
|
|
|
|
|
|
ret = []Field{}
|
2025-06-13 10:39:04 +08:00
|
|
|
|
for _, column := range table.Columns {
|
2025-06-13 10:49:09 +08:00
|
|
|
|
ret = append(ret, column)
|
2025-06-13 10:39:04 +08:00
|
|
|
|
}
|
2025-06-13 10:49:09 +08:00
|
|
|
|
return ret
|
2025-06-13 10:39:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-06 16:54:55 +08:00
|
|
|
|
func (table *Table) GetField(id string) (ret Field, fieldIndex int) {
|
2025-07-05 12:11:12 +08:00
|
|
|
|
for _, column := range table.Columns {
|
|
|
|
|
|
if column.ID == id {
|
2025-07-06 16:54:55 +08:00
|
|
|
|
return column, fieldIndex
|
2025-07-05 12:11:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-06 16:54:55 +08:00
|
|
|
|
return nil, -1
|
2023-07-11 19:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-06 11:15:00 +08:00
|
|
|
|
func (table *Table) GetValue(itemID, keyID string) (ret *Value) {
|
|
|
|
|
|
for _, row := range table.Rows {
|
|
|
|
|
|
if row.ID == itemID {
|
|
|
|
|
|
return row.GetValue(keyID)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-05 12:11:12 +08:00
|
|
|
|
func (*Table) GetType() LayoutType {
|
|
|
|
|
|
return LayoutTypeTable
|
2023-07-11 19:45:27 +08:00
|
|
|
|
}
|