From 9c1d6a1a70e15ebb164d350f5aca31e58e6e8397 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 3 Mar 2023 10:26:02 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=8C=81=E4=B9=85=E5=8C=96=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E8=A7=86=E5=9B=BE=20https://github.com/siyuan-note/si?= =?UTF-8?q?yuan/issues/2829?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/av/av.go | 14 +++++------ kernel/av/cell.go | 14 +++-------- kernel/av/column.go | 45 ++++++++-------------------------- kernel/av/column_block.go | 4 +-- kernel/av/column_date.go | 2 +- kernel/av/column_number.go | 2 +- kernel/av/column_relation.go | 2 +- kernel/av/column_rollup.go | 2 +- kernel/av/column_select.go | 2 +- kernel/av/column_text.go | 4 +-- kernel/av/row.go | 28 +++++++++++++++++++++ kernel/model/attribute_view.go | 11 ++++----- 12 files changed, 63 insertions(+), 67 deletions(-) create mode 100644 kernel/av/row.go diff --git a/kernel/av/av.go b/kernel/av/av.go index 5cebca21f..ce9cab646 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 0a28a897d..d2559d5c7 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 1bb6886a0..ee57ba28a 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"` // 列类型 +// Column 描述了属性视图的基础结构。 +type Column struct { + ID string `json:"id"` // 列 ID + Name string `json:"name"` // 列名 + Type 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 -} - -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 ab458a853..47e5f172f 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 7f71c3e18..e7787b63b 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 d1914af83..dc48325bc 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 73c076085..fe17f83de 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 a2eac26b2..20306bacf 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 db00ac56a..20bc17bd9 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 6a23abbd6..db61dc332 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 000000000..845c92b42 --- /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 . + +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 df0326b2f..31013da1c 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] = "" }