瀏覽代碼

:art: 改进块引计数浮窗显示逻辑 https://github.com/siyuan-note/siyuan/issues/6853

Liang Ding 2 年之前
父節點
當前提交
2eb3e8ffc7
共有 3 個文件被更改,包括 91 次插入70 次删除
  1. 20 14
      kernel/api/filetree.go
  2. 51 47
      kernel/model/backlink.go
  3. 20 9
      kernel/model/file.go

+ 20 - 14
kernel/api/filetree.go

@@ -705,8 +705,13 @@ func getDoc(c *gin.Context) {
 		endID = endIDArg.(string)
 		size = 36
 	}
+	isBacklinkArg := arg["isBacklink"]
+	isBacklink := false
+	if nil != isBacklinkArg {
+		isBacklink = isBacklinkArg.(bool)
+	}
 
-	blockCount, childBlockCount, content, parentID, parent2ID, rootID, typ, eof, boxID, docPath, err := model.GetDoc(startID, endID, id, index, keyword, mode, size)
+	blockCount, childBlockCount, content, parentID, parent2ID, rootID, typ, eof, boxID, docPath, isBacklinkExpand, err := model.GetDoc(startID, endID, id, index, keyword, mode, size, isBacklink)
 	if errors.Is(err, filelock.ErrUnableAccessFile) {
 		ret.Code = 2
 		ret.Data = id
@@ -727,19 +732,20 @@ func getDoc(c *gin.Context) {
 	isSyncing := model.IsSyncingFile(rootID)
 
 	ret.Data = map[string]interface{}{
-		"id":              id,
-		"mode":            mode,
-		"parentID":        parentID,
-		"parent2ID":       parent2ID,
-		"rootID":          rootID,
-		"type":            typ,
-		"content":         content,
-		"blockCount":      blockCount,
-		"childBlockCount": childBlockCount,
-		"eof":             eof,
-		"box":             boxID,
-		"path":            docPath,
-		"isSyncing":       isSyncing,
+		"id":               id,
+		"mode":             mode,
+		"parentID":         parentID,
+		"parent2ID":        parent2ID,
+		"rootID":           rootID,
+		"type":             typ,
+		"content":          content,
+		"blockCount":       blockCount,
+		"childBlockCount":  childBlockCount,
+		"eof":              eof,
+		"box":              boxID,
+		"path":             docPath,
+		"isSyncing":        isSyncing,
+		"isBacklinkExpand": isBacklinkExpand,
 	}
 }
 

+ 51 - 47
kernel/model/backlink.go

@@ -147,53 +147,7 @@ func buildBacklink(refID string, refTree *parse.Tree, mentionKeywords []string,
 		return
 	}
 
-	var renderNodes []*ast.Node
-	expand := true
-	if ast.NodeListItem == n.Type {
-		if nil == n.FirstChild {
-			return
-		}
-
-		c := n.FirstChild
-		if 3 == n.ListData.Typ {
-			c = n.FirstChild.Next
-		}
-
-		if c != n.LastChild { // 存在子列表
-			for liFirstBlockSpan := c.FirstChild; nil != liFirstBlockSpan; liFirstBlockSpan = liFirstBlockSpan.Next {
-				if treenode.IsBlockRef(liFirstBlockSpan) {
-					continue
-				}
-				if "" != strings.TrimSpace(liFirstBlockSpan.Text()) {
-					expand = false
-					break
-				}
-			}
-		}
-
-		renderNodes = append(renderNodes, n)
-	} else if ast.NodeHeading == n.Type {
-		c := n.FirstChild
-		if nil == c {
-			return
-		}
-
-		for headingFirstSpan := c; nil != headingFirstSpan; headingFirstSpan = headingFirstSpan.Next {
-			if treenode.IsBlockRef(headingFirstSpan) {
-				continue
-			}
-			if "" != strings.TrimSpace(headingFirstSpan.Text()) {
-				expand = false
-				break
-			}
-		}
-
-		renderNodes = append(renderNodes, n)
-		cc := treenode.HeadingChildren(n)
-		renderNodes = append(renderNodes, cc...)
-	} else {
-		renderNodes = append(renderNodes, n)
-	}
+	renderNodes, expand := getBacklinkRenderNodes(n)
 
 	if 0 < len(mentionKeywords) {
 		for _, renderNode := range renderNodes {
@@ -241,6 +195,56 @@ func buildBacklink(refID string, refTree *parse.Tree, mentionKeywords []string,
 	return
 }
 
+func getBacklinkRenderNodes(n *ast.Node) (ret []*ast.Node, expand bool) {
+	expand = true
+	if ast.NodeListItem == n.Type {
+		if nil == n.FirstChild {
+			return
+		}
+
+		c := n.FirstChild
+		if 3 == n.ListData.Typ {
+			c = n.FirstChild.Next
+		}
+
+		if c != n.LastChild { // 存在子列表
+			for liFirstBlockSpan := c.FirstChild; nil != liFirstBlockSpan; liFirstBlockSpan = liFirstBlockSpan.Next {
+				if treenode.IsBlockRef(liFirstBlockSpan) {
+					continue
+				}
+				if "" != strings.TrimSpace(liFirstBlockSpan.Text()) {
+					expand = false
+					break
+				}
+			}
+		}
+
+		ret = append(ret, n)
+	} else if ast.NodeHeading == n.Type {
+		c := n.FirstChild
+		if nil == c {
+			return
+		}
+
+		for headingFirstSpan := c; nil != headingFirstSpan; headingFirstSpan = headingFirstSpan.Next {
+			if treenode.IsBlockRef(headingFirstSpan) {
+				continue
+			}
+			if "" != strings.TrimSpace(headingFirstSpan.Text()) {
+				expand = false
+				break
+			}
+		}
+
+		ret = append(ret, n)
+		cc := treenode.HeadingChildren(n)
+		ret = append(ret, cc...)
+	} else {
+		ret = append(ret, n)
+	}
+	return
+}
+
 func GetBacklink2(id, keyword, mentionKeyword string, sortMode, mentionSortMode int) (boxID string, backlinks, backmentions []*Path, linkRefsCount, mentionsCount int) {
 	keyword = strings.TrimSpace(keyword)
 	mentionKeyword = strings.TrimSpace(mentionKeyword)

+ 20 - 9
kernel/model/file.go

@@ -452,7 +452,7 @@ func getMarkSpanEnd() string {
 	return "</span>"
 }
 
-func GetDoc(startID, endID, id string, index int, keyword string, mode int, size int) (blockCount, childBlockCount int, dom, parentID, parent2ID, rootID, typ string, eof bool, boxID, docPath string, err error) {
+func GetDoc(startID, endID, id string, index int, keyword string, mode int, size int, isBacklink bool) (blockCount, childBlockCount int, dom, parentID, parent2ID, rootID, typ string, eof bool, boxID, docPath string, isBacklinkExpand bool, err error) {
 	WaitForWritingFiles() // 写入数据时阻塞,避免获取到的数据不一致
 
 	inputIndex := index
@@ -477,9 +477,16 @@ func GetDoc(startID, endID, id string, index int, keyword string, mode int, size
 		return
 	}
 
+	if isBacklink { // 引用计数浮窗请求,需要按照反链逻辑组装 https://github.com/siyuan-note/siyuan/issues/6853
+		if ast.NodeParagraph == node.Type {
+			node = node.Parent
+		}
+	}
+
 	located := false
 	isDoc := ast.NodeDocument == node.Type
 	isHeading := ast.NodeHeading == node.Type
+
 	boxID = node.Box
 	docPath = node.Path
 	if isDoc {
@@ -582,16 +589,20 @@ func GetDoc(startID, endID, id string, index int, keyword string, mode int, size
 	}
 
 	var nodes []*ast.Node
-
-	// 如果同时存在 startID 和 endID,则只加载 startID 和 endID 之间的块 [startID, endID]
-	if "" != startID && "" != endID {
-		nodes, eof = loadNodesByStartEnd(tree, startID, endID)
-		if 1 > len(nodes) {
-			// 按 mode 加载兜底
+	if isBacklink {
+		// 引用计数浮窗请求,需要按照反链逻辑组装 https://github.com/siyuan-note/siyuan/issues/6853
+		nodes, isBacklinkExpand = getBacklinkRenderNodes(node)
+	} else {
+		// 如果同时存在 startID 和 endID,则只加载 startID 和 endID 之间的块 [startID, endID]
+		if "" != startID && "" != endID {
+			nodes, eof = loadNodesByStartEnd(tree, startID, endID)
+			if 1 > len(nodes) {
+				// 按 mode 加载兜底
+				nodes, eof = loadNodesByMode(node, inputIndex, mode, size, isDoc, isHeading)
+			}
+		} else {
 			nodes, eof = loadNodesByMode(node, inputIndex, mode, size, isDoc, isHeading)
 		}
-	} else {
-		nodes, eof = loadNodesByMode(node, inputIndex, mode, size, isDoc, isHeading)
 	}
 
 	refCount := sql.QueryRootChildrenRefCount(rootID)