Browse Source

:art: 记录文档浏览位置 https://github.com/siyuan-note/siyuan/issues/4042

Liang Ding 3 years ago
parent
commit
e40bc18d86
2 changed files with 44 additions and 3 deletions
  1. 9 1
      kernel/api/filetree.go
  2. 35 2
      kernel/model/file.go

+ 9 - 1
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

+ 35 - 2
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