🎨 嵌入块查询结果中显示块引用计数 https://github.com/siyuan-note/siyuan/issues/7191

This commit is contained in:
Liang Ding 2023-01-31 19:40:55 +08:00
parent ef436cf12e
commit 35baf86b6e
No known key found for this signature in database
GPG Key ID: 136F30F901A2231D
2 changed files with 35 additions and 0 deletions

View File

@ -19,6 +19,7 @@ package model
import (
"errors"
"fmt"
"strconv"
"time"
"github.com/88250/lute"
@ -466,6 +467,18 @@ func getEmbeddedBlock(embedBlockID string, trees map[string]*parse.Tree, sqlBloc
return
}
// 嵌入块查询结果中显示块引用计数 https://github.com/siyuan-note/siyuan/issues/7191
var defIDs []string
for _, n := range nodes {
defIDs = append(defIDs, n.ID)
}
refCount := sql.QueryRefCount(defIDs)
for _, n := range nodes {
if cnt := refCount[n.ID]; 0 < cnt {
n.SetIALAttr("refcount", strconv.Itoa(cnt))
}
}
luteEngine := NewLute()
luteEngine.RenderOptions.ProtyleContenteditable = false // 不可编辑
dom := renderBlockDOMByNodes(nodes, luteEngine)

View File

@ -79,6 +79,28 @@ func queryRefTexts() (ret []string) {
return
}
func QueryRefCount(defIDs []string) (ret map[string]int) {
ret = map[string]int{}
ids := strings.Join(defIDs, "','")
ids = "('" + ids + "')"
rows, err := query("SELECT def_block_id, COUNT(*) AS ref_cnt FROM refs WHERE def_block_id IN " + ids + " GROUP BY def_block_id")
if nil != err {
logging.LogErrorf("sql query failed: %s", err)
return
}
defer rows.Close()
for rows.Next() {
var id string
var cnt int
if err = rows.Scan(&id, &cnt); nil != err {
logging.LogErrorf("query scan field failed: %s", err)
return
}
ret[id] = cnt
}
return
}
func QueryRootChildrenRefCount(defRootID string) (ret map[string]int) {
ret = map[string]int{}
rows, err := query("SELECT def_block_id, COUNT(*) AS ref_cnt FROM refs WHERE def_block_root_id = ? GROUP BY def_block_id", defRootID)