mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-05-04 16:31:22 +08:00
♻️ Refactor av data structure
This commit is contained in:
parent
7ca8eab0cd
commit
fbd26d1fd6
@ -37,38 +37,38 @@ type AttributeView struct {
|
|||||||
Columns []*Column `json:"columns"` // 列
|
Columns []*Column `json:"columns"` // 列
|
||||||
Rows []*Row `json:"rows"` // 行
|
Rows []*Row `json:"rows"` // 行
|
||||||
|
|
||||||
CurrentViewID string `json:"currentViewId"` // 当前视图 ID
|
CurrentViewID string `json:"currentViewID"` // 当前视图 ID
|
||||||
Views []*View `json:"views"` // 视图
|
Views []*View `json:"views"` // 视图
|
||||||
}
|
}
|
||||||
|
|
||||||
// View 描述了视图的结构。
|
// View 描述了视图的结构。
|
||||||
type View struct {
|
type View struct {
|
||||||
ID string `json:"id"` // 视图 ID
|
ID string `json:"id"` // 视图 ID
|
||||||
Name string `json:"name"` // 视图名称
|
Name string `json:"name"` // 视图名称
|
||||||
Type ViewType `json:"type"` // 视图类型
|
|
||||||
|
|
||||||
Table *Table `json:"table,omitempty"` // 表格视图
|
CurrentLayoutID string `json:"CurrentLayoutID"` // 当前布局 ID
|
||||||
// TODO Kanban *Kanban `json:"kanban,omitempty"` // 看板视图
|
CurrentLayoutType LayoutType `json:"type"` // 当前布局类型
|
||||||
|
Table *LayoutTable `json:"table,omitempty"` // 表格布局
|
||||||
}
|
}
|
||||||
|
|
||||||
// ViewType 描述了视图的类型。
|
// LayoutType 描述了视图布局的类型。
|
||||||
type ViewType string
|
type LayoutType string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ViewTypeTable ViewType = "table" // 属性视图类型 - 表格
|
LayoutTypeTable LayoutType = "table" // 属性视图类型 - 表格
|
||||||
ViewTypeKanban ViewType = "kanban" // 属性视图类型 - 看板
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewView() *View {
|
func NewView() *View {
|
||||||
id := ast.NewNodeID()
|
|
||||||
name := "Table"
|
name := "Table"
|
||||||
|
layoutID := ast.NewNodeID()
|
||||||
return &View{
|
return &View{
|
||||||
ID: id,
|
ID: ast.NewNodeID(),
|
||||||
Name: name,
|
Name: name,
|
||||||
Type: ViewTypeTable,
|
CurrentLayoutID: layoutID,
|
||||||
Table: &Table{
|
CurrentLayoutType: LayoutTypeTable,
|
||||||
|
Table: &LayoutTable{
|
||||||
Spec: 0,
|
Spec: 0,
|
||||||
ID: id,
|
ID: layoutID,
|
||||||
Filters: []*ViewFilter{},
|
Filters: []*ViewFilter{},
|
||||||
Sorts: []*ViewSort{},
|
Sorts: []*ViewSort{},
|
||||||
},
|
},
|
||||||
@ -81,7 +81,7 @@ type Viewable interface {
|
|||||||
Sortable
|
Sortable
|
||||||
Calculable
|
Calculable
|
||||||
|
|
||||||
GetType() ViewType
|
GetType() LayoutType
|
||||||
GetID() string
|
GetID() string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,13 +21,14 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Table 描述了表格视图的结构。
|
// LayoutTable 描述了表格布局的结构。
|
||||||
type Table struct {
|
type LayoutTable struct {
|
||||||
Spec int `json:"spec"` // 视图格式版本
|
Spec int `json:"spec"` // 布局格式版本
|
||||||
ID string `json:"id"` // 视图 ID
|
ID string `json:"id"` // 布局 ID
|
||||||
|
|
||||||
Columns []*TableColumn `json:"columns"` // 表格列
|
Columns []*TableColumn `json:"columns"` // 表格列
|
||||||
Rows []*TableRow `json:"rows"` // 表格行
|
ColIDs []string `json:"colIds"` // 列 ID,用于自定义排序
|
||||||
|
RowIDs []string `json:"rowIds"` // 行 ID,用于自定义排序
|
||||||
Filters []*ViewFilter `json:"filters"` // 过滤规则
|
Filters []*ViewFilter `json:"filters"` // 过滤规则
|
||||||
Sorts []*ViewSort `json:"sorts"` // 排序规则
|
Sorts []*ViewSort `json:"sorts"` // 排序规则
|
||||||
}
|
}
|
||||||
@ -49,8 +50,18 @@ type TableRow struct {
|
|||||||
Cells []*Cell `json:"cells"`
|
Cells []*Cell `json:"cells"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (table *Table) GetType() ViewType {
|
// Table 描述了表格实例的结构。
|
||||||
return ViewTypeTable
|
type Table struct {
|
||||||
|
ID string `json:"id"` // 表格布局 ID
|
||||||
|
Name string `json:"name"` // 表格名称
|
||||||
|
Filters []*ViewFilter `json:"filters"` // 过滤规则
|
||||||
|
Sorts []*ViewSort `json:"sorts"` // 排序规则
|
||||||
|
Columns []*TableColumn `json:"columns"` // 表格列
|
||||||
|
Rows []*TableRow `json:"rows"` // 表格行
|
||||||
|
}
|
||||||
|
|
||||||
|
func (table *Table) GetType() LayoutType {
|
||||||
|
return LayoutTypeTable
|
||||||
}
|
}
|
||||||
|
|
||||||
func (table *Table) GetID() string {
|
func (table *Table) GetID() string {
|
||||||
|
@ -58,8 +58,8 @@ func RenderAttributeView(avID string) (viewable av.Viewable, attrView *av.Attrib
|
|||||||
view = attrView.Views[0]
|
view = attrView.Views[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
switch view.Type {
|
switch view.CurrentLayoutType {
|
||||||
case av.ViewTypeTable:
|
case av.LayoutTypeTable:
|
||||||
viewable, err = renderAttributeViewTable(attrView, view)
|
viewable, err = renderAttributeViewTable(attrView, view)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,7 +70,13 @@ func RenderAttributeView(avID string) (viewable av.Viewable, attrView *av.Attrib
|
|||||||
}
|
}
|
||||||
|
|
||||||
func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *av.Table, err error) {
|
func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *av.Table, err error) {
|
||||||
ret = view.Table
|
ret = &av.Table{
|
||||||
|
ID: view.Table.ID,
|
||||||
|
Name: view.Name,
|
||||||
|
Filters: view.Table.Filters,
|
||||||
|
Sorts: view.Table.Sorts,
|
||||||
|
}
|
||||||
|
|
||||||
for _, avRow := range attrView.Rows {
|
for _, avRow := range attrView.Rows {
|
||||||
row := &av.TableRow{ID: avRow.ID, Cells: avRow.Cells}
|
row := &av.TableRow{ID: avRow.ID, Cells: avRow.Cells}
|
||||||
ret.Rows = append(ret.Rows, row)
|
ret.Rows = append(ret.Rows, row)
|
||||||
@ -404,27 +410,32 @@ func sortAttributeViewRow(operation *Operation) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var row *av.Row
|
view, err := attrView.GetView(operation.ViewID)
|
||||||
|
if nil != err {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var rowID string
|
||||||
var index, previousIndex int
|
var index, previousIndex int
|
||||||
for i, r := range attrView.Rows {
|
for i, r := range view.Table.RowIDs {
|
||||||
if r.ID == operation.ID {
|
if r == operation.ID {
|
||||||
row = r
|
rowID = r
|
||||||
index = i
|
index = i
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if nil == row {
|
if "" == rowID {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
attrView.Rows = append(attrView.Rows[:index], attrView.Rows[index+1:]...)
|
view.Table.RowIDs = append(view.Table.RowIDs[:index], view.Table.RowIDs[index+1:]...)
|
||||||
for i, r := range attrView.Rows {
|
for i, r := range view.Table.RowIDs {
|
||||||
if r.ID == operation.PreviousID {
|
if r == operation.PreviousID {
|
||||||
previousIndex = i + 1
|
previousIndex = i + 1
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
attrView.Rows = util.InsertElem(attrView.Rows, previousIndex, row)
|
view.Table.RowIDs = util.InsertElem(view.Table.RowIDs, previousIndex, rowID)
|
||||||
|
|
||||||
err = av.SaveAttributeView(attrView)
|
err = av.SaveAttributeView(attrView)
|
||||||
return
|
return
|
||||||
@ -444,6 +455,11 @@ func sortAttributeViewColumn(operation *Operation) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
view, err := attrView.GetView(operation.ViewID)
|
||||||
|
if nil != err {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var col *av.Column
|
var col *av.Column
|
||||||
var index, previousIndex int
|
var index, previousIndex int
|
||||||
for i, column := range attrView.Columns {
|
for i, column := range attrView.Columns {
|
||||||
@ -561,7 +577,6 @@ func updateAttributeViewColumn(operation *Operation) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (tx *Transaction) doRemoveAttrViewColumn(operation *Operation) (ret *TxErr) {
|
func (tx *Transaction) doRemoveAttrViewColumn(operation *Operation) (ret *TxErr) {
|
||||||
err := removeAttributeViewColumn(operation)
|
err := removeAttributeViewColumn(operation)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
@ -694,7 +709,6 @@ func (tx *Transaction) doUpdateAttrViewColOptions(operation *Operation) (ret *Tx
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (tx *Transaction) doSetAttrView(operation *Operation) (ret *TxErr) {
|
func (tx *Transaction) doSetAttrView(operation *Operation) (ret *TxErr) {
|
||||||
err := setAttributeView(operation)
|
err := setAttributeView(operation)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
|
Loading…
Reference in New Issue
Block a user