Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Vanessa 2025-01-12 18:59:11 +08:00
commit ae7eaebf9e
5 changed files with 83 additions and 40 deletions

View file

@ -438,11 +438,9 @@ func getRefIDs(c *gin.Context) {
} }
id := arg["id"].(string) id := arg["id"].(string)
refIDs, refTexts, defIDs, originalRefBlockIDs := model.GetBlockRefs(id, true) refDefs, originalRefBlockIDs := model.GetBlockRefs(id)
ret.Data = map[string]any{ ret.Data = map[string]any{
"refIDs": refIDs, "refDefs": refDefs,
"refTexts": refTexts,
"defIDs": defIDs,
"originalRefBlockIDs": originalRefBlockIDs, "originalRefBlockIDs": originalRefBlockIDs,
} }
} }

View file

@ -359,7 +359,7 @@ func TransferBlockRef(fromID, toID string, refIDs []string) (err error) {
util.PushMsg(Conf.Language(116), 7000) util.PushMsg(Conf.Language(116), 7000)
if 1 > len(refIDs) { // 如果不指定 refIDs则转移所有引用了 fromID 的块 if 1 > len(refIDs) { // 如果不指定 refIDs则转移所有引用了 fromID 的块
refIDs, _ = sql.QueryRefIDsByDefID(fromID, false) refIDs = sql.QueryRefIDsByDefID(fromID, false)
} }
trees := filesys.LoadTrees(refIDs) trees := filesys.LoadTrees(refIDs)

View file

@ -91,9 +91,14 @@ func GetDocInfo(blockID string) (ret *BlockInfo) {
} }
} }
refIDs, _ := sql.QueryRefIDsByDefID(blockID, Conf.Editor.BacklinkContainChildren) refDefs := queryDocRefDefs(blockID)
ret.RefIDs, _ = buildBacklinkListItemRefs(refIDs) buildBacklinkListItemRefs(refDefs)
ret.RefCount = len(ret.RefIDs) // 填充块引计数 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 // 填充属性视图角标 Display the database title on the block superscript https://github.com/siyuan-note/siyuan/issues/10545
avIDs := strings.Split(ret.IAL[av.NodeAttrNameAvs], ",") avIDs := strings.Split(ret.IAL[av.NodeAttrNameAvs], ",")
@ -164,8 +169,8 @@ func GetDocsInfo(blockIDs []string, queryRefCount bool, queryAv bool) (rets []*B
} }
} }
if queryRefCount { if queryRefCount {
ret.RefIDs, _ = sql.QueryRefIDsByDefID(blockID, Conf.Editor.BacklinkContainChildren) ret.RefIDs = sql.QueryRefIDsByDefID(blockID, Conf.Editor.BacklinkContainChildren)
ret.RefCount = len(ret.RefIDs) // 填充块引计数 ret.RefCount = len(ret.RefIDs)
} }
if queryAv { if queryAv {
@ -321,10 +326,13 @@ func getNodeRefText0(node *ast.Node, maxLen int, removeLineBreak bool) string {
return ret return ret
} }
func GetBlockRefs(defID string, isBacklink bool) (refIDs, refTexts, defIDs []string, originalRefIDs map[string]string) { type RefDefs struct {
refIDs = []string{} RefID string `json:"refID"`
refTexts = []string{} DefIDs []string `json:"defIDs"`
defIDs = []string{} }
func GetBlockRefs(defID string) (refDefs []*RefDefs, originalRefIDs map[string]string) {
refDefs = []*RefDefs{}
originalRefIDs = map[string]string{} originalRefIDs = map[string]string{}
bt := treenode.GetBlockTree(defID) bt := treenode.GetBlockTree(defID)
if nil == bt { if nil == bt {
@ -332,15 +340,31 @@ func GetBlockRefs(defID string, isBacklink bool) (refIDs, refTexts, defIDs []str
} }
isDoc := bt.ID == bt.RootID isDoc := bt.ID == bt.RootID
refIDs, refTexts = sql.QueryRefIDsByDefID(defID, isDoc)
if isDoc { if isDoc {
defIDs = sql.QueryChildDefIDsByRootDefID(defID) refDefs = queryDocRefDefs(defID)
} else { } 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 { originalRefIDs = buildBacklinkListItemRefs(refDefs)
refIDs, originalRefIDs = buildBacklinkListItemRefs(refIDs) 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 return
} }
@ -559,10 +583,13 @@ func buildBlockBreadcrumb(node *ast.Node, excludeTypes []string, isEmbedBlock bo
return return
} }
func buildBacklinkListItemRefs(refIDs []string) (retRefIDs []string, originalRefBlockIDs map[string]string) { func buildBacklinkListItemRefs(refDefs []*RefDefs) (originalRefBlockIDs map[string]string) {
retRefIDs = []string{}
originalRefBlockIDs = map[string]string{} originalRefBlockIDs = map[string]string{}
var refIDs []string
for _, refDef := range refDefs {
refIDs = append(refIDs, refDef.RefID)
}
sqlRefBlocks := sql.GetBlocks(refIDs) sqlRefBlocks := sql.GetBlocks(refIDs)
refBlocks := fromSQLBlocks(&sqlRefBlocks, "", 12) refBlocks := fromSQLBlocks(&sqlRefBlocks, "", 12)
@ -603,21 +630,17 @@ func buildBacklinkListItemRefs(refIDs []string) (retRefIDs []string, originalRef
} }
if paragraphUseParentLi { 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) processedParagraphs.Add(parent.ID)
} else {
retRefIDs = append(retRefIDs, refBlock.ID)
} }
originalRefBlockIDs[parent.ID] = 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 return
} }

