Skip to content

Commit

Permalink
🎨 Supports searching database blocks by the view title #9348
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Oct 5, 2023
1 parent cf154dc commit d38311c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 28 deletions.
4 changes: 4 additions & 0 deletions kernel/av/av.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,7 @@ var (
ErrViewNotFound = errors.New("view not found")
ErrKeyNotFound = errors.New("key not found")
)

const (
NodeAttrNameAvs = "custom-avs" // 用于标记块所属的属性视图,逗号分隔 av id
)
41 changes: 17 additions & 24 deletions kernel/model/attribute_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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, ","))
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -1346,7 +1343,3 @@ func updateAttributeViewColumnOption(operation *Operation) (err error) {
err = av.SaveAttributeView(attrView)
return
}

const (
NodeAttrNameAvs = "custom-avs" // 用于标记块所属的属性视图,逗号分隔 av id
)
5 changes: 3 additions & 2 deletions kernel/model/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/siyuan-note/siyuan/kernel/av"
"image"
"image/jpeg"
"image/png"
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions kernel/model/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 14 additions & 0 deletions kernel/treenode/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package treenode

import (
"bytes"
"github.com/siyuan-note/siyuan/kernel/av"
"strings"
"sync"

Expand Down Expand Up @@ -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{}
Expand Down

0 comments on commit d38311c

Please sign in to comment.