mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-09-22 00:20:47 +02:00
🎨 The database rollup field supports using the relation field https://github.com/siyuan-note/siyuan/issues/15851
Signed-off-by: Daniel <845765@qq.com>
This commit is contained in:
parent
9b62385e86
commit
07cfc58642
5 changed files with 112 additions and 68 deletions
|
@ -81,7 +81,7 @@ const genSearchList = (element: Element, keyword: string, avId: string, isRelati
|
|||
showMessage(window.siyuan.languages.selectRelation);
|
||||
return;
|
||||
}
|
||||
fetchPost(isRelation ? "/api/av/searchAttributeViewRelationKey" : "/api/av/searchAttributeViewNonRelationKey", {
|
||||
fetchPost(isRelation ? "/api/av/searchAttributeViewRelationKey" : "/api/av/searchAttributeViewRollupDestKeys", {
|
||||
avID: avId,
|
||||
keyword
|
||||
}, (response) => {
|
||||
|
|
|
@ -567,6 +567,24 @@ func searchAttributeViewNonRelationKey(c *gin.Context) {
|
|||
}
|
||||
}
|
||||
|
||||
func searchAttributeViewRollupDestKeys(c *gin.Context) {
|
||||
ret := gulu.Ret.NewResult()
|
||||
defer c.JSON(http.StatusOK, ret)
|
||||
|
||||
arg, _ := util.JsonArg(c, ret)
|
||||
if nil == arg {
|
||||
return
|
||||
}
|
||||
|
||||
avID := arg["avID"].(string)
|
||||
keyword := arg["keyword"].(string)
|
||||
|
||||
rollupDestKeys := model.SearchAttributeViewRollupDestKeys(avID, keyword)
|
||||
ret.Data = map[string]interface{}{
|
||||
"keys": rollupDestKeys,
|
||||
}
|
||||
}
|
||||
|
||||
func searchAttributeViewRelationKey(c *gin.Context) {
|
||||
ret := gulu.Ret.NewResult()
|
||||
defer c.JSON(http.StatusOK, ret)
|
||||
|
|
|
@ -448,6 +448,7 @@ func ServeAPI(ginServer *gin.Engine) {
|
|||
ginServer.Handle("POST", "/api/av/getAttributeView", model.CheckAuth, model.CheckReadonly, getAttributeView)
|
||||
ginServer.Handle("POST", "/api/av/searchAttributeViewRelationKey", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, searchAttributeViewRelationKey)
|
||||
ginServer.Handle("POST", "/api/av/searchAttributeViewNonRelationKey", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, searchAttributeViewNonRelationKey)
|
||||
ginServer.Handle("POST", "/api/av/searchAttributeViewRollupDestKeys", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, searchAttributeViewRollupDestKeys)
|
||||
ginServer.Handle("POST", "/api/av/getAttributeViewFilterSort", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, getAttributeViewFilterSort)
|
||||
ginServer.Handle("POST", "/api/av/addAttributeViewKey", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, addAttributeViewKey)
|
||||
ginServer.Handle("POST", "/api/av/removeAttributeViewKey", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, removeAttributeViewKey)
|
||||
|
|
|
@ -1311,6 +1311,26 @@ func SearchAttributeViewNonRelationKey(avID, keyword string) (ret []*av.Key) {
|
|||
return
|
||||
}
|
||||
|
||||
func SearchAttributeViewRollupDestKeys(avID, keyword string) (ret []*av.Key) {
|
||||
waitForSyncingStorages()
|
||||
|
||||
ret = []*av.Key{}
|
||||
attrView, err := av.ParseAttributeView(avID)
|
||||
if err != nil {
|
||||
logging.LogErrorf("parse attribute view [%s] failed: %s", avID, err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, keyValues := range attrView.KeyValues {
|
||||
if av.KeyTypeRollup != keyValues.Key.Type && av.KeyTypeLineNumber != keyValues.Key.Type {
|
||||
if strings.Contains(strings.ToLower(keyValues.Key.Name), strings.ToLower(keyword)) {
|
||||
ret = append(ret, keyValues.Key)
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func SearchAttributeViewRelationKey(avID, keyword string) (ret []*av.Key) {
|
||||
waitForSyncingStorages()
|
||||
|
||||
|
@ -1592,8 +1612,7 @@ func GetBlockAttributeViewKeys(nodeID string) (ret []*BlockAttributeViewKeys) {
|
|||
}
|
||||
}
|
||||
|
||||
// 先渲染主键、创建时间、更新时间
|
||||
|
||||
// 渲染主键、创建时间、更新时间
|
||||
for _, kv := range keyValues {
|
||||
switch kv.Key.Type {
|
||||
case av.KeyTypeBlock: // 对于主键可能需要填充静态锚文本 Database-bound block primary key supports setting static anchor text https://github.com/siyuan-note/siyuan/issues/10049
|
||||
|
@ -1627,8 +1646,7 @@ func GetBlockAttributeViewKeys(nodeID string) (ret []*BlockAttributeViewKeys) {
|
|||
}
|
||||
}
|
||||
|
||||
// 再渲染关联和汇总
|
||||
|
||||
// 渲染关联和汇总
|
||||
rollupFurtherCollections := sql.GetFurtherCollections(attrView, cachedAttrViews)
|
||||
for _, kv := range keyValues {
|
||||
switch kv.Key.Type {
|
||||
|
@ -1685,7 +1703,7 @@ func GetBlockAttributeViewKeys(nodeID string) (ret []*BlockAttributeViewKeys) {
|
|||
}
|
||||
}
|
||||
|
||||
// 最后渲染模板
|
||||
// 渲染模板
|
||||
templateKeys, _ := sql.GetTemplateKeysByResolutionOrder(attrView)
|
||||
var renderTemplateErr error
|
||||
for _, templateKey := range templateKeys {
|
||||
|
|
131
kernel/sql/av.go
131
kernel/sql/av.go
|
@ -352,7 +352,7 @@ func fillAttributeViewBaseValue(baseValue *av.BaseValue, fieldID, itemID string,
|
|||
}
|
||||
|
||||
func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection av.Collection, ials map[string]map[string]string, depth *int, cachedAttrViews map[string]*av.AttributeView) {
|
||||
// 先渲染主键、创建时间、更新时间
|
||||
// 渲染主键、创建时间、更新时间
|
||||
|
||||
for _, item := range collection.GetItems() {
|
||||
for _, value := range item.GetValues() {
|
||||
|
@ -415,8 +415,42 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection
|
|||
}
|
||||
}
|
||||
|
||||
// 再渲染关联和汇总
|
||||
// 渲染关联
|
||||
for _, item := range collection.GetItems() {
|
||||
for _, value := range item.GetValues() {
|
||||
if av.KeyTypeRelation != value.Type {
|
||||
continue
|
||||
}
|
||||
|
||||
value.Relation.Contents = nil
|
||||
relKey, _ := attrView.GetKey(value.KeyID)
|
||||
if nil != relKey && nil != relKey.Relation {
|
||||
destAv := cachedAttrViews[relKey.Relation.AvID]
|
||||
if nil == destAv {
|
||||
destAv, _ = av.ParseAttributeView(relKey.Relation.AvID)
|
||||
if nil != destAv {
|
||||
cachedAttrViews[relKey.Relation.AvID] = destAv
|
||||
}
|
||||
}
|
||||
if nil != destAv {
|
||||
blocks := map[string]*av.Value{}
|
||||
blockValues := destAv.GetBlockKeyValues()
|
||||
if nil != blockValues {
|
||||
for _, blockValue := range blockValues.Values {
|
||||
blocks[blockValue.BlockID] = blockValue
|
||||
}
|
||||
for _, blockID := range value.Relation.BlockIDs {
|
||||
if val := blocks[blockID]; nil != val {
|
||||
value.Relation.Contents = append(value.Relation.Contents, val)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 渲染汇总
|
||||
rollupFurtherCollections := map[string]av.Collection{}
|
||||
for _, field := range collection.GetFields() {
|
||||
if av.KeyTypeRollup != field.GetType() {
|
||||
|
@ -451,7 +485,7 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection
|
|||
|
||||
isSameAv := destAv.ID == attrView.ID
|
||||
var furtherCollection av.Collection
|
||||
if av.KeyTypeTemplate == destKey.Type || (!isSameAv && (av.KeyTypeUpdated == destKey.Type || av.KeyTypeCreated == destKey.Type)) {
|
||||
if av.KeyTypeTemplate == destKey.Type || (!isSameAv && (av.KeyTypeUpdated == destKey.Type || av.KeyTypeCreated == destKey.Type || av.KeyTypeRelation == destKey.Type)) {
|
||||
viewable := renderView(destAv, destAv.Views[0], "", depth, cachedAttrViews)
|
||||
if nil != viewable {
|
||||
furtherCollection = viewable.(av.Collection)
|
||||
|
@ -465,70 +499,43 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection
|
|||
|
||||
for _, item := range collection.GetItems() {
|
||||
for _, value := range item.GetValues() {
|
||||
itemID := item.GetID()
|
||||
if av.KeyTypeRollup != value.Type {
|
||||
continue
|
||||
}
|
||||
|
||||
switch value.Type {
|
||||
case av.KeyTypeRollup: // 渲染汇总
|
||||
rollupKey, _ := attrView.GetKey(value.KeyID)
|
||||
if nil == rollupKey || nil == rollupKey.Rollup {
|
||||
break
|
||||
}
|
||||
rollupKey, _ := attrView.GetKey(value.KeyID)
|
||||
if nil == rollupKey || nil == rollupKey.Rollup {
|
||||
break
|
||||
}
|
||||
|
||||
relKey, _ := attrView.GetKey(rollupKey.Rollup.RelationKeyID)
|
||||
if nil == relKey || nil == relKey.Relation {
|
||||
break
|
||||
}
|
||||
relKey, _ := attrView.GetKey(rollupKey.Rollup.RelationKeyID)
|
||||
if nil == relKey || nil == relKey.Relation {
|
||||
break
|
||||
}
|
||||
|
||||
relVal := attrView.GetValue(relKey.ID, itemID)
|
||||
if nil == relVal || nil == relVal.Relation {
|
||||
break
|
||||
}
|
||||
relVal := attrView.GetValue(relKey.ID, item.GetID())
|
||||
if nil == relVal || nil == relVal.Relation {
|
||||
break
|
||||
}
|
||||
|
||||
destAv := cachedAttrViews[relKey.Relation.AvID]
|
||||
if nil == destAv {
|
||||
destAv, _ = av.ParseAttributeView(relKey.Relation.AvID)
|
||||
if nil != destAv {
|
||||
cachedAttrViews[relKey.Relation.AvID] = destAv
|
||||
}
|
||||
}
|
||||
if nil == destAv {
|
||||
break
|
||||
}
|
||||
|
||||
destKey, _ := destAv.GetKey(rollupKey.Rollup.KeyID)
|
||||
if nil == destKey {
|
||||
break
|
||||
}
|
||||
|
||||
furtherCollection := rollupFurtherCollections[rollupKey.ID]
|
||||
value.Rollup.BuildContents(destAv.KeyValues, destKey, relVal, rollupKey.Rollup.Calc, furtherCollection)
|
||||
case av.KeyTypeRelation: // 渲染关联
|
||||
value.Relation.Contents = nil
|
||||
relKey, _ := attrView.GetKey(value.KeyID)
|
||||
if nil != relKey && nil != relKey.Relation {
|
||||
destAv := cachedAttrViews[relKey.Relation.AvID]
|
||||
if nil == destAv {
|
||||
destAv, _ = av.ParseAttributeView(relKey.Relation.AvID)
|
||||
if nil != destAv {
|
||||
cachedAttrViews[relKey.Relation.AvID] = destAv
|
||||
}
|
||||
}
|
||||
if nil != destAv {
|
||||
blocks := map[string]*av.Value{}
|
||||
blockValues := destAv.GetBlockKeyValues()
|
||||
if nil != blockValues {
|
||||
for _, blockValue := range blockValues.Values {
|
||||
blocks[blockValue.BlockID] = blockValue
|
||||
}
|
||||
for _, blockID := range value.Relation.BlockIDs {
|
||||
if val := blocks[blockID]; nil != val {
|
||||
value.Relation.Contents = append(value.Relation.Contents, val)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
destAv := cachedAttrViews[relKey.Relation.AvID]
|
||||
if nil == destAv {
|
||||
destAv, _ = av.ParseAttributeView(relKey.Relation.AvID)
|
||||
if nil != destAv {
|
||||
cachedAttrViews[relKey.Relation.AvID] = destAv
|
||||
}
|
||||
}
|
||||
if nil == destAv {
|
||||
break
|
||||
}
|
||||
|
||||
destKey, _ := destAv.GetKey(rollupKey.Rollup.KeyID)
|
||||
if nil == destKey {
|
||||
break
|
||||
}
|
||||
|
||||
furtherCollection := rollupFurtherCollections[rollupKey.ID]
|
||||
value.Rollup.BuildContents(destAv.KeyValues, destKey, relVal, rollupKey.Rollup.Calc, furtherCollection)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -561,7 +568,7 @@ func GetFurtherCollections(attrView *av.AttributeView, cachedAttrViews map[strin
|
|||
isSameAv := destAv.ID == attrView.ID
|
||||
|
||||
var furtherCollection av.Collection
|
||||
if av.KeyTypeTemplate == destKey.Type || (!isSameAv && (av.KeyTypeUpdated == destKey.Type || av.KeyTypeCreated == destKey.Type)) {
|
||||
if av.KeyTypeTemplate == destKey.Type || (!isSameAv && (av.KeyTypeUpdated == destKey.Type || av.KeyTypeCreated == destKey.Type || av.KeyTypeRelation == destKey.Type)) {
|
||||
viewable := RenderView(destAv, destAv.Views[0], "")
|
||||
if nil != viewable {
|
||||
furtherCollection = viewable.(av.Collection)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue