🎨 Attribute Panel - Database attributes custom sorting https://github.com/siyuan-note/siyuan/issues/11428
This commit is contained in:
parent
0f6a280513
commit
2058f9a0c0
5 changed files with 90 additions and 17 deletions
|
@ -211,7 +211,7 @@ func removeAttributeViewKey(c *gin.Context) {
|
|||
util.PushReloadAttrView(avID)
|
||||
}
|
||||
|
||||
func sortAttributeViewKey(c *gin.Context) {
|
||||
func sortAttributeViewViewKey(c *gin.Context) {
|
||||
ret := gulu.Ret.NewResult()
|
||||
defer c.JSON(http.StatusOK, ret)
|
||||
|
||||
|
@ -228,7 +228,30 @@ func sortAttributeViewKey(c *gin.Context) {
|
|||
keyID := arg["keyID"].(string)
|
||||
previousKeyID := arg["previousKeyID"].(string)
|
||||
|
||||
err := model.SortAttributeViewKey(avID, viewID, keyID, previousKeyID)
|
||||
err := model.SortAttributeViewViewKey(avID, viewID, keyID, previousKeyID)
|
||||
if nil != err {
|
||||
ret.Code = -1
|
||||
ret.Msg = err.Error()
|
||||
return
|
||||
}
|
||||
|
||||
util.PushReloadAttrView(avID)
|
||||
}
|
||||
|
||||
func sortAttributeViewKey(c *gin.Context) {
|
||||
ret := gulu.Ret.NewResult()
|
||||
defer c.JSON(http.StatusOK, ret)
|
||||
|
||||
arg, ok := util.JsonArg(c, ret)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
avID := arg["avID"].(string)
|
||||
keyID := arg["keyID"].(string)
|
||||
previousKeyID := arg["previousKeyID"].(string)
|
||||
|
||||
err := model.SortAttributeViewKey(avID, keyID, previousKeyID)
|
||||
if nil != err {
|
||||
ret.Code = -1
|
||||
ret.Msg = err.Error()
|
||||
|
|
|
@ -416,6 +416,7 @@ func ServeAPI(ginServer *gin.Engine) {
|
|||
ginServer.Handle("POST", "/api/av/getAttributeViewFilterSort", model.CheckAuth, model.CheckReadonly, getAttributeViewFilterSort)
|
||||
ginServer.Handle("POST", "/api/av/addAttributeViewKey", model.CheckAuth, model.CheckReadonly, addAttributeViewKey)
|
||||
ginServer.Handle("POST", "/api/av/removeAttributeViewKey", model.CheckAuth, model.CheckReadonly, removeAttributeViewKey)
|
||||
ginServer.Handle("POST", "/api/av/sortAttributeViewViewKey", model.CheckAuth, model.CheckReadonly, sortAttributeViewViewKey)
|
||||
ginServer.Handle("POST", "/api/av/sortAttributeViewKey", model.CheckAuth, model.CheckReadonly, sortAttributeViewKey)
|
||||
ginServer.Handle("POST", "/api/av/addAttributeViewValues", model.CheckAuth, model.CheckReadonly, addAttributeViewValues)
|
||||
ginServer.Handle("POST", "/api/av/removeAttributeViewValues", model.CheckAuth, model.CheckReadonly, removeAttributeViewValues)
|
||||
|
|
|
@ -40,7 +40,8 @@ type AttributeView struct {
|
|||
Spec int `json:"spec"` // 格式版本
|
||||
ID string `json:"id"` // 属性视图 ID
|
||||
Name string `json:"name"` // 属性视图名称
|
||||
KeyValues []*KeyValues `json:"keyValues"` // 属性视图属性列值
|
||||
KeyValues []*KeyValues `json:"keyValues"` // 属性视图属性键值
|
||||
KeyIDs []string `json:"keyIDs"` // 属性视图属性键 ID,用于排序
|
||||
ViewID string `json:"viewID"` // 当前视图 ID
|
||||
Views []*View `json:"views"` // 视图
|
||||
}
|
||||
|
|
|
@ -523,19 +523,14 @@ func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) {
|
|||
util.PushErrMsg(fmt.Sprintf(Conf.Language(44), util.EscapeHTML(renderTemplateErr.Error())), 30000)
|
||||
}
|
||||
|
||||
// Attribute Panel - Database sort attributes by view column order https://github.com/siyuan-note/siyuan/issues/9319
|
||||
viewID := attrs[av.NodeAttrView]
|
||||
view, _ := attrView.GetCurrentView(viewID)
|
||||
if nil != view {
|
||||
sorts := map[string]int{}
|
||||
for i, col := range view.Table.Columns {
|
||||
sorts[col.ID] = i
|
||||
}
|
||||
|
||||
sort.Slice(keyValues, func(i, j int) bool {
|
||||
return sorts[keyValues[i].Key.ID] < sorts[keyValues[j].Key.ID]
|
||||
})
|
||||
// 字段排序
|
||||
sorts := map[string]int{}
|
||||
for i, k := range attrView.KeyIDs {
|
||||
sorts[k] = i
|
||||
}
|
||||
sort.Slice(keyValues, func(i, j int) bool {
|
||||
return sorts[keyValues[i].Key.ID] < sorts[keyValues[j].Key.ID]
|
||||
})
|
||||
|
||||
blockIDs := treenode.GetMirrorAttrViewBlockIDs(avID)
|
||||
if 1 > len(blockIDs) {
|
||||
|
@ -2269,14 +2264,14 @@ func sortAttributeViewRow(operation *Operation) (err error) {
|
|||
}
|
||||
|
||||
func (tx *Transaction) doSortAttrViewColumn(operation *Operation) (ret *TxErr) {
|
||||
err := SortAttributeViewKey(operation.AvID, operation.BlockID, operation.ID, operation.PreviousID)
|
||||
err := SortAttributeViewViewKey(operation.AvID, operation.BlockID, operation.ID, operation.PreviousID)
|
||||
if nil != err {
|
||||
return &TxErr{code: TxErrWriteAttributeView, id: operation.AvID, msg: err.Error()}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func SortAttributeViewKey(avID, blockID, keyID, previousKeyID string) (err error) {
|
||||
func SortAttributeViewViewKey(avID, blockID, keyID, previousKeyID string) (err error) {
|
||||
if keyID == previousKeyID {
|
||||
// 拖拽到自己的右侧,不做任何操作 https://github.com/siyuan-note/siyuan/issues/11048
|
||||
return
|
||||
|
@ -2321,6 +2316,57 @@ func SortAttributeViewKey(avID, blockID, keyID, previousKeyID string) (err error
|
|||
return
|
||||
}
|
||||
|
||||
func (tx *Transaction) doSortAttrViewKey(operation *Operation) (ret *TxErr) {
|
||||
err := SortAttributeViewKey(operation.AvID, operation.ID, operation.PreviousID)
|
||||
if nil != err {
|
||||
return &TxErr{code: TxErrWriteAttributeView, id: operation.AvID, msg: err.Error()}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func SortAttributeViewKey(avID, keyID, previousKeyID string) (err error) {
|
||||
if keyID == previousKeyID {
|
||||
return
|
||||
}
|
||||
|
||||
attrView, err := av.ParseAttributeView(avID)
|
||||
if nil != err {
|
||||
return
|
||||
}
|
||||
|
||||
if 1 > len(attrView.KeyIDs) {
|
||||
for _, keyValues := range attrView.KeyValues {
|
||||
attrView.KeyIDs = append(attrView.KeyIDs, keyValues.Key.ID)
|
||||
}
|
||||
}
|
||||
|
||||
var currentKeyID string
|
||||
var idx, previousIndex int
|
||||
for i, k := range attrView.KeyIDs {
|
||||
if k == keyID {
|
||||
currentKeyID = k
|
||||
idx = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if "" == currentKeyID {
|
||||
return
|
||||
}
|
||||
|
||||
attrView.KeyIDs = append(attrView.KeyIDs[:idx], attrView.KeyIDs[idx+1:]...)
|
||||
|
||||
for i, k := range attrView.KeyIDs {
|
||||
if k == previousKeyID {
|
||||
previousIndex = i + 1
|
||||
break
|
||||
}
|
||||
}
|
||||
attrView.KeyIDs = util.InsertElem(attrView.KeyIDs, previousIndex, currentKeyID)
|
||||
|
||||
err = av.SaveAttributeView(attrView)
|
||||
return
|
||||
}
|
||||
|
||||
func (tx *Transaction) doAddAttrViewColumn(operation *Operation) (ret *TxErr) {
|
||||
var icon string
|
||||
if nil != operation.Data {
|
||||
|
|
|
@ -258,6 +258,8 @@ func performTx(tx *Transaction) (ret *TxErr) {
|
|||
ret = tx.doSortAttrViewRow(op)
|
||||
case "sortAttrViewCol":
|
||||
ret = tx.doSortAttrViewColumn(op)
|
||||
case "sortAttrViewKey":
|
||||
ret = tx.doSortAttrViewKey(op)
|
||||
case "updateAttrViewCell":
|
||||
ret = tx.doUpdateAttrViewCell(op)
|
||||
case "updateAttrViewColOptions":
|
||||
|
|
Loading…
Add table
Reference in a new issue