Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: 持久化属性视图 https://github.com/siyuan-note/siyuan/issues/2829

Liang Ding9c1d6a1a70e1
diff --git a/kernel/av/av.go b/kernel/av/av.go
index 5cebca21f0..ce9cab6469 100644
--- a/kernel/av/av.go
+++ b/kernel/av/av.go
@@ -31,9 +31,9 @@ import (
// AttributeView 描述了属性视图的结构。
type AttributeView struct {
- ID string `json:"id"` // 属性视图 ID
- Columns []Column `json:"columns"` // 表格列名
- Rows [][]Cell `json:"rows"` // 表格行记录
+ ID string `json:"id"` // 属性视图 ID
+ Columns []interface{} `json:"columns"` // 表格列名
+ Rows []*Row `json:"rows"` // 表格行记录
Type AttributeViewType `json:"type"` // 属性视图类型
Projections []string `json:"projections"` // 显示的列名,SELECT *
@@ -51,8 +51,8 @@ const (
func NewAttributeView(id string) *AttributeView {
return &AttributeView{
ID: id,
- Columns: []Column{NewColumnBlock()},
- Rows: [][]Cell{},
+ Columns: []interface{}{NewColumnBlock()},
+ Rows: []*Row{},
Type: AttributeViewTypeTable,
Projections: []string{},
Filters: []*AttributeViewFilter{},
@@ -63,12 +63,12 @@ func NewAttributeView(id string) *AttributeView {
func (av *AttributeView) GetColumnNames() (ret []string) {
ret = []string{}
for _, column := range av.Columns {
- ret = append(ret, column.Name())
+ ret = append(ret, column.(*Column).Name)
}
return
}
-func (av *AttributeView) InsertColumn(index int, column Column) {
+func (av *AttributeView) InsertColumn(index int, column interface{}) {
if 0 > index || len(av.Columns) == index {
av.Columns = append(av.Columns, column)
return
diff --git a/kernel/av/cell.go b/kernel/av/cell.go
index 0a28a897d5..d2559d5c74 100644
--- a/kernel/av/cell.go
+++ b/kernel/av/cell.go
@@ -16,20 +16,14 @@
package av
-type Cell interface {
-
- // Value 返回单元格的值。
- Value() string
+type Cell struct {
+ Value string
}
type CellBlock struct {
- ID string `json:"id"`
+ *Cell
}
func NewCellBlock(blockID string) *CellBlock {
- return &CellBlock{ID: blockID}
-}
-
-func (c *CellBlock) Value() string {
- return c.ID
+ return &CellBlock{&Cell{Value: blockID}}
}
diff --git a/kernel/av/column.go b/kernel/av/column.go
index 1bb6886a0d..ee57ba28ae 100644
--- a/kernel/av/column.go
+++ b/kernel/av/column.go
@@ -18,19 +18,6 @@ package av
import "github.com/88250/lute/ast"
-// Column 描述了属性视图的列。
-type Column interface {
-
- // ID 用于获取列 ID。
- ID() string
-
- // Name 用于获取列名。
- Name() string
-
- // Type 用于获取列类型。
- Type() ColumnType
-}
-
type ColumnType string
const (
@@ -43,29 +30,17 @@ const (
ColumnTypeText ColumnType = "text"
)
-// BaseColumn 描述了属性视图的基础结构。
-type BaseColumn struct {
- BaseID string `json:"id"` // 列 ID
- BaseName string `json:"name"` // 列名
- BaseType ColumnType `json:"type"` // 列类型
-}
-
-func (c *BaseColumn) ID() string {
- return c.BaseID
-}
-
-func (c *BaseColumn) Name() string {
- return c.BaseName
-}
-
-func (c *BaseColumn) Type() ColumnType {
- return c.BaseType
+// Column 描述了属性视图的基础结构。
+type Column struct {
+ ID string `json:"id"` // 列 ID
+ Name string `json:"name"` // 列名
+ Type ColumnType `json:"type"` // 列类型
}
-func NewBaseColumn(name string, columnType ColumnType) *BaseColumn {
- return &BaseColumn{
- BaseID: ast.NewNodeID(),
- BaseName: name,
- BaseType: columnType,
+func NewColumn(name string, columnType ColumnType) *Column {
+ return &Column{
+ ID: ast.NewNodeID(),
+ Name: name,
+ Type: columnType,
}
}
diff --git a/kernel/av/column_block.go b/kernel/av/column_block.go
index ab458a853e..47e5f172fb 100644
--- a/kernel/av/column_block.go
+++ b/kernel/av/column_block.go
@@ -17,11 +17,11 @@
package av
type ColumnBlock struct {
- *BaseColumn
+ *Column
}
func NewColumnBlock() *ColumnBlock {
return &ColumnBlock{
- BaseColumn: NewBaseColumn("Block", ColumnTypeBlock),
+ Column: NewColumn("Block", ColumnTypeBlock),
}
}
diff --git a/kernel/av/column_date.go b/kernel/av/column_date.go
index 7f71c3e181..e7787b63b1 100644
--- a/kernel/av/column_date.go
+++ b/kernel/av/column_date.go
@@ -17,5 +17,5 @@
package av
type ColumnDate struct {
- *BaseColumn
+ *Column
}
diff --git a/kernel/av/column_number.go b/kernel/av/column_number.go
index d1914af839..dc48325bcc 100644
--- a/kernel/av/column_number.go
+++ b/kernel/av/column_number.go
@@ -17,5 +17,5 @@
package av
type ColumnNumber struct {
- *BaseColumn
+ *Column
}
diff --git a/kernel/av/column_relation.go b/kernel/av/column_relation.go
index 73c0760856..fe17f83de4 100644
--- a/kernel/av/column_relation.go
+++ b/kernel/av/column_relation.go
@@ -17,6 +17,6 @@
package av
type ColumnRelation struct {
- *BaseColumn
+ *Column
AttributeViewID string `json:"attributeViewId"` // 关联的属性视图 ID
}
diff --git a/kernel/av/column_rollup.go b/kernel/av/column_rollup.go
index a2eac26b29..20306bacf1 100644
--- a/kernel/av/column_rollup.go
+++ b/kernel/av/column_rollup.go
@@ -17,6 +17,6 @@
package av
type ColumnRollup struct {
- *BaseColumn
+ *Column
RelationColumnID string `json:"relationColumnId"` // 目标关联列 ID
}
diff --git a/kernel/av/column_select.go b/kernel/av/column_select.go
index db00ac56a3..20bc17bd90 100644
--- a/kernel/av/column_select.go
+++ b/kernel/av/column_select.go
@@ -17,7 +17,7 @@
package av
type ColumnSelect struct {
- *BaseColumn
+ *Column
Options []*ColumnSelectOption `json:"options"`
}
diff --git a/kernel/av/column_text.go b/kernel/av/column_text.go
index 6a23abbd6f..db61dc332a 100644
--- a/kernel/av/column_text.go
+++ b/kernel/av/column_text.go
@@ -17,11 +17,11 @@
package av
type ColumnText struct {
- *BaseColumn
+ *Column
}
func NewColumnText(name string) *ColumnText {
return &ColumnText{
- BaseColumn: NewBaseColumn(name, ColumnTypeText),
+ Column: NewColumn(name, ColumnTypeText),
}
}
diff --git a/kernel/av/row.go b/kernel/av/row.go
new file mode 100644
index 0000000000..845c92b42a
--- /dev/null
+++ b/kernel/av/row.go
@@ -0,0 +1,28 @@
+// SiYuan - Build Your Eternal Digital Garden
+// 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 "github.com/88250/lute/ast"
+
+type Row struct {
+ ID string `json:"id"`
+ Cells []interface{} `json:"cells"`
+}
+
+func NewRow() *Row {
+ return &Row{ID: ast.NewNodeID()}
+}
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index df0326b2fd..31013da1c5 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -19,7 +19,6 @@ package model
import (
"errors"
"fmt"
-
"github.com/88250/lute/parse"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/av"
@@ -94,7 +93,7 @@ func removeAttributeViewBlock(blockID, avID string, tree *parse.Tree) (err error
}
for i, row := range attrView.Rows {
- if row[0].Value() == blockID {
+ if row.Cells[0].(*av.Cell).Value == blockID {
attrView.Rows = append(attrView.Rows[:i], attrView.Rows[i+1:]...)
break
}
@@ -124,18 +123,18 @@ func addAttributeViewBlock(blockID, avID string, tree *parse.Tree) (err error) {
// 不允许重复添加相同的块到属性视图中
for _, row := range attrView.Rows {
- if row[0].Value() == blockID {
+ if row.Cells[0].(*av.Cell).Value == blockID {
return
}
}
- var row []av.Cell
- row = append(row, av.NewCellBlock(block.ID))
+ row := av.NewRow()
+ row.Cells = append(row.Cells, av.NewCellBlock(block.ID))
if 1 < len(attrView.Columns) {
attrs := parse.IAL2Map(node.KramdownIAL)
for _, col := range attrView.Columns[1:] {
- colName := col.Name()
+ colName := col.(*av.Column).Name
attrs[colName] = ""
}