mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 23:20:13 +01:00
🎨 Improve database field default filling https://github.com/siyuan-note/siyuan/issues/11966
This commit is contained in:
parent
141c139782
commit
8695d86cf1
3 changed files with 122 additions and 39 deletions
|
|
@ -27,6 +27,31 @@ import (
|
|||
"github.com/siyuan-note/siyuan/kernel/util"
|
||||
)
|
||||
|
||||
// getAttributeViewAddingBlockDefaultValues 用于获取添加块时的默认值。
|
||||
// 存在过滤或分组条件时,添加块时需要填充默认值到过滤字段或分组字段中,前端需要调用该接口来获取这些默认值以便填充。
|
||||
func getAttributeViewAddingBlockDefaultValues(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)
|
||||
viewID := arg["viewID"].(string)
|
||||
var groupID string
|
||||
if groupIDArg := arg["groupID"]; nil != groupIDArg {
|
||||
groupID = groupIDArg.(string)
|
||||
}
|
||||
var previousID string
|
||||
if nil != arg["previousID"] {
|
||||
previousID = arg["previousID"].(string)
|
||||
}
|
||||
|
||||
ret.Data = model.GetAttrViewAddingBlockDefaultValues(avID, viewID, groupID, previousID)
|
||||
}
|
||||
|
||||
func batchReplaceAttributeViewBlocks(c *gin.Context) {
|
||||
// Add kernel API `/api/av/batchReplaceAttributeViewBlocks` https://github.com/siyuan-note/siyuan/issues/15313
|
||||
ret := gulu.Ret.NewResult()
|
||||
|
|
|
|||
|
|
@ -462,6 +462,7 @@ func ServeAPI(ginServer *gin.Engine) {
|
|||
ginServer.Handle("POST", "/api/av/changeAttrViewLayout", model.CheckAuth, changeAttrViewLayout)
|
||||
ginServer.Handle("POST", "/api/av/setAttrViewGroup", model.CheckAuth, setAttrViewGroup)
|
||||
ginServer.Handle("POST", "/api/av/batchReplaceAttributeViewBlocks", model.CheckAuth, batchReplaceAttributeViewBlocks)
|
||||
ginServer.Handle("POST", "/api/av/getAttributeViewAddingBlockDefaultValues", model.CheckAuth, getAttributeViewAddingBlockDefaultValues)
|
||||
|
||||
ginServer.Handle("POST", "/api/ai/chatGPT", model.CheckAuth, model.CheckAdminRole, chatGPT)
|
||||
ginServer.Handle("POST", "/api/ai/chatGPTWithAction", model.CheckAuth, model.CheckAdminRole, chatGPTWithAction)
|
||||
|
|
|
|||
|
|
@ -44,6 +44,81 @@ import (
|
|||
"github.com/xrash/smetrics"
|
||||
)
|
||||
|
||||
func GetAttrViewAddingBlockDefaultValues(avID, viewID, groupID, previousBlockID string) (ret map[string]*av.Value) {
|
||||
ret = map[string]*av.Value{}
|
||||
|
||||
attrView, err := av.ParseAttributeView(avID)
|
||||
if err != nil {
|
||||
logging.LogErrorf("parse attribute view [%s] failed: %s", avID, err)
|
||||
return
|
||||
}
|
||||
|
||||
view := attrView.GetView(viewID)
|
||||
if nil == view {
|
||||
logging.LogErrorf("view [%s] not found in attribute view [%s]", viewID, avID)
|
||||
return
|
||||
}
|
||||
|
||||
groupView := view
|
||||
if "" != groupID {
|
||||
groupView = view.GetGroup(groupID)
|
||||
}
|
||||
if nil == groupView {
|
||||
logging.LogErrorf("group [%s] not found in view [%s] of attribute view [%s]", groupID, viewID, avID)
|
||||
return
|
||||
}
|
||||
return getAttrViewAddingBlockDefaultValues(attrView, view, groupView, previousBlockID)
|
||||
}
|
||||
|
||||
func getAttrViewAddingBlockDefaultValues(attrView *av.AttributeView, view, groupView *av.View, previousBlockID string) (ret map[string]*av.Value) {
|
||||
ret = map[string]*av.Value{}
|
||||
|
||||
nearItem := getNearItem(attrView, view, groupView, previousBlockID)
|
||||
filterKeyIDs := map[string]bool{}
|
||||
for _, f := range view.Filters {
|
||||
filterKeyIDs[f.Column] = true
|
||||
}
|
||||
|
||||
for _, filter := range view.Filters {
|
||||
keyValues, _ := attrView.GetKeyValues(filter.Column)
|
||||
if nil == keyValues {
|
||||
continue
|
||||
}
|
||||
|
||||
var defaultVal *av.Value
|
||||
if nil != nearItem {
|
||||
defaultVal = nearItem.GetValue(filter.Column)
|
||||
}
|
||||
|
||||
newValue := filter.GetAffectValue(keyValues.Key, defaultVal)
|
||||
if nil != newValue {
|
||||
ret[keyValues.Key.ID] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
groupKey := view.GetGroupKey(attrView)
|
||||
if nil != groupKey && !filterKeyIDs[groupKey.ID] /* 命中了过滤条件的话就不重复处理了 */ {
|
||||
if keyValues, _ := attrView.GetKeyValues(groupKey.ID); nil != keyValues {
|
||||
newValue := getNewValueByNearItem(nearItem, groupKey, ast.NewNodeID())
|
||||
|
||||
newValue.ID = ast.NewNodeID()
|
||||
newValue.CreatedAt = util.CurrentTimeMillis()
|
||||
newValue.UpdatedAt = newValue.CreatedAt + 1000
|
||||
newValue.KeyID = keyValues.Key.ID
|
||||
|
||||
if av.KeyTypeSelect == groupKey.Type || av.KeyTypeMSelect == groupKey.Type {
|
||||
// 因为单选或多选只能按选项分组,并且可能存在空白分组(前面可能找不到临近项) ,所以单选或多选类型的分组字段使用分组值内容对应的选项
|
||||
if opt := groupKey.GetOption(groupView.GroupValue); nil != opt && groupValueDefault != groupView.GroupValue {
|
||||
newValue.MSelect = append(newValue.MSelect, &av.ValueSelect{Content: opt.Name, Color: opt.Color})
|
||||
}
|
||||
}
|
||||
|
||||
ret[groupKey.ID] = newValue
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (tx *Transaction) doSortAttrViewGroup(operation *Operation) (ret *TxErr) {
|
||||
if err := sortAttributeViewGroup(operation.AvID, operation.BlockID, operation.PreviousID, operation.ID); nil != err {
|
||||
return &TxErr{code: TxErrHandleAttributeView, id: operation.AvID, msg: err.Error()}
|
||||
|
|
@ -3075,23 +3150,6 @@ func addAttributeViewBlock(now int64, avID, blockID, groupID, previousBlockID, a
|
|||
|
||||
// 如果存在过滤条件,则将过滤条件应用到新添加的块上
|
||||
if nil != view && 0 < len(view.Filters) && !ignoreFillFilter {
|
||||
sameKeyFilterSort := false // 是否在同一个字段上同时存在过滤和排序
|
||||
if 0 < len(view.Sorts) {
|
||||
sortKeys := map[string]bool{}
|
||||
for _, s := range view.Sorts {
|
||||
sortKeys[s.Column] = true
|
||||
}
|
||||
|
||||
for k := range filterKeyIDs {
|
||||
if sortKeys[k] {
|
||||
sameKeyFilterSort = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !sameKeyFilterSort {
|
||||
// 如果在同一个字段上仅存在过滤条件,则将过滤条件应用到新添加的块上
|
||||
for _, filter := range view.Filters {
|
||||
for _, keyValues := range attrView.KeyValues {
|
||||
if keyValues.Key.ID == filter.Column {
|
||||
|
|
@ -3121,7 +3179,6 @@ func addAttributeViewBlock(now int64, avID, blockID, groupID, previousBlockID, a
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 处理日期字段默认填充当前创建时间
|
||||
// The database date field supports filling the current time by default https://github.com/siyuan-note/siyuan/issues/10823
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue