Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Fix mobile search results in replace mode https://github.com/siyuan-note/siyuan/issues/19442

Daniel005a9343f497
diff --git a/app/src/mobile/menu/search.ts b/app/src/mobile/menu/search.ts
index f6ee20a9ac..f7150289c9 100644
--- a/app/src/mobile/menu/search.ts
+++ b/app/src/mobile/menu/search.ts
@@ -1,5 +1,6 @@
import {closePanel} from "../util/closePanel";
-import type {APICallbackResponse, APIPOSTRoutes, FullTextSearchBlockRequestInput} from "../../types/api";
+import {buildSearchRequest} from "../../search/config";
+import type {APICallbackResponse, APIPOSTRoutes} from "../../types/api";
import {getCurrentEditor, openMobileFileById} from "../editor";
import {Constants} from "../../constants";
import {fetchPost} from "../../util/fetch";
@@ -316,17 +317,7 @@ export const updateSearchResult = (config: Config.IUILayoutTabSearchConfig, elem
previousElement.setAttribute("disabled", "disabled");
}
const endpoint = requestConfig.method === 4 ? "/api/search/semanticSearchBlock" : "/api/search/fullTextSearchBlock";
- const searchParam: FullTextSearchBlockRequestInput = {
- query: requestConfig.query,
- method: requestConfig.method,
- types: {...requestConfig.types},
- subTypes: requestConfig.subTypes,
- paths: requestConfig.idPath || [],
- groupBy: requestConfig.group,
- orderBy: requestConfig.sort,
- page: requestConfig.page,
- pageSize: 32,
- };
+ const searchParam = buildSearchRequest(requestConfig);
// 限定在单个加密 box 内搜索时带 notebook,让内核走加密 db;跨 box 或全局搜索走原函数
const idPaths = requestConfig.idPath || [];
if (idPaths.length > 0) {
diff --git a/app/src/search/config.test.ts b/app/src/search/config.test.ts
index 47fcb732a9..e33515bda6 100644
--- a/app/src/search/config.test.ts
+++ b/app/src/search/config.test.ts
@@ -1,6 +1,7 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
import {
+ buildSearchRequest,
cloneSearchConfig,
getSearchPathID,
getGlobalSearchPath,
@@ -25,6 +26,62 @@ import {
syncSearchConfigHPath,
} from "./config";
+describe("search request configuration", () => {
+ it("disables hierarchical path matches only while replacing", () => {
+ const config: Config.IUILayoutTabSearchConfig = {query: "ancestor", method: 0};
+
+ assert.equal(buildSearchRequest(config).searchHPath, true);
+ config.hasReplace = true;
+ assert.equal(buildSearchRequest(config).searchHPath, false);
+ config.hasReplace = false;
+ assert.equal(buildSearchRequest(config).searchHPath, true);
+ });
+
+ it("preserves filters and pagination when replacing", () => {
+ const config: Config.IUILayoutTabSearchConfig = {
+ query: "ancestor",
+ method: 0,
+ types: {
+ document: true,
+ audioBlock: false,
+ videoBlock: false,
+ iframeBlock: false,
+ widgetBlock: false,
+ heading: false,
+ list: false,
+ listItem: false,
+ codeBlock: false,
+ htmlBlock: false,
+ mathBlock: false,
+ table: false,
+ blockquote: false,
+ callout: false,
+ tabs: false,
+ tabItem: false,
+ superBlock: false,
+ paragraph: false,
+ embedBlock: false,
+ databaseBlock: false,
+ },
+ idPath: ["notebook/document"],
+ group: 1,
+ sort: 2,
+ page: 3,
+ };
+ const search = buildSearchRequest(config);
+ assert.deepEqual(buildSearchRequest({...config, hasReplace: true}), {...search, searchHPath: false});
+ assert.equal(search.query, "ancestor");
+ assert.deepEqual(search.paths, config.idPath);
+ assert.deepEqual(search.types, config.types);
+ assert.equal(search.groupBy, 1);
+ assert.equal(search.orderBy, 2);
+ assert.equal(search.page, 3);
+ assert.equal(search.pageSize, 32);
+ assert.equal(buildSearchRequest({}).page, 1);
+ assert.deepEqual(buildSearchRequest({}).paths, []);
+ });
+});
+
describe("search configuration scope", () => {
const criterionA: Config.IUILayoutTabSearchConfig = {
removed: false,
diff --git a/app/src/search/config.ts b/app/src/search/config.ts
index 230b2c6aa2..fb07cb0971 100644
--- a/app/src/search/config.ts
+++ b/app/src/search/config.ts
@@ -1,3 +1,20 @@
+import type {FullTextSearchBlockRequestInput} from "../types/api";
+
+export const buildSearchRequest = (config: Config.IUILayoutTabSearchConfig): FullTextSearchBlockRequestInput => {
+ return {
+ query: config.query,
+ method: config.method,
+ types: {...config.types},
+ subTypes: config.subTypes,
+ paths: config.idPath || [],
+ groupBy: config.group,
+ orderBy: config.sort,
+ page: config.page || 1,
+ pageSize: 32,
+ searchHPath: !config.hasReplace,
+ };
+};
+
const temporaryPathConfigs = new WeakSet<Config.IUILayoutTabSearchConfig>();
const searchPathRequestVersions = new WeakMap<object, number>();
diff --git a/app/src/search/util.ts b/app/src/search/util.ts
index 4aba089650..128d39617f 100644
--- a/app/src/search/util.ts
+++ b/app/src/search/util.ts
@@ -1,4 +1,4 @@
-import type {APICallbackResponse, APIPOSTRoutes, BlockQueryRequestInput, FullTextSearchBlockRequestInput} from "../types/api";
+import type {APICallbackResponse, APIPOSTRoutes, BlockQueryRequestInput} from "../types/api";
import {getAllModels} from "../layout/getAll";
/// #if !BROWSER
import * as path from "path";
@@ -56,6 +56,7 @@ import {getContenteditableElement} from "../protyle/wysiwyg/getBlock";
import {IDatabaseItemOpenData, openDatabaseItem} from "../protyle/render/av/openDatabaseItem";
import {scheduleSearchRequest} from "./request";
import {
+ buildSearchRequest,
cloneSearchConfig,
hasSearchConfigTemporaryPath,
resolvePersistedSearchConfig,
@@ -1507,18 +1508,7 @@ export const inputEvent = (element: Element, config: Config.IUILayoutTabSearchCo
previousElement.setAttribute("disabled", "disabled");
}
const endpoint = requestConfig.method === 4 ? "/api/search/semanticSearchBlock" : "/api/search/fullTextSearchBlock";
- const searchParam: FullTextSearchBlockRequestInput = {
- query: requestConfig.query,
- method: requestConfig.method,
- types: {...requestConfig.types},
- subTypes: requestConfig.subTypes,
- paths: requestConfig.idPath || [],
- groupBy: requestConfig.group,
- orderBy: requestConfig.sort,
- page: requestConfig.page || 1,
- pageSize: 32,
- searchHPath: !requestConfig.hasReplace,
- };
+ const searchParam = buildSearchRequest(requestConfig);
// 限定在单个加密 box 内搜索时带 notebook,让内核走加密 db;跨 box 或全局搜索走原函数
const idPaths = requestConfig.idPath || [];
if (idPaths.length > 0) {