From e40bc18d86e3f8f20740639173b29ce0dee5c1eb Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sat, 6 Aug 2022 00:00:23 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E8=AE=B0=E5=BD=95=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E6=B5=8F=E8=A7=88=E4=BD=8D=E7=BD=AE=20https://github.com/siyua?= =?UTF-8?q?n-note/siyuan/issues/4042?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/api/filetree.go | 10 +++++++++- kernel/model/file.go | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/kernel/api/filetree.go b/kernel/api/filetree.go index 0b3f8f3ba..cdc2bfc77 100644 --- a/kernel/api/filetree.go +++ b/kernel/api/filetree.go @@ -598,8 +598,16 @@ func getDoc(c *gin.Context) { if nil != s { size = int(s.(float64)) } + startID := "" + endID := "" + startIDArg := arg["startID"] + endIDArg := arg["endID"] + if nil != startIDArg && nil != endIDArg { + startID = startIDArg.(string) + endID = endIDArg.(string) + } - blockCount, content, parentID, parent2ID, rootID, typ, eof, boxID, docPath, err := model.GetDoc(id, index, keyword, mode, size) + blockCount, content, parentID, parent2ID, rootID, typ, eof, boxID, docPath, err := model.GetDoc(startID, endID, id, index, keyword, mode, size) if errors.Is(err, filelock.ErrUnableLockFile) { ret.Code = 2 ret.Data = id diff --git a/kernel/model/file.go b/kernel/model/file.go index ab0d97594..3aaf427f7 100644 --- a/kernel/model/file.go +++ b/kernel/model/file.go @@ -422,7 +422,7 @@ func BlockWordCount(id string) (blockRuneCount, blockWordCount, rootBlockRuneCou return } -func GetDoc(id string, index int, keyword string, mode int, size int) (blockCount 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) (blockCount int, dom, parentID, parent2ID, rootID, typ string, eof bool, boxID, docPath string, err error) { WaitForWritingFiles() // 写入数据时阻塞,避免获取到的数据不一致 inputIndex := index @@ -550,7 +550,18 @@ func GetDoc(id string, index int, keyword string, mode int, size int) (blockCoun typ = node.Type.String() } - nodes, eof := loadNodesByMode(node, inputIndex, mode, size, isDoc, isHeading) + var nodes []*ast.Node + + // 如果同时存在 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) + } refCount := sql.QueryRootChildrenRefCount(rootID) var virtualBlockRefKeywords []string @@ -698,6 +709,28 @@ func GetDoc(id string, index int, keyword string, mode int, size int) (blockCoun return } +func loadNodesByStartEnd(tree *parse.Tree, startID, endID string) (nodes []*ast.Node, eof bool) { + node := treenode.GetNodeInTree(tree, startID) + if nil == node { + return + } + nodes = append(nodes, node) + for n := node.Next; nil != n; n = n.Next { + if n.ID == endID { + next := n.Next + if nil == next { + eof = true + } else { + eof = util2.IsDocIAL(n.Tokens) || util2.IsDocIAL(next.Tokens) + } + break + } + + nodes = append(nodes, n) + } + return +} + func loadNodesByMode(node *ast.Node, inputIndex, mode, size int, isDoc, isHeading bool) (nodes []*ast.Node, eof bool) { if 2 == mode /* 向下 */ { next := node.Next