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

This commit is contained in:
Liang Ding 2022-12-24 12:05:55 +08:00
parent a20a338e6d
commit 2eb3e8ffc7
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
3 changed files with 91 additions and 70 deletions

View file

@ -705,8 +705,13 @@ func getDoc(c *gin.Context) {
endID = endIDArg.(string) endID = endIDArg.(string)
size = 36 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) { if errors.Is(err, filelock.ErrUnableAccessFile) {
ret.Code = 2 ret.Code = 2
ret.Data = id ret.Data = id
@ -727,19 +732,20 @@ func getDoc(c *gin.Context) {
isSyncing := model.IsSyncingFile(rootID) isSyncing := model.IsSyncingFile(rootID)
ret.Data = map[string]interface{}{ ret.Data = map[string]interface{}{
"id": id, "id": id,
"mode": mode, "mode": mode,
"parentID": parentID, "parentID": parentID,
"parent2ID": parent2ID, "parent2ID": parent2ID,
"rootID": rootID, "rootID": rootID,
"type": typ, "type": typ,
"content": content, "content": content,
"blockCount": blockCount, "blockCount": blockCount,
"childBlockCount": childBlockCount, "childBlockCount": childBlockCount,
"eof": eof, "eof": eof,
"box": boxID, "box": boxID,
"path": docPath, "path": docPath,
"isSyncing": isSyncing, "isSyncing": isSyncing,
"isBacklinkExpand": isBacklinkExpand,
} }
} }

View file

@ -147,53 +147,7 @@ func buildBacklink(refID string, refTree *parse.Tree, mentionKeywords []string,
return return
} }
var renderNodes []*ast.Node renderNodes, expand := getBacklinkRenderNodes(n)
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)
}
if 0 < len(mentionKeywords) { if 0 < len(mentionKeywords) {
for _, renderNode := range renderNodes { for _, renderNode := range renderNodes {
@ -241,6 +195,56 @@ func buildBacklink(refID string, refTree *parse.Tree, mentionKeywords []string,
return 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) { func GetBacklink2(id, keyword, mentionKeyword string, sortMode, mentionSortMode int) (boxID string, backlinks, backmentions []*Path, linkRefsCount, mentionsCount int) {
keyword = strings.TrimSpace(keyword) keyword = strings.TrimSpace(keyword)
mentionKeyword = strings.TrimSpace(mentionKeyword) mentionKeyword = strings.TrimSpace(mentionKeyword)

View file

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