diff --git a/kernel/api/block.go b/kernel/api/block.go index 2e5f8df91..56ac83c20 100644 --- a/kernel/api/block.go +++ b/kernel/api/block.go @@ -438,11 +438,9 @@ func getRefIDs(c *gin.Context) { } id := arg["id"].(string) - refIDs, refTexts, defIDs, originalRefBlockIDs := model.GetBlockRefs(id, true) + refDefs, originalRefBlockIDs := model.GetBlockRefs(id) ret.Data = map[string]any{ - "refIDs": refIDs, - "refTexts": refTexts, - "defIDs": defIDs, + "refDefs": refDefs, "originalRefBlockIDs": originalRefBlockIDs, } } diff --git a/kernel/model/block.go b/kernel/model/block.go index 30cbaeb8b..1e41fc587 100644 --- a/kernel/model/block.go +++ b/kernel/model/block.go @@ -359,7 +359,7 @@ func TransferBlockRef(fromID, toID string, refIDs []string) (err error) { util.PushMsg(Conf.Language(116), 7000) if 1 > len(refIDs) { // 如果不指定 refIDs,则转移所有引用了 fromID 的块 - refIDs, _ = sql.QueryRefIDsByDefID(fromID, false) + refIDs = sql.QueryRefIDsByDefID(fromID, false) } trees := filesys.LoadTrees(refIDs) diff --git a/kernel/model/blockinfo.go b/kernel/model/blockinfo.go index fe49373a6..4f92a7eb5 100644 --- a/kernel/model/blockinfo.go +++ b/kernel/model/blockinfo.go @@ -91,9 +91,14 @@ func GetDocInfo(blockID string) (ret *BlockInfo) { } } - refIDs, _ := sql.QueryRefIDsByDefID(blockID, Conf.Editor.BacklinkContainChildren) - ret.RefIDs, _ = buildBacklinkListItemRefs(refIDs) - ret.RefCount = len(ret.RefIDs) // 填充块引计数 + refDefs := queryDocRefDefs(blockID) + buildBacklinkListItemRefs(refDefs) + var refIDs []string + for _, refDef := range refDefs { + refIDs = append(refIDs, refDef.RefID) + } + ret.RefIDs = refIDs + ret.RefCount = len(ret.RefIDs) // 填充属性视图角标 Display the database title on the block superscript https://github.com/siyuan-note/siyuan/issues/10545 avIDs := strings.Split(ret.IAL[av.NodeAttrNameAvs], ",") @@ -164,8 +169,8 @@ func GetDocsInfo(blockIDs []string, queryRefCount bool, queryAv bool) (rets []*B } } if queryRefCount { - ret.RefIDs, _ = sql.QueryRefIDsByDefID(blockID, Conf.Editor.BacklinkContainChildren) - ret.RefCount = len(ret.RefIDs) // 填充块引计数 + ret.RefIDs = sql.QueryRefIDsByDefID(blockID, Conf.Editor.BacklinkContainChildren) + ret.RefCount = len(ret.RefIDs) } if queryAv { @@ -321,10 +326,13 @@ func getNodeRefText0(node *ast.Node, maxLen int, removeLineBreak bool) string { return ret } -func GetBlockRefs(defID string, isBacklink bool) (refIDs, refTexts, defIDs []string, originalRefIDs map[string]string) { - refIDs = []string{} - refTexts = []string{} - defIDs = []string{} +type RefDefs struct { + RefID string `json:"refID"` + DefIDs []string `json:"defIDs"` +} + +func GetBlockRefs(defID string) (refDefs []*RefDefs, originalRefIDs map[string]string) { + refDefs = []*RefDefs{} originalRefIDs = map[string]string{} bt := treenode.GetBlockTree(defID) if nil == bt { @@ -332,15 +340,31 @@ func GetBlockRefs(defID string, isBacklink bool) (refIDs, refTexts, defIDs []str } isDoc := bt.ID == bt.RootID - refIDs, refTexts = sql.QueryRefIDsByDefID(defID, isDoc) if isDoc { - defIDs = sql.QueryChildDefIDsByRootDefID(defID) + refDefs = queryDocRefDefs(defID) } else { - defIDs = append(defIDs, defID) + refIDs := sql.QueryRefIDsByDefID(defID, false) + for _, refID := range refIDs { + refDefs = append(refDefs, &RefDefs{RefID: refID, DefIDs: []string{defID}}) + } } - if isBacklink { - refIDs, originalRefIDs = buildBacklinkListItemRefs(refIDs) + originalRefIDs = buildBacklinkListItemRefs(refDefs) + return +} + +func queryDocRefDefs(rootID string) (refDefs []*RefDefs) { + refDefs = []*RefDefs{} + refDefIDs := sql.QueryChildRefDefIDsByRootDefID(rootID) + for rID, dIDs := range refDefIDs { + var defIDs []string + for _, dID := range dIDs { + defIDs = append(defIDs, dID) + } + if 1 > len(defIDs) { + defIDs = []string{} + } + refDefs = append(refDefs, &RefDefs{RefID: rID, DefIDs: defIDs}) } return } @@ -559,10 +583,13 @@ func buildBlockBreadcrumb(node *ast.Node, excludeTypes []string, isEmbedBlock bo return } -func buildBacklinkListItemRefs(refIDs []string) (retRefIDs []string, originalRefBlockIDs map[string]string) { - retRefIDs = []string{} +func buildBacklinkListItemRefs(refDefs []*RefDefs) (originalRefBlockIDs map[string]string) { originalRefBlockIDs = map[string]string{} + var refIDs []string + for _, refDef := range refDefs { + refIDs = append(refIDs, refDef.RefID) + } sqlRefBlocks := sql.GetBlocks(refIDs) refBlocks := fromSQLBlocks(&sqlRefBlocks, "", 12) @@ -603,21 +630,17 @@ func buildBacklinkListItemRefs(refIDs []string) (retRefIDs []string, originalRef } if paragraphUseParentLi { - retRefIDs = append(retRefIDs, parent.ID) + for _, refDef := range refDefs { + if refDef.RefID == refBlock.ID { + refDef.RefID = parent.ID + break + } + } processedParagraphs.Add(parent.ID) - } else { - retRefIDs = append(retRefIDs, refBlock.ID) } originalRefBlockIDs[parent.ID] = refBlock.ID } } - - for _, ref := range refBlocks { - if !processedParagraphs.Contains(ref.ParentID) { - retRefIDs = append(retRefIDs, ref.ID) - } - } - retRefIDs = gulu.Str.RemoveDuplicatedElem(retRefIDs) return } diff --git a/kernel/model/push_reload.go b/kernel/model/push_reload.go index 767b33672..a010ee1d4 100644 --- a/kernel/model/push_reload.go +++ b/kernel/model/push_reload.go @@ -128,7 +128,7 @@ func refreshProtyle(rootID string) { } // 刷新关联的嵌入块 - refIDs, _ := sql.QueryRefIDsByDefID(rootID, true) + refIDs := sql.QueryRefIDsByDefID(rootID, true) var rootIDs []string bts := treenode.GetBlockTrees(refIDs) for _, bt := range bts { @@ -154,11 +154,11 @@ func refreshRefCount(rootID, blockID string) { isDoc := bt.ID == bt.RootID var rootRefIDs []string var refCount, rootRefCount int - refIDs, _ := sql.QueryRefIDsByDefID(bt.ID, isDoc) + refIDs := sql.QueryRefIDsByDefID(bt.ID, isDoc) if isDoc { rootRefIDs = refIDs } else { - rootRefIDs, _ = sql.QueryRefIDsByDefID(bt.RootID, true) + rootRefIDs = sql.QueryRefIDsByDefID(bt.RootID, true) } refCount = len(refIDs) rootRefCount = len(rootRefIDs) diff --git a/kernel/sql/block_ref_query.go b/kernel/sql/block_ref_query.go index ebc28e117..6f15b9f7d 100644 --- a/kernel/sql/block_ref_query.go +++ b/kernel/sql/block_ref_query.go @@ -315,6 +315,29 @@ func queryDefIDsByNameAlias(keyword string, excludeIDs []string) (ret []string) return } +func QueryChildRefDefIDsByRootDefID(rootDefID string) (ret map[string][]string) { + ret = map[string][]string{} + rows, err := query("SELECT block_id, def_block_id FROM refs WHERE def_block_root_id = ?", rootDefID) + if err != nil { + logging.LogErrorf("sql query failed: %s", err) + return + } + defer rows.Close() + for rows.Next() { + var defID, refID string + if err = rows.Scan(&defID, &refID); err != nil { + logging.LogErrorf("query scan field failed: %s", err) + return + } + if nil == ret[defID] { + ret[defID] = []string{refID} + } else { + ret[defID] = append(ret[defID], refID) + } + } + return +} + func QueryChildDefIDsByRootDefID(rootDefID string) (ret []string) { ret = []string{} rows, err := query("SELECT DISTINCT(def_block_id) FROM refs WHERE def_block_root_id = ?", rootDefID) @@ -334,14 +357,14 @@ func QueryChildDefIDsByRootDefID(rootDefID string) (ret []string) { return } -func QueryRefIDsByDefID(defID string, containChildren bool) (refIDs, refTexts []string) { +func QueryRefIDsByDefID(defID string, containChildren bool) (refIDs []string) { refIDs = []string{} var rows *sql.Rows var err error if containChildren { - rows, err = query("SELECT block_id, content FROM refs WHERE def_block_root_id = ?", defID) + rows, err = query("SELECT block_id FROM refs WHERE def_block_root_id = ?", defID) } else { - rows, err = query("SELECT block_id, content FROM refs WHERE def_block_id = ?", defID) + rows, err = query("SELECT block_id FROM refs WHERE def_block_id = ?", defID) } if err != nil { logging.LogErrorf("sql query failed: %s", err) @@ -349,13 +372,12 @@ func QueryRefIDsByDefID(defID string, containChildren bool) (refIDs, refTexts [] } defer rows.Close() for rows.Next() { - var id, content string - if err = rows.Scan(&id, &content); err != nil { + var id string + if err = rows.Scan(&id); err != nil { logging.LogErrorf("query scan field failed: %s", err) return } refIDs = append(refIDs, id) - refTexts = append(refTexts, content) } return }