This commit is contained in:
Liang Ding 2022-08-06 00:00:23 +08:00
parent 229adee48c
commit e40bc18d86
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
2 changed files with 44 additions and 3 deletions

View file

@ -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

View file

@ -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