View file

@ -128,7 +128,7 @@ func refreshProtyle(rootID string) {
} }
// 刷新关联的嵌入块 // 刷新关联的嵌入块
refIDs, _ := sql.QueryRefIDsByDefID(rootID, true) refIDs := sql.QueryRefIDsByDefID(rootID, true)
var rootIDs []string var rootIDs []string
bts := treenode.GetBlockTrees(refIDs) bts := treenode.GetBlockTrees(refIDs)
for _, bt := range bts { for _, bt := range bts {
@ -154,11 +154,11 @@ func refreshRefCount(rootID, blockID string) {
isDoc := bt.ID == bt.RootID isDoc := bt.ID == bt.RootID
var rootRefIDs []string var rootRefIDs []string
var refCount, rootRefCount int var refCount, rootRefCount int
refIDs, _ := sql.QueryRefIDsByDefID(bt.ID, isDoc) refIDs := sql.QueryRefIDsByDefID(bt.ID, isDoc)
if isDoc { if isDoc {
rootRefIDs = refIDs rootRefIDs = refIDs
} else { } else {
rootRefIDs, _ = sql.QueryRefIDsByDefID(bt.RootID, true) rootRefIDs = sql.QueryRefIDsByDefID(bt.RootID, true)
} }
refCount = len(refIDs) refCount = len(refIDs)
rootRefCount = len(rootRefIDs) rootRefCount = len(rootRefIDs)

View file

@ -315,6 +315,29 @@ func queryDefIDsByNameAlias(keyword string, excludeIDs []string) (ret []string)
return 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) { func QueryChildDefIDsByRootDefID(rootDefID string) (ret []string) {
ret = []string{} ret = []string{}
rows, err := query("SELECT DISTINCT(def_block_id) FROM refs WHERE def_block_root_id = ?", rootDefID) 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 return
} }
func QueryRefIDsByDefID(defID string, containChildren bool) (refIDs, refTexts []string) { func QueryRefIDsByDefID(defID string, containChildren bool) (refIDs []string) {
refIDs = []string{} refIDs = []string{}
var rows *sql.Rows var rows *sql.Rows
var err error var err error
if containChildren { 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 { } 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 { if err != nil {
logging.LogErrorf("sql query failed: %s", err) logging.LogErrorf("sql query failed: %s", err)
@ -349,13 +372,12 @@ func QueryRefIDsByDefID(defID string, containChildren bool) (refIDs, refTexts []
} }
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
var id, content string var id string
if err = rows.Scan(&id, &content); err != nil { if err = rows.Scan(&id); err != nil {
logging.LogErrorf("query scan field failed: %s", err) logging.LogErrorf("query scan field failed: %s", err)
return return
} }
refIDs = append(refIDs, id) refIDs = append(refIDs, id)
refTexts = append(refTexts, content)
} }
return return
} }