🎨 Supports searching database blocks by the view title https://github.com/siyuan-note/siyuan/issues/9348

This commit is contained in:
Daniel 2023-10-05 12:02:17 +08:00
parent cf154dcaa1
commit d38311c48c
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
5 changed files with 40 additions and 28 deletions

View file

@ -493,3 +493,7 @@ var (
ErrViewNotFound = errors.New("view not found") ErrViewNotFound = errors.New("view not found")
ErrKeyNotFound = errors.New("key not found") ErrKeyNotFound = errors.New("key not found")
) )
const (
NodeAttrNameAvs = "custom-avs" // 用于标记块所属的属性视图,逗号分隔 av id
)

View file

@ -70,7 +70,7 @@ func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) {
ret = []*BlockAttributeViewKeys{} ret = []*BlockAttributeViewKeys{}
attrs := GetBlockAttrs(blockID) attrs := GetBlockAttrs(blockID)
avs := attrs[NodeAttrNameAvs] avs := attrs[av.NodeAttrNameAvs]
if "" == avs { if "" == avs {
return return
} }
@ -352,16 +352,13 @@ func setAttributeViewName(operation *Operation) (err error) {
return return
} }
attrView.Name = operation.Data.(string) view, err := attrView.GetView()
data, err := gulu.JSON.MarshalJSON(attrView)
if nil != err { if nil != err {
return return
} }
if err = gulu.JSON.UnmarshalJSON(data, attrView); nil != err { attrView.Name = operation.Data.(string)
return view.Name = operation.Data.(string)
}
err = av.SaveAttributeView(attrView) err = av.SaveAttributeView(attrView)
return return
@ -554,13 +551,13 @@ func addAttributeViewBlock(blockID string, operation *Operation, tree *parse.Tre
if !operation.IsDetached { if !operation.IsDetached {
attrs := parse.IAL2Map(node.KramdownIAL) attrs := parse.IAL2Map(node.KramdownIAL)
if "" == attrs[NodeAttrNameAvs] { if "" == attrs[av.NodeAttrNameAvs] {
attrs[NodeAttrNameAvs] = operation.AvID attrs[av.NodeAttrNameAvs] = operation.AvID
} else { } else {
avIDs := strings.Split(attrs[NodeAttrNameAvs], ",") avIDs := strings.Split(attrs[av.NodeAttrNameAvs], ",")
avIDs = append(avIDs, operation.AvID) avIDs = append(avIDs, operation.AvID)
avIDs = gulu.Str.RemoveDuplicatedElem(avIDs) 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 { 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") node.RemoveIALAttr("custom-hidden")
} }
if avs := attrs[NodeAttrNameAvs]; "" != avs { if avs := attrs[av.NodeAttrNameAvs]; "" != avs {
avIDs := strings.Split(avs, ",") avIDs := strings.Split(avs, ",")
avIDs = gulu.Str.RemoveElem(avIDs, operation.AvID) avIDs = gulu.Str.RemoveElem(avIDs, operation.AvID)
if 0 == len(avIDs) { if 0 == len(avIDs) {
delete(attrs, NodeAttrNameAvs) delete(attrs, av.NodeAttrNameAvs)
node.RemoveIALAttr(NodeAttrNameAvs) node.RemoveIALAttr(av.NodeAttrNameAvs)
} else { } else {
attrs[NodeAttrNameAvs] = strings.Join(avIDs, ",") attrs[av.NodeAttrNameAvs] = strings.Join(avIDs, ",")
node.SetIALAttr(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) attrs := parse.IAL2Map(node.KramdownIAL)
if "" == attrs[NodeAttrNameAvs] { if "" == attrs[av.NodeAttrNameAvs] {
attrs[NodeAttrNameAvs] = avID attrs[av.NodeAttrNameAvs] = avID
} else { } else {
avIDs := strings.Split(attrs[NodeAttrNameAvs], ",") avIDs := strings.Split(attrs[av.NodeAttrNameAvs], ",")
if gulu.Str.Contains(avID, avIDs) { if gulu.Str.Contains(avID, avIDs) {
return return
} }
avIDs = append(avIDs, avID) avIDs = append(avIDs, avID)
avIDs = gulu.Str.RemoveDuplicatedElem(avIDs) avIDs = gulu.Str.RemoveDuplicatedElem(avIDs)
attrs[NodeAttrNameAvs] = strings.Join(avIDs, ",") attrs[av.NodeAttrNameAvs] = strings.Join(avIDs, ",")
} }
if nil != tx { if nil != tx {
@ -1346,7 +1343,3 @@ func updateAttributeViewColumnOption(operation *Operation) (err error) {
err = av.SaveAttributeView(attrView) err = av.SaveAttributeView(attrView)
return return
} }
const (
NodeAttrNameAvs = "custom-avs" // 用于标记块所属的属性视图,逗号分隔 av id
)

View file

@ -22,6 +22,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/siyuan-note/siyuan/kernel/av"
"image" "image"
"image/jpeg" "image/jpeg"
"image/png" "image/png"
@ -154,7 +155,7 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
// 重新指向数据库属性值 // 重新指向数据库属性值
ial := parse.IAL2Map(n.KramdownIAL) ial := parse.IAL2Map(n.KramdownIAL)
for k, _ := range ial { for k, _ := range ial {
if strings.HasPrefix(k, NodeAttrNameAvs) { if strings.HasPrefix(k, av.NodeAttrNameAvs) {
avBlockIDs[oldNodeID] = newNodeID avBlockIDs[oldNodeID] = newNodeID
} }
} }
@ -257,7 +258,7 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
ial := parse.IAL2Map(n.KramdownIAL) ial := parse.IAL2Map(n.KramdownIAL)
for k, v := range ial { for k, v := range ial {
if strings.HasPrefix(k, NodeAttrNameAvs) { if strings.HasPrefix(k, av.NodeAttrNameAvs) {
newKey, newVal := k, v newKey, newVal := k, v
for oldAvID, newAvID := range avIDs { for oldAvID, newAvID := range avIDs {
newKey = strings.ReplaceAll(newKey, oldAvID, newAvID) newKey = strings.ReplaceAll(newKey, oldAvID, newAvID)

View file

@ -714,7 +714,7 @@ func (tx *Transaction) doDelete(operation *Operation) (ret *TxErr) {
} }
func syncDelete2AttributeView(node *ast.Node) { func syncDelete2AttributeView(node *ast.Node) {
avs := node.IALAttr(NodeAttrNameAvs) avs := node.IALAttr(av.NodeAttrNameAvs)
if "" == avs { if "" == avs {
return return
} }
@ -1233,7 +1233,7 @@ func refreshDynamicRefTexts(updatedDefNodes map[string]*ast.Node, updatedTrees m
// 2. 更新属性视图主键内容 // 2. 更新属性视图主键内容
for _, updatedDefNode := range updatedDefNodes { for _, updatedDefNode := range updatedDefNodes {
avs := updatedDefNode.IALAttr(NodeAttrNameAvs) avs := updatedDefNode.IALAttr(av.NodeAttrNameAvs)
if "" == avs { if "" == avs {
continue continue
} }

View file

@ -18,6 +18,7 @@ package treenode
import ( import (
"bytes" "bytes"
"github.com/siyuan-note/siyuan/kernel/av"
"strings" "strings"
"sync" "sync"
@ -139,6 +140,19 @@ func NodeStaticContent(node *ast.Node, excludeTypes []string, includeTextMarkATi
if ast.NodeDocument == node.Type { if ast.NodeDocument == node.Type {
return node.IALAttr("title") 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{} buf := bytes.Buffer{}