瀏覽代碼

:art: `Replace All` is no longer affected by pagination https://github.com/siyuan-note/siyuan/issues/8265

Daniel 2 年之前
父節點
當前提交
ecd4a58d03
共有 2 個文件被更改,包括 36 次插入21 次删除
  1. 28 20
      kernel/api/search.go
  2. 8 1
      kernel/model/search.go

+ 28 - 20
kernel/api/search.go

@@ -35,19 +35,16 @@ func findReplace(c *gin.Context) {
 		return
 	}
 
+	_, _, paths, boxes, types, method, orderBy, groupBy := parseSearchArgs(arg)
+
 	k := arg["k"].(string)
 	r := arg["r"].(string)
-	methodArg := arg["method"]
-	var method int // 0:文本,1:查询语法,2:SQL,3:正则表达式
-	if nil != methodArg {
-		method = int(methodArg.(float64))
-	}
 	idsArg := arg["ids"].([]interface{})
 	var ids []string
 	for _, id := range idsArg {
 		ids = append(ids, id.(string))
 	}
-	err := model.FindReplace(k, r, ids, method)
+	err := model.FindReplace(k, r, ids, paths, boxes, types, method, orderBy, groupBy)
 	if nil != err {
 		ret.Code = -1
 		ret.Msg = err.Error()
@@ -218,7 +215,18 @@ func fullTextSearchBlock(c *gin.Context) {
 		return
 	}
 
-	page := 1
+	page, query, paths, boxes, types, method, orderBy, groupBy := parseSearchArgs(arg)
+	blocks, matchedBlockCount, matchedRootCount, pageCount := model.FullTextSearchBlock(query, boxes, paths, types, method, orderBy, groupBy, page)
+	ret.Data = map[string]interface{}{
+		"blocks":            blocks,
+		"matchedBlockCount": matchedBlockCount,
+		"matchedRootCount":  matchedRootCount,
+		"pageCount":         pageCount,
+	}
+}
+
+func parseSearchArgs(arg map[string]interface{}) (page int, query string, paths, boxes []string, types map[string]bool, method, orderBy, groupBy int) {
+	page = 1
 	if nil != arg["page"] {
 		page = int(arg["page"].(float64))
 	}
@@ -226,9 +234,12 @@ func fullTextSearchBlock(c *gin.Context) {
 		page = 1
 	}
 
-	query := arg["query"].(string)
+	queryArg := arg["query"]
+	if nil != queryArg {
+		query = queryArg.(string)
+	}
+
 	pathsArg := arg["paths"]
-	var paths, boxes []string
 	if nil != pathsArg {
 		for _, p := range pathsArg.([]interface{}) {
 			path := p.(string)
@@ -244,7 +255,7 @@ func fullTextSearchBlock(c *gin.Context) {
 		paths = gulu.Str.RemoveDuplicatedElem(paths)
 		boxes = gulu.Str.RemoveDuplicatedElem(boxes)
 	}
-	var types map[string]bool
+
 	if nil != arg["types"] {
 		typesArg := arg["types"].(map[string]interface{})
 		types = map[string]bool{}
@@ -252,26 +263,23 @@ func fullTextSearchBlock(c *gin.Context) {
 			types[t] = b.(bool)
 		}
 	}
+
+	// method:0:关键字,1:查询语法,2:SQL,3:正则表达式
 	methodArg := arg["method"]
-	var method int // 0:关键字,1:查询语法,2:SQL,3:正则表达式
 	if nil != methodArg {
 		method = int(methodArg.(float64))
 	}
+
+	// orderBy:0:按块类型(默认),1:按创建时间升序,2:按创建时间降序,3:按更新时间升序,4:按更新时间降序,5:按内容顺序(仅在按文档分组时),6:按相关度升序,7:按相关度降序
 	orderByArg := arg["orderBy"]
-	var orderBy int // 0:按块类型(默认),1:按创建时间升序,2:按创建时间降序,3:按更新时间升序,4:按更新时间降序,5:按内容顺序(仅在按文档分组时),6:按相关度升序,7:按相关度降序
 	if nil != orderByArg {
 		orderBy = int(orderByArg.(float64))
 	}
+
+	// groupBy: 0:不分组,1:按文档分组
 	groupByArg := arg["groupBy"]
-	var groupBy int // 0:不分组,1:按文档分组
 	if nil != groupByArg {
 		groupBy = int(groupByArg.(float64))
 	}
-	blocks, matchedBlockCount, matchedRootCount, pageCount := model.FullTextSearchBlock(query, boxes, paths, types, method, orderBy, groupBy, page)
-	ret.Data = map[string]interface{}{
-		"blocks":            blocks,
-		"matchedBlockCount": matchedBlockCount,
-		"matchedRootCount":  matchedRootCount,
-		"pageCount":         pageCount,
-	}
+	return
 }

+ 8 - 1
kernel/model/search.go

@@ -207,7 +207,7 @@ func SearchRefBlock(id, rootID, keyword string, beforeLen int, isSquareBrackets
 	return
 }
 
-func FindReplace(keyword, replacement string, ids []string, method int) (err error) {
+func FindReplace(keyword, replacement string, ids []string, paths, boxes []string, types map[string]bool, method, orderBy, groupBy int) (err error) {
 	// method:0:文本,1:查询语法,2:SQL,3:正则表达式
 	if 1 == method || 2 == method {
 		err = errors.New(Conf.Language(132))
@@ -232,6 +232,13 @@ func FindReplace(keyword, replacement string, ids []string, method int) (err err
 		return
 	}
 
+	if 1 > len(ids) {
+		blocks, _, _, _ := FullTextSearchBlock(keyword, boxes, paths, types, method, orderBy, groupBy, 1)
+		for _, block := range blocks {
+			ids = append(ids, block.ID)
+		}
+	}
+
 	for _, id := range ids {
 		bt := treenode.GetBlockTree(id)
 		if nil == bt {