Skip to content

Commit

Permalink
🎨 Database-bound block primary key supports setting static anchor text
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Dec 21, 2024
1 parent be6f3a2 commit 776b4fd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
5 changes: 3 additions & 2 deletions kernel/av/av.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,9 @@ var (
)

const (
NodeAttrNameAvs = "custom-avs" // 用于标记块所属的属性视图,逗号分隔 av id
NodeAttrView = "custom-sy-av-view" // 用于标记块所属的属性视图视图 view id Database block support specified view https://github.com/siyuan-note/siyuan/issues/10443
NodeAttrNameAvs = "custom-avs" // 用于标记块所属的属性视图,逗号分隔 av id
NodeAttrView = "custom-sy-av-view" // 用于标记块所属的属性视图视图 view id Database block support specified view https://github.com/siyuan-note/siyuan/issues/10443
NodeAttrViewStaticText = "custom-sy-av-s-text" // 用于标记块所属的属性视图静态文本 Database-bound block primary key supports setting static anchor text https://github.com/siyuan-note/siyuan/issues/10049

NodeAttrViewNames = "av-names" // 用于临时标记块所属的属性视图名称,空格分隔
)
31 changes: 29 additions & 2 deletions kernel/model/attribute_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,13 @@ func GetBlockAttributeViewKeys(blockID 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
if nil != kv.Values[0].Block {
ial := sql.GetBlockAttrs(blockID)
if v := ial[av.NodeAttrViewStaticText+"-"+attrView.ID]; "" != v {
kv.Values[0].Block.Content = v
}
}
case av.KeyTypeRollup:
if nil == kv.Key.Rollup {
break
Expand Down Expand Up @@ -3099,8 +3106,13 @@ func UpdateAttributeViewCell(tx *Transaction, avID, keyID, rowID string, valueDa
unbindBlockAv(tx, avID, oldBoundBlockID)
bindBlockAv(tx, avID, val.BlockID)
} else { // 之前绑定的块和现在绑定的块一样
// 直接返回,因为锚文本不允许更改
return
// 设置静态锚文本 Database-bound block primary key supports setting static anchor text https://github.com/siyuan-note/siyuan/issues/10049
node, tree, getErr := getNodeByBlockID(tx, val.BlockID)
if nil != getErr || nil == node {
return
}

updateBlockValueStaticText(node, tree, avID, strings.TrimSpace(val.Block.Content))
}
}
}
Expand Down Expand Up @@ -3269,6 +3281,21 @@ func bindBlockAv0(tx *Transaction, avID string, node *ast.Node, tree *parse.Tree
return
}

func updateBlockValueStaticText(node *ast.Node, tree *parse.Tree, avID, text string) {
attrs := parse.IAL2Map(node.KramdownIAL)
attrs[av.NodeAttrViewStaticText+"-"+avID] = text
var err error
if nil != tree {
err = setNodeAttrsWithTx(nil, node, tree, attrs)
} else {
err = setNodeAttrs(node, nil, attrs)
}
if err != nil {
logging.LogWarnf("set node [%s] attrs failed: %s", node.ID, err)
return
}
}

func getNodeByBlockID(tx *Transaction, blockID string) (node *ast.Node, tree *parse.Tree, err error) {
if nil != tx {
tree, err = tx.loadTree(blockID)
Expand Down
9 changes: 9 additions & 0 deletions kernel/sql/av.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ func RenderAttributeViewTable(attrView *av.AttributeView, view *av.View, query s
for _, row := range ret.Rows {
for _, cell := range row.Cells {
switch cell.ValueType {
case av.KeyTypeBlock: // 对于主键可能需要填充静态锚文本 Database-bound block primary key supports setting static anchor text https://github.com/siyuan-note/siyuan/issues/10049
if nil != cell.Value.Block {
for k, v := range ials[row.ID] {
if k == av.NodeAttrViewStaticText+"-"+attrView.ID {
cell.Value.Block.Content = v
break
}
}
}
case av.KeyTypeRollup: // 渲染汇总列
rollupKey, _ := attrView.GetKey(cell.Value.KeyID)
if nil == rollupKey || nil == rollupKey.Rollup {
Expand Down

0 comments on commit 776b4fd

Please sign in to comment.