From d38311c48cec7b96dff29a9378b65801f80f0a0d Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Thu, 5 Oct 2023 12:02:17 +0800 Subject: [PATCH] :art: Supports searching database blocks by the view title https://github.com/siyuan-note/siyuan/issues/9348 --- kernel/av/av.go | 4 ++++ kernel/model/attribute_view.go | 41 ++++++++++++++-------------------- kernel/model/import.go | 5 +++-- kernel/model/transaction.go | 4 ++-- kernel/treenode/node.go | 14 ++++++++++++ 5 files changed, 40 insertions(+), 28 deletions(-) diff --git a/kernel/av/av.go b/kernel/av/av.go index 5b0373b2227..4b2d55072d5 100644 --- a/kernel/av/av.go +++ b/kernel/av/av.go @@ -493,3 +493,7 @@ var ( ErrViewNotFound = errors.New("view not found") ErrKeyNotFound = errors.New("key not found") ) + +const ( + NodeAttrNameAvs = "custom-avs" // 用于标记块所属的属性视图,逗号分隔 av id +) diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index 66809a87a8e..23a6f67e7c3 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -70,7 +70,7 @@ func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) { ret = []*BlockAttributeViewKeys{} attrs := GetBlockAttrs(blockID) - avs := attrs[NodeAttrNameAvs] + avs := attrs[av.NodeAttrNameAvs] if "" == avs { return } @@ -352,16 +352,13 @@ func setAttributeViewName(operation *Operation) (err error) { return } - attrView.Name = operation.Data.(string) - - data, err := gulu.JSON.MarshalJSON(attrView) + view, err := attrView.GetView() if nil != err { return } - if err = gulu.JSON.UnmarshalJSON(data, attrView); nil != err { - return - } + attrView.Name = operation.Data.(string) + view.Name = operation.Data.(string) err = av.SaveAttributeView(attrView) return @@ -554,13 +551,13 @@ func addAttributeViewBlock(blockID string, operation *Operation, tree *parse.Tre if !operation.IsDetached { attrs := parse.IAL2Map(node.KramdownIAL) - if "" == attrs[NodeAttrNameAvs] { - attrs[NodeAttrNameAvs] = operation.AvID + if "" == attrs[av.NodeAttrNameAvs] { + attrs[av.NodeAttrNameAvs] = operation.AvID } else { - avIDs := strings.Split(attrs[NodeAttrNameAvs], ",") + avIDs := strings.Split(attrs[av.NodeAttrNameAvs], ",") avIDs = append(avIDs, operation.AvID) avIDs = gulu.Str.RemoveDuplicatedElem(avIDs) - attrs[NodeAttrNameAvs] = strings.Join(avIDs, ",") + attrs[av.NodeAttrNameAvs] = strings.Join(avIDs, ",") } if err = setNodeAttrsWithTx(tx, node, tree, attrs); nil != err { @@ -628,15 +625,15 @@ func (tx *Transaction) removeAttributeViewBlock(operation *Operation) (err error node.RemoveIALAttr("custom-hidden") } - if avs := attrs[NodeAttrNameAvs]; "" != avs { + if avs := attrs[av.NodeAttrNameAvs]; "" != avs { avIDs := strings.Split(avs, ",") avIDs = gulu.Str.RemoveElem(avIDs, operation.AvID) if 0 == len(avIDs) { - delete(attrs, NodeAttrNameAvs) - node.RemoveIALAttr(NodeAttrNameAvs) + delete(attrs, av.NodeAttrNameAvs) + node.RemoveIALAttr(av.NodeAttrNameAvs) } else { - attrs[NodeAttrNameAvs] = strings.Join(avIDs, ",") - node.SetIALAttr(NodeAttrNameAvs, strings.Join(avIDs, ",")) + attrs[av.NodeAttrNameAvs] = strings.Join(avIDs, ",") + node.SetIALAttr(av.NodeAttrNameAvs, strings.Join(avIDs, ",")) } } @@ -1160,17 +1157,17 @@ func bindBlockAv(tx *Transaction, avID, blockID string) { } attrs := parse.IAL2Map(node.KramdownIAL) - if "" == attrs[NodeAttrNameAvs] { - attrs[NodeAttrNameAvs] = avID + if "" == attrs[av.NodeAttrNameAvs] { + attrs[av.NodeAttrNameAvs] = avID } else { - avIDs := strings.Split(attrs[NodeAttrNameAvs], ",") + avIDs := strings.Split(attrs[av.NodeAttrNameAvs], ",") if gulu.Str.Contains(avID, avIDs) { return } avIDs = append(avIDs, avID) avIDs = gulu.Str.RemoveDuplicatedElem(avIDs) - attrs[NodeAttrNameAvs] = strings.Join(avIDs, ",") + attrs[av.NodeAttrNameAvs] = strings.Join(avIDs, ",") } if nil != tx { @@ -1346,7 +1343,3 @@ func updateAttributeViewColumnOption(operation *Operation) (err error) { err = av.SaveAttributeView(attrView) return } - -const ( - NodeAttrNameAvs = "custom-avs" // 用于标记块所属的属性视图,逗号分隔 av id -) diff --git a/kernel/model/import.go b/kernel/model/import.go index e8c5c0ae046..8fe66f800bf 100644 --- a/kernel/model/import.go +++ b/kernel/model/import.go @@ -22,6 +22,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/siyuan-note/siyuan/kernel/av" "image" "image/jpeg" "image/png" @@ -154,7 +155,7 @@ func ImportSY(zipPath, boxID, toPath string) (err error) { // 重新指向数据库属性值 ial := parse.IAL2Map(n.KramdownIAL) for k, _ := range ial { - if strings.HasPrefix(k, NodeAttrNameAvs) { + if strings.HasPrefix(k, av.NodeAttrNameAvs) { avBlockIDs[oldNodeID] = newNodeID } } @@ -257,7 +258,7 @@ func ImportSY(zipPath, boxID, toPath string) (err error) { ial := parse.IAL2Map(n.KramdownIAL) for k, v := range ial { - if strings.HasPrefix(k, NodeAttrNameAvs) { + if strings.HasPrefix(k, av.NodeAttrNameAvs) { newKey, newVal := k, v for oldAvID, newAvID := range avIDs { newKey = strings.ReplaceAll(newKey, oldAvID, newAvID) diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go index 32adc945385..67eef3fc397 100644 --- a/kernel/model/transaction.go +++ b/kernel/model/transaction.go @@ -714,7 +714,7 @@ func (tx *Transaction) doDelete(operation *Operation) (ret *TxErr) { } func syncDelete2AttributeView(node *ast.Node) { - avs := node.IALAttr(NodeAttrNameAvs) + avs := node.IALAttr(av.NodeAttrNameAvs) if "" == avs { return } @@ -1233,7 +1233,7 @@ func refreshDynamicRefTexts(updatedDefNodes map[string]*ast.Node, updatedTrees m // 2. 更新属性视图主键内容 for _, updatedDefNode := range updatedDefNodes { - avs := updatedDefNode.IALAttr(NodeAttrNameAvs) + avs := updatedDefNode.IALAttr(av.NodeAttrNameAvs) if "" == avs { continue } diff --git a/kernel/treenode/node.go b/kernel/treenode/node.go index 6b37e3192cf..4d47ba43813 100644 --- a/kernel/treenode/node.go +++ b/kernel/treenode/node.go @@ -18,6 +18,7 @@ package treenode import ( "bytes" + "github.com/siyuan-note/siyuan/kernel/av" "strings" "sync" @@ -139,6 +140,19 @@ func NodeStaticContent(node *ast.Node, excludeTypes []string, includeTextMarkATi if ast.NodeDocument == node.Type { return node.IALAttr("title") + } else if ast.NodeAttributeView == node.Type { + if "" != node.AttributeViewID { + attrView, err := av.ParseAttributeView(node.AttributeViewID) + if nil == err { + buf := bytes.Buffer{} + for _, v := range attrView.Views { + buf.WriteString(v.Name) + buf.WriteString(" ") + } + return strings.TrimSpace(buf.String()) + } + } + return "" } buf := bytes.Buffer{}