An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
Merge remote-tracking branch 'origin/dev' into dev
Vanessa3d8b78d36c8d
diff --cc app/src/layout/tabUtil.tsindex a3328b97c2,a3328b97c2..f480e56904--- a/app/src/layout/tabUtil.ts+++ b/app/src/layout/tabUtil.ts@@@ -26,6 -26,6 +26,7 @@@ import {fetchPost} from "../util/fetch"import {isWindow} from "../util/functions";import {Wnd} from "./Wnd";import {requestResponsiveDockLayout} from "./dock/responsive";++import {cloneSearchConfig} from "../search/config";export const setTabPosition = (onlyPadding = false, onlyClear = false) => {const isWindowMode = isWindow();@@@ -420,7 -420,7 +421,7 @@@ export const copyTab = (app: App, tab:model = new Search({app,tab: newTab,-- config: tab.model.config++ config: cloneSearchConfig(tab.model.config)});} else if (tab.model instanceof Custom) {const custom = tab.model as Custom;diff --cc app/src/protyle/index.tsindex 82b137c9cb,82b137c9cb..5e9b468051--- a/app/src/protyle/index.ts+++ b/app/src/protyle/index.ts@@@ -46,6 -46,6 +46,12 @@@ import {setStorageVal} from "./util/comimport {merge} from "./util/merge";/// #if !MOBILEimport {getAllModels} from "../layout/getAll";++import {++ invalidateSearchPathRequests,++ refreshSearchPathAfterNotebookRename,++ refreshSearchPathAfterRename,++} from "../search/path";++import {syncSearchConfigHPath} from "../search/config";/// #endifimport {isSupportCSSHL} from "./render/searchMarkRender";import {renderAVAttribute} from "./render/av/blockAttr";@@@ -60,6 -60,6 +66,31 @@@ import} from "./render/av/databaseRowRefresh";import {registerCustomBlockRoot} from "../plugin/customBlockRender";++/// #if !MOBILE++const forSearchByEditor = (edit: Protyle, callback: (config: Config.IUILayoutTabSearchConfig, element: Element) => void) => {++ window.siyuan.dialogs.find((item) => {++ const searchElement = item.element.querySelector(".b3-dialog__body");++ if (item.editors?.edit === edit && item.data && searchElement) {++ callback(item.data, searchElement);++ return true;++ }++ });++ getAllModels().search.find((item) => {++ if (item.editors.edit === edit) {++ callback(item.config, item.element);++ return true;++ }++ });++};++++const persistRefreshedSearchPath = (config: Config.IUILayoutTabSearchConfig) => {++ const localConfig = window.siyuan.storage[Constants.LOCAL_SEARCHDATA];++ if (syncSearchConfigHPath(localConfig, config)) {++ setStorageVal(Constants.LOCAL_SEARCHDATA, localConfig);++ }++};++/// #endif++export class Protyle {public readonly version: string;@@@ -232,6 -232,6 +263,22 @@@/// #endif}break;++ case "renamenotebook":++ /// #if !MOBILE++ forSearchByEditor(this, (config, element) => {++ void refreshSearchPathAfterNotebookRename({++ config,++ element,++ notebookId: data.data.box,++ notebookName: data.data.name,++ }).then((refreshed) => {++ if (refreshed) {++ persistRefreshedSearchPath(config);++ }++ });++ });++ /// #endif++ break;case "rename":if (this.protyle.path === data.data.path) {if (this.protyle.model) {@@@ -265,6 -265,6 +312,19 @@@item.innerHTML = data.data.refText;}});++ /// #if !MOBILE++ forSearchByEditor(this, (config, element) => {++ void refreshSearchPathAfterRename({++ config,++ element,++ rename: data.data,++ }).then((refreshed) => {++ if (refreshed) {++ persistRefreshedSearchPath(config);++ }++ });++ });++ /// #endifbreak;case "moveDoc":if (this.protyle.path === data.data.fromPath) {@@@ -276,6 -276,6 +336,11 @@@this.protyle.element.removeAttribute("data-notebook-id");}}++ /// #if !MOBILE++ forSearchByEditor(this, (_config, element) => {++ invalidateSearchPathRequests(element);++ });++ /// #endifbreak;case "closeBox":case "removeBox":diff --cc app/src/search/config.test.tsindex 562a528d42,dc39ce4649..47fcb732a9--- a/app/src/search/config.test.ts+++ b/app/src/search/config.test.ts@@@ -1,340 -1,83 +1,391 @@@import {describe, it} from "node:test";import * as assert from "node:assert/strict";-import {resolveGlobalSearchScope, resolveSearchConfigUpdate} from "./config";+import {+ cloneSearchConfig,+ getSearchPathID,+ getGlobalSearchPath,+ hasExplicitSearchScope,+ hasSearchConfigTemporaryPath,+ isSameSearchPath,+ isCurrentSearchPath,+ isSearchPathRequestVersionCurrent,+ isSearchPathAffectedByNotebookRename,+ isSearchPathAffectedByRename,+ nextSearchPathRequestVersion,+ refreshSearchConfigHPath,+ replaceSearchConfigPath,+ resolvePersistedSearchConfig,++ resolveGlobalSearchScope,+ resolveSearchHPath,+ resolveSearchNotebookHPath,+ resolveSearchConfig,++ resolveSearchConfigUpdate,+ setSearchConfigTemporaryPath,+ syncSearchConfig,+ syncSearchConfigHPath,+} from "./config";-describe("search configuration", () => {- it("uses the saved criterion path for global search", () => {- const config: Config.IUILayoutTabSearchConfig = {- removed: false,- hPath: "Notebook/Document A",- idPath: ["notebook/document-a"],- };+describe("search configuration scope", () => {+ const criterionA: Config.IUILayoutTabSearchConfig = {+ removed: false,+ name: "criterion A",+ hPath: "Notebook/Document A",+ idPath: ["notebook/document-a"],+ k: "keyword",+ method: 0,+ };+ const documentB: Config.IUILayoutTabSearchConfig = {+ removed: false,+ hPath: "Notebook/Document B",+ idPath: ["notebook/document-b"],+ k: "keyword",+ method: 0,+ };- const scope = resolveGlobalSearchScope(config);+ it("keeps the current document effective without changing the saved criterion", () => {+ const result = resolveSearchConfig(criterionA, documentB, true);- assert.deepEqual(scope, {- hPath: "Notebook/Document A",- idPath: ["notebook/document-a"],- });- assert.notStrictEqual(scope.idPath, config.idPath);+ assert.equal(result.effectiveConfig.hPath, documentB.hPath);+ assert.deepEqual(result.effectiveConfig.idPath, documentB.idPath);+ assert.equal(result.persistedConfig.hPath, criterionA.hPath);+ assert.deepEqual(result.persistedConfig.idPath, criterionA.idPath);+ assert.equal(criterionA.hPath, "Notebook/Document A");+ assert.deepEqual(criterionA.idPath, ["notebook/document-a"]);++ result.effectiveConfig.idPath.push("notebook/document-b-child");+ assert.deepEqual(documentB.idPath, ["notebook/document-b"]);+ assert.deepEqual(result.persistedConfig.idPath, ["notebook/document-a"]);});- it("uses an empty path for global search without a criterion", () => {- const scope = resolveGlobalSearchScope({+ it("restores the saved criterion path for global search", () => {+ const {persistedConfig} = resolveSearchConfig(criterionA, documentB, true);- const globalPath = getGlobalSearchPath(persistedConfig);++ const globalPath = resolveGlobalSearchScope(persistedConfig);++ assert.equal(globalPath.hPath, criterionA.hPath);+ assert.deepEqual(globalPath.idPath, criterionA.idPath);+ assert.notEqual(globalPath.idPath, persistedConfig.idPath);+ });++ it("clears the global path after the criterion is removed", () => {+ const globalPath = getGlobalSearchPath({removed: true,- hPath: "Notebook/Document B",- idPath: ["notebook/document-b"],+ hPath: documentB.hPath,+ idPath: [...documentB.idPath],});- assert.deepEqual(scope, {+ assert.deepEqual(globalPath, {hPath: "",idPath: [],});});+ it("keeps the persisted path when a shortcut changes the effective scope", () => {+ const effectiveDocumentSearch = replaceSearchConfigPath(criterionA, documentB);+ const persistedDocumentSearch = replaceSearchConfigPath(effectiveDocumentSearch, criterionA);+ const globalPath = getGlobalSearchPath(persistedDocumentSearch);++ assert.equal(effectiveDocumentSearch.hPath, documentB.hPath);+ assert.deepEqual(effectiveDocumentSearch.idPath, documentB.idPath);+ assert.equal(persistedDocumentSearch.hPath, criterionA.hPath);+ assert.deepEqual(globalPath.idPath, criterionA.idPath);+ });++ it("does not persist temporary document paths after changing other options", () => {+ const activeCriterionConfig = resolvePersistedSearchConfig(documentB, criterionA, true);+ const removedCriterionConfig = resolvePersistedSearchConfig({+ ...documentB,+ removed: true,+ }, criterionA, true);++ assert.equal(activeCriterionConfig.hPath, criterionA.hPath);+ assert.deepEqual(activeCriterionConfig.idPath, criterionA.idPath);+ assert.equal(removedCriterionConfig.hPath, "");+ assert.deepEqual(removedCriterionConfig.idPath, []);+ });++ it("treats explicitly scoped replace paths as temporary", () => {+ assert.equal(hasExplicitSearchScope({}), false);+ assert.equal(hasExplicitSearchScope({notebookIds: []}), false);+ assert.equal(hasExplicitSearchScope({notebookId: "notebook"}), true);+ assert.equal(hasExplicitSearchScope({notebookIds: ["notebook-a", "notebook-b"]}), true);++ const scopedReplace = {+ ...documentB,+ hasReplace: true,+ };+ const replaceCriterion = {+ ...criterionA,+ hasReplace: true,+ replaceTypes: {text: true},+ };+ setSearchConfigTemporaryPath(scopedReplace, hasExplicitSearchScope({notebookId: "notebook"}));++ const appliedCriterion = resolveSearchConfig(+ replaceCriterion,+ scopedReplace,+ hasSearchConfigTemporaryPath(scopedReplace),+ );+ syncSearchConfig(scopedReplace, appliedCriterion.effectiveConfig);+ scopedReplace.method = 3;+ scopedReplace.replaceTypes = {text: false};+ const pinnedSearchConfig = cloneSearchConfig(scopedReplace);+ const persistedConfig = resolvePersistedSearchConfig(+ scopedReplace,+ appliedCriterion.persistedConfig,+ hasSearchConfigTemporaryPath(scopedReplace),+ );++ assert.equal(scopedReplace.hPath, documentB.hPath);+ assert.deepEqual(scopedReplace.idPath, documentB.idPath);+ assert.equal(persistedConfig.hPath, criterionA.hPath);+ assert.deepEqual(persistedConfig.idPath, criterionA.idPath);+ assert.equal(persistedConfig.method, 3);+ assert.deepEqual(persistedConfig.replaceTypes, {text: false});+ assert.equal(hasSearchConfigTemporaryPath(pinnedSearchConfig), false);+ });++ it("detects renamed documents that affect the current search path", () => {+ assert.equal(isSearchPathAffectedByRename(+ ["notebook/parent/document"], "notebook", "/parent/document.sy", "document"), true);+ assert.equal(isSearchPathAffectedByRename(+ ["notebook/parent/document.sy"], "notebook", "/parent.sy", "parent"), true);+ assert.equal(isSearchPathAffectedByRename(+ ["document"], "notebook", "/parent/document.sy", "document"), true);+ assert.equal(isSearchPathAffectedByRename(+ ["notebook/other"], "notebook", "/parent/document.sy", "document"), false);+ assert.equal(isSearchPathAffectedByRename(+ ["notebook/parent"], "notebook", "/parent/child.sy", "child"), false);+ assert.equal(isSearchPathAffectedByRename(+ ["notebook/parent2"], "notebook", "/parent.sy", "parent"), false);+ assert.equal(isSearchPathAffectedByRename(+ ["notebook/parent/document"], "notebook", "/parent/document.sy", undefined), true);+ assert.equal(isSearchPathAffectedByRename(+ ["document"], "notebook", "/parent/document.sy", undefined), true);+ });++ it("detects notebook renames for notebook and document scopes", () => {+ assert.equal(isSearchPathAffectedByNotebookRename(["notebook"], "notebook"), true);+ assert.equal(isSearchPathAffectedByNotebookRename(["notebook/parent/document"], "notebook"), true);+ assert.equal(isSearchPathAffectedByNotebookRename(["notebook2/document"], "notebook"), false);+ });++ it("resolves notebook scopes without requiring a document block", () => {+ const notebooks = [{id: "notebook", name: "Old Notebook"}];++ assert.equal(resolveSearchNotebookHPath("notebook", notebooks), "Old Notebook");+ assert.equal(resolveSearchNotebookHPath("notebook", notebooks, {notebook: "Renamed Notebook"}),+ "Renamed Notebook");+ assert.equal(resolveSearchNotebookHPath("document", notebooks), undefined);+ });++ it("validates that a stored search path has not moved", () => {+ assert.equal(getSearchPathID("notebook/parent/document.sy"), "document");+ assert.equal(isCurrentSearchPath(+ "notebook/parent/document", "notebook", "/parent/document.sy"), true);+ assert.equal(isCurrentSearchPath(+ "notebook/parent/document.sy", "notebook", "/parent/document.sy"), true);+ assert.equal(isCurrentSearchPath(+ "notebook/parent/document", "notebook", "/moved/document.sy"), false);+ assert.equal(isCurrentSearchPath(+ "notebook/parent/document", "other-notebook", "/parent/document.sy"), false);+ assert.equal(isCurrentSearchPath("notebook", "notebook", "/notebook.sy"), true);+ assert.equal(isCurrentSearchPath("document", "notebook", "/parent/document.sy"), true);+ });++ it("updates only a persisted human-readable path for the same stable scope", () => {+ const persistedCriterion = cloneSearchConfig(criterionA);+ const renamedCriterion = {+ ...cloneSearchConfig(criterionA),+ hPath: "Notebook/Renamed Document A",+ };+ assert.equal(syncSearchConfigHPath(persistedCriterion, renamedCriterion), true);+ assert.equal(persistedCriterion.hPath, renamedCriterion.hPath);++ const persistedOtherScope = cloneSearchConfig(criterionA);+ assert.equal(syncSearchConfigHPath(persistedOtherScope, documentB), false);+ assert.equal(persistedOtherScope.hPath, criterionA.hPath);+ });++ it("invalidates older asynchronous search path requests", () => {+ const searchElement = {};+ const firstVersion = nextSearchPathRequestVersion(searchElement);+ assert.equal(isSearchPathRequestVersionCurrent(searchElement, firstVersion), true);++ const secondVersion = nextSearchPathRequestVersion(searchElement);+ assert.equal(isSearchPathRequestVersionCurrent(searchElement, firstVersion), false);+ assert.equal(isSearchPathRequestVersionCurrent(searchElement, secondVersion), true);+ });++ it("resolves current human-readable paths without changing their stable paths", async () => {+ const idPath = ["notebook/parent/document", "notebook/second"];+ const requestedPaths: string[] = [];+ const hPath = await resolveSearchHPath(idPath, async (path) => {+ requestedPaths.push(path);+ return path.endsWith("document") ? "Notebook/New <Title>" : "Notebook/Second";+ });++ assert.equal(hPath, "Notebook/New <Title> Notebook/Second");+ assert.deepEqual(requestedPaths, idPath);+ assert.deepEqual(idPath, ["notebook/parent/document", "notebook/second"]);+ assert.equal(await resolveSearchHPath([], async () => "unused"), undefined);+ assert.equal(await resolveSearchHPath(idPath, async () => undefined), undefined);+ assert.equal(await resolveSearchHPath(idPath, async () => {+ throw new Error("unavailable");+ }), undefined);+ });++ it("keeps the configured order when path requests finish out of order", async () => {+ const idPath = ["notebook/first", "notebook/second"];+ const resolvers = new Map<string, (value: string) => void>();+ const hPathPromise = resolveSearchHPath(idPath, (path) => new Promise((resolve) => {+ resolvers.set(path, resolve);+ }));++ resolvers.get(idPath[1])("Notebook/Second");+ resolvers.get(idPath[0])("Notebook/First");++ assert.equal(await hPathPromise, "Notebook/First Notebook/Second");+ });++ it("refreshes the original search configuration after resolving its current path", async () => {+ const config: Config.IUILayoutTabSearchConfig = {+ ...documentB,+ hPath: "Notebook/Old Document B",+ idPath: [...documentB.idPath],+ };+ const reference = config;+ let resolvedPath: string[];+ let renderedPath = "";++ const refreshed = await refreshSearchConfigHPath({+ config,+ resolveHPath: async (idPath) => {+ resolvedPath = idPath;+ return "Notebook/Renamed Document B";+ },+ render: (hPath) => {+ renderedPath = hPath;+ },+ });++ assert.equal(refreshed, true);+ assert.equal(config, reference);+ assert.equal(config.hPath, "Notebook/Renamed Document B");+ assert.deepEqual(config.idPath, documentB.idPath);+ assert.deepEqual(resolvedPath, documentB.idPath);+ assert.notEqual(resolvedPath, config.idPath);+ assert.equal(renderedPath, config.hPath);+ });++ it("discards a resolved path after the stable scope changes", async () => {+ const config: Config.IUILayoutTabSearchConfig = {+ ...documentB,+ hPath: "Notebook/Old Document B",+ idPath: [...documentB.idPath],+ };+ let resolveHPath: (value: string) => void;+ let rendered = false;+ const refreshed = refreshSearchConfigHPath({+ config,+ resolveHPath: () => new Promise((resolve) => {+ resolveHPath = resolve;+ }),+ render: () => {+ rendered = true;+ },+ });++ config.idPath = ["notebook/other"];+ resolveHPath("Notebook/Renamed Document B");++ assert.equal(await refreshed, false);+ assert.equal(config.hPath, "Notebook/Old Document B");+ assert.equal(rendered, false);+ });++ it("does not refresh when the request is stale or fails", async () => {+ const staleConfig = cloneSearchConfig(documentB);+ assert.equal(await refreshSearchConfigHPath({+ config: staleConfig,+ resolveHPath: async () => "Notebook/Renamed Document B",+ isCurrent: () => false,+ }), false);+ assert.equal(staleConfig.hPath, documentB.hPath);++ const failedConfig = cloneSearchConfig(documentB);+ assert.equal(await refreshSearchConfigHPath({+ config: failedConfig,+ resolveHPath: async () => {+ throw new Error("unavailable");+ },+ }), false);+ assert.equal(failedConfig.hPath, documentB.hPath);+ assert.equal(isSameSearchPath(documentB.idPath, failedConfig.idPath), true);+ });++ it("updates shared configuration references in place", () => {+ const target: Config.IUILayoutTabSearchConfig = {+ hPath: documentB.hPath,+ idPath: [...documentB.idPath],+ query: "stale query",+ };+ const reference = target;++ const result = syncSearchConfig(target, criterionA);++ assert.equal(result, reference);+ assert.equal(reference.hPath, criterionA.hPath);+ assert.deepEqual(reference.idPath, criterionA.idPath);+ assert.equal(reference.query, undefined);+ assert.notEqual(reference.idPath, criterionA.idPath);+ });+++ it("keeps the saved path while overriding the runtime path for document search", () => {+ const criterion: Config.IUILayoutTabSearchConfig = {+ removed: false,+ name: "Document A search",+ hPath: "Notebook/Document A",+ idPath: ["notebook/document-a"],+ replaceTypes: {+ aText: true,+ },+ };+ const currentConfig: Config.IUILayoutTabSearchConfig = {+ hPath: "Notebook/Document B",+ idPath: ["notebook/document-b"],+ };++ const result = resolveSearchConfigUpdate({+ selectedConfig: criterion,+ currentConfig,+ useCurrentPath: true,+ persistedConfig: criterion,+ });++ assert.equal(result.runtimeConfig.hPath, "Notebook/Document B");+ assert.deepEqual(result.runtimeConfig.idPath, ["notebook/document-b"]);+ assert.equal(result.persistedConfig.hPath, "Notebook/Document A");+ assert.deepEqual(result.persistedConfig.idPath, ["notebook/document-a"]);+ assert.notStrictEqual(result.runtimeConfig.idPath, currentConfig.idPath);+ assert.notStrictEqual(result.persistedConfig.idPath, criterion.idPath);+ assert.notStrictEqual(result.persistedConfig.replaceTypes, criterion.replaceTypes);+ assert.deepEqual(criterion.idPath, ["notebook/document-a"]);+ });++ it("uses the selected runtime path when no contextual override is requested", () => {+ const result = resolveSearchConfigUpdate({+ selectedConfig: {+ hPath: "Notebook/Document B",+ idPath: ["notebook/document-b"],+ },+ currentConfig: {+ hPath: "Notebook/Document A",+ idPath: ["notebook/document-a"],+ },+ useCurrentPath: false,+ });++ assert.equal(result.runtimeConfig.hPath, "Notebook/Document B");+ assert.deepEqual(result.runtimeConfig.idPath, ["notebook/document-b"]);+ });});diff --cc app/src/search/config.tsindex dd98a7d818,136bb97a1d..230b2c6aa2--- a/app/src/search/config.ts+++ b/app/src/search/config.ts@@@ -191,3 -14,20 +191,22 @@@ export const getGlobalSearchPath = (conidPath: [...(config.idPath || [])],};};+++export const resolveGlobalSearchScope = getGlobalSearchPath;+++ export const resolveSearchConfigUpdate = (options: {+ selectedConfig: Config.IUILayoutTabSearchConfig,+ currentConfig: Config.IUILayoutTabSearchConfig,+ useCurrentPath: boolean,+ persistedConfig?: Config.IUILayoutTabSearchConfig,+ }) => {+ const runtimeConfig = cloneSearchConfig(options.selectedConfig);+ if (options.useCurrentPath) {+ runtimeConfig.hPath = options.currentConfig.hPath || "";+ runtimeConfig.idPath = [...(options.currentConfig.idPath || [])];+ }+ return {+ runtimeConfig,+ persistedConfig: cloneSearchConfig(options.persistedConfig || options.selectedConfig),+ };+ };diff --cc app/src/search/menu.tsindex 278fec1cdd,278fec1cdd..dc102770cd--- a/app/src/search/menu.ts+++ b/app/src/search/menu.ts@@@ -9,6 -9,6 +9,7 @@@ import {isSensitiveSearchConfig, setStoimport {confirmDialog} from "../dialog/confirmDialog";import {goUnRef, updateSearchResult} from "../mobile/menu/search";import {getDefaultSubType} from "./getDefault";++import {hasSearchConfigTemporaryPath, resolvePersistedSearchConfig} from "./config";export const filterMenu = (config: Config.IUILayoutTabSearchConfig, cb: () => void) => {const filterDialog = new Dialog({@@@ -288,7 -288,7 +289,11 @@@}});cb();-- window.siyuan.storage[Constants.LOCAL_SEARCHDATA] = Object.assign({}, config);++ window.siyuan.storage[Constants.LOCAL_SEARCHDATA] = resolvePersistedSearchConfig(++ config,++ window.siyuan.storage[Constants.LOCAL_SEARCHDATA],++ hasSearchConfigTemporaryPath(config),++ );setStorageVal(Constants.LOCAL_SEARCHDATA, window.siyuan.storage[Constants.LOCAL_SEARCHDATA]);filterDialog.destroy();});@@@ -325,7 -325,7 +330,11 @@@ export const replaceFilterMenu = (confifilterDialog.element.querySelectorAll(".b3-switch").forEach((item: HTMLInputElement) => {config.replaceTypes[item.getAttribute("data-type") as keyof (typeof config.replaceTypes)] = item.checked;});-- window.siyuan.storage[Constants.LOCAL_SEARCHDATA] = Object.assign({}, config);++ window.siyuan.storage[Constants.LOCAL_SEARCHDATA] = resolvePersistedSearchConfig(++ config,++ window.siyuan.storage[Constants.LOCAL_SEARCHDATA],++ hasSearchConfigTemporaryPath(config),++ );setStorageVal(Constants.LOCAL_SEARCHDATA, window.siyuan.storage[Constants.LOCAL_SEARCHDATA]);filterDialog.destroy();});@@@ -743,7 -743,7 +752,7 @@@ export const moreMenu = async (config:};const configIsSame = (config: Config.IUILayoutTabSearchConfig, config2: Config.IUILayoutTabSearchConfig) => {-- if (config2.group === config.group && config2.hPath === config.hPath && config2.hasReplace === config.hasReplace &&++ if (config2.group === config.group && config2.hasReplace === config.hasReplace &&config2.k === config.k && config2.method === config.method && config2.r === config.r &&config2.sort === config.sort && objEquals(config2.types, config.types) &&objEquals({...getDefaultSubType(), ...config2.subTypes},diff --cc app/src/search/spread.tsindex 05334ea514,c04ff8f6f3..0a1fb9582d--- a/app/src/search/spread.ts+++ b/app/src/search/spread.ts@@@ -6,6 -6,7 +6,15 @@@ import {focusByRange} from "../protyle/import {genSearch, updateConfig} from "./util";import type {App} from "../index";import {cancelSearchRequest} from "./request";-import {resolveGlobalSearchScope} from "./config";++import {++ hasExplicitSearchScope,++ replaceSearchConfigPath,++ resolveGlobalSearchScope,++ setSearchConfigTemporaryPath,++} from "./config";++import {beginSearchPathRequest} from "./path";++++let openSearchVersion = 0;export const openSearch = async (options: {app: App,@@@ -15,7 -16,7 +24,12 @@@notebookIds?: string[],searchPath?: string}) => {++ const version = ++openSearchVersion;++ const existingSearchDialog = window.siyuan.dialogs.find((item) => item.element.querySelector("#searchList"));++ const existingSearchElement = existingSearchDialog?.element.querySelector(".b3-dialog__body");++ const isCurrentPathRequest = existingSearchElement ? beginSearchPathRequest(existingSearchElement) : undefined;const localData = window.siyuan.storage[Constants.LOCAL_SEARCHDATA];++ const hasScopedPath = hasExplicitSearchScope(options);let hPath = "";let idPath: string[] = [];if (options.notebookIds?.length) {@@@ -29,6 -30,6 +43,9 @@@notebook: options.notebookId,path: options.searchPath.endsWith(".sy") ? options.searchPath : options.searchPath + ".sy"});++ if (version !== openSearchVersion || (isCurrentPathRequest && !isCurrentPathRequest())) {++ return;++ }if (response.code !== 0 || typeof response.data !== "string") {return;}@@@ -59,8 -56,8 +72,12 @@@replaceTypes: Object.assign({}, localData.replaceTypes),page: options.key ? 1 : localData.page};++ setSearchConfigTemporaryPath(config, hasScopedPath || options.hotkey === Constants.DIALOG_SEARCH);// 搜索中继续执行 ctrl+F/P 不退出 https://github.com/siyuan-note/siyuan/issues/11637const exitDialog = window.siyuan.dialogs.find((item) => {++ if (item !== existingSearchDialog || (isCurrentPathRequest && !isCurrentPathRequest())) {++ return false;++ }// 再次打开if (item.element.querySelector("#searchList")) {const searchElement = item.element.querySelector(".b3-dialog__body");@@@ -69,32 -66,34 +86,55 @@@if (selectText) {cloneData.k = selectText;}++ if (hasScopedPath) {++ setSearchConfigTemporaryPath(item.data, true);++ } else if (options.hotkey === Constants.DIALOG_GLOBALSEARCH) {++ setSearchConfigTemporaryPath(item.data, false);++ }item.element.setAttribute("data-key", options.hotkey);- const updateOptions = {- persistedConfig: localData.removed ? undefined : localData,- useCurrentPath: false,- };if (options.notebookId || options.notebookIds?.length) {cloneData.hasReplace = options.hotkey === Constants.DIALOG_REPLACE;cloneData.hPath = hPath;cloneData.idPath = [...idPath];- item.data = updateConfig(searchElement, cloneData, item.data, item.editors.edit);- item.data = updateConfig(searchElement, cloneData, item.data, item.editors.edit, updateOptions);++ item.data = updateConfig(searchElement, cloneData, item.data, item.editors.edit, {++ storageConfig: replaceSearchConfigPath(cloneData, localData),++ });} else if (options.hotkey === Constants.DIALOG_REPLACE) {cloneData.hasReplace = true;- item.data = updateConfig(searchElement, cloneData, item.data, item.editors.edit);- item.data = updateConfig(searchElement, cloneData, item.data, item.editors.edit, updateOptions);++ item.data = updateConfig(searchElement, cloneData, item.data, item.editors.edit, {++ storageConfig: replaceSearchConfigPath(cloneData, localData),++ });} else if (options.hotkey === Constants.DIALOG_GLOBALSEARCH) {cloneData.hasReplace = false;- cloneData.hPath = "";- cloneData.idPath = [];- item.data = updateConfig(searchElement, cloneData, item.data, item.editors.edit);+ cloneData.hPath = hPath;+ cloneData.idPath = [...idPath];- item.data = updateConfig(searchElement, cloneData, item.data, item.editors.edit, updateOptions);++ item.data = updateConfig(searchElement, cloneData, item.data, item.editors.edit, {++ storageConfig: replaceSearchConfigPath(cloneData, localData),++ });} else if (options.hotkey === Constants.DIALOG_SEARCH) {-- cloneData.hasReplace = false;const toPath = item.editors.edit.protyle.path;++ const toNotebook = item.editors.edit.protyle.notebookId;fetchPost("/api/filetree/getHPathsByPaths", {paths: [toPath]}, (response) => {++ if (version !== openSearchVersion || !item.element.isConnected ||++ item.element.getAttribute("data-key") !== Constants.DIALOG_SEARCH ||++ (isCurrentPathRequest && !isCurrentPathRequest())) {++ return;++ }if (!Array.isArray(response.data) || typeof response.data[0] !== "string") {return;}-- cloneData.idPath = [pathPosix().join(item.editors.edit.protyle.notebookId, toPath)];-- cloneData.hPath = response.data[0];- item.data.idPath = cloneData.idPath;- item.data.hPath = cloneData.hPath;- item.data = updateConfig(searchElement, cloneData, item.data, item.editors.edit);- item.data = updateConfig(searchElement, cloneData, item.data, item.editors.edit, updateOptions);++ const currentData = JSON.parse(JSON.stringify(item.data)) as Config.IUILayoutTabSearchConfig;++ currentData.hasReplace = false;++ if (selectText) {++ currentData.k = selectText;++ }++ currentData.idPath = [pathPosix().join(toNotebook, toPath)];++ currentData.hPath = response.data[0];++ setSearchConfigTemporaryPath(item.data, true);++ item.data = updateConfig(searchElement, currentData, item.data, item.editors.edit, {++ storageConfig: replaceSearchConfigPath(++ currentData, window.siyuan.storage[Constants.LOCAL_SEARCHDATA]),++ });});}return true;diff --cc app/src/search/util.tsindex 744bb96535,64c5e15d31..bedb7a3d33--- a/app/src/search/util.ts+++ b/app/src/search/util.ts@@@ -53,6 -53,7 +53,34 @@@ import {electronUndo} from "../protyle/import {getContenteditableElement} from "../protyle/wysiwyg/getBlock";import {IDatabaseItemOpenData, openDatabaseItem} from "../protyle/render/av/openDatabaseItem";import {scheduleSearchRequest} from "./request";-import {resolveSearchConfigUpdate} from "./config";++import {++ cloneSearchConfig,++ hasSearchConfigTemporaryPath,++ resolvePersistedSearchConfig,++ resolveSearchConfigUpdate,++ syncSearchConfig,++ syncSearchConfigHPath,++} from "./config";++import {beginSearchPathRequest, invalidateSearchPathRequests, refreshCurrentSearchPath} from "./path";++++const persistSearchConfig = (config: Config.IUILayoutTabSearchConfig) => {++ window.siyuan.storage[Constants.LOCAL_SEARCHDATA] = resolvePersistedSearchConfig(++ config,++ window.siyuan.storage[Constants.LOCAL_SEARCHDATA],++ hasSearchConfigTemporaryPath(config),++ );++ setStorageVal(Constants.LOCAL_SEARCHDATA, window.siyuan.storage[Constants.LOCAL_SEARCHDATA]);++};++++const hydrateSearchPath = (element: Element, config: Config.IUILayoutTabSearchConfig) => {++ void refreshCurrentSearchPath({element, config}).then((refreshed) => {++ const localConfig = window.siyuan.storage[Constants.LOCAL_SEARCHDATA];++ if (!refreshed || !syncSearchConfigHPath(localConfig, config)) {++ return;++ }++ setStorageVal(Constants.LOCAL_SEARCHDATA, localConfig);++ });++};export const openGlobalSearch = (app: App, text: string, replace: boolean, searchData?: Config.IUILayoutTabSearchConfig) => {text = text.trim();@@@ -373,7 -374,7 +401,7 @@@ export const genSearch = (app: App, contypes: getDefaultType(),subTypes: getDefaultSubType(),replaceTypes: Object.assign({}, Constants.SIYUAN_DEFAULT_REPLACETYPES),- }, config, edit, true);- }, config, edit, {clear: true});++ }, config, edit, {clear: true, preserveCurrentPath: true});element.querySelector(".b3-chip--current")?.classList.remove("b3-chip--current");event.stopPropagation();event.preventDefault();@@@ -409,7 -409,8 +436,9 @@@target.classList.add("b3-chip--current");criteriaData.find(item => {if (item.name === target.innerText.trim()) {- config = updateConfig(element, item, config, edit);- const criterion = Object.assign({}, item, {removed: false});- config = updateConfig(element, criterion, config, edit, {persistedConfig: criterion});++ const criterion = cloneSearchConfig(item);++ criterion.removed = false;++ config = updateConfig(element, criterion, config, edit, {preserveCurrentPath: true});return true;}});@@@ -440,13 -441,13 +469,14 @@@types: getDefaultType(),subTypes: getDefaultSubType(),replaceTypes: Object.assign({}, Constants.SIYUAN_DEFAULT_REPLACETYPES),- }, config, edit, true);- }, config, edit, {clear: true});++ }, config, edit, {clear: true, preserveCurrentPath: true});}target.parentElement.remove();event.stopPropagation();event.preventDefault();break;} else if (target.classList.contains("search__rmpath")) {++ invalidateSearchPathRequests(element);config.idPath = [];config.hPath = "";config.page = 1;@@@ -482,7 -483,7 +512,11 @@@} else if (target.id === "searchPath") {movePathTo({cb: (toPath, toNotebook) => {++ const isCurrentPathRequest = beginSearchPathRequest(element);fetchPost("/api/filetree/getHPathsByPaths", {paths: toPath}, (response) => {++ if (!isCurrentPathRequest()) {++ return;++ }config.idPath = [];const hPathList: string[] = [];let enableIncludeChild = false;@@@ -524,6 -525,6 +558,7 @@@if (target.hasAttribute("disabled")) {return;}++ invalidateSearchPathRequests(element);const svgElement = target.firstElementChild;svgElement.classList.toggle("ft__primary");if (!svgElement.classList.contains("ft__primary")) {@@@ -608,7 -609,7 +643,7 @@@config.r = replaceInputElement.value;openFile({app,-- searchData: config,++ searchData: cloneSearchConfig(config),position: (!window.siyuan.config.fileTree.noSplitScreenWhenOpenTab && (window.siyuan.layout.centerLayout.children.length > 1 || window.innerWidth > 1024)) ? "right" : undefined});if (closeCB) {@@@ -641,7 -642,7 +676,7 @@@types: getDefaultType(),subTypes: getDefaultSubType(),replaceTypes: Object.assign({}, Constants.SIYUAN_DEFAULT_REPLACETYPES),- }, config, edit, true);- }, config, edit, {clear: true});++ }, config, edit, {clear: true, preserveCurrentPath: true});element.querySelector("#criteria .b3-chip--current")?.classList.remove("b3-chip--current");}, () => {const localData = window.siyuan.storage[Constants.LOCAL_SEARCHKEYS];@@@ -764,8 -765,8 +799,7 @@@element.querySelector("#searchSyntaxCheck").outerHTML = genQueryHTML(config.method, "searchSyntaxCheck");config.page = 1;inputEvent(element, config, edit, true);-- window.siyuan.storage[Constants.LOCAL_SEARCHDATA] = JSON.parse(JSON.stringify(config));-- setStorageVal(Constants.LOCAL_SEARCHDATA, window.siyuan.storage[Constants.LOCAL_SEARCHDATA]);++ persistSearchConfig(config);});const rect = target.getBoundingClientRect();window.siyuan.menus.menu.popup({x: rect.right, y: rect.bottom, isLeft: true});@@@ -930,8 -931,8 +964,7 @@@searchInputElement.addEventListener("blur", () => {if (config.removed) {config.k = searchInputElement.value;-- window.siyuan.storage[Constants.LOCAL_SEARCHDATA] = JSON.parse(JSON.stringify(config));-- setStorageVal(Constants.LOCAL_SEARCHDATA, window.siyuan.storage[Constants.LOCAL_SEARCHDATA]);++ persistSearchConfig(config);}saveKeyList("keys", searchInputElement.value, config);});@@@ -956,6 -957,6 +989,7 @@@height: searchInputElement.clientHeight,});inputEvent(element, config, edit);++ hydrateSearchPath(element, config);return {edit, unRefEdit};};@@@ -1074,16 -1075,23 +1108,23 @@@ export const genQueryHTML = (method: nu};export const updateConfig = (element: Element, item: Config.IUILayoutTabSearchConfig, config: Config.IUILayoutTabSearchConfig,- edit: Protyle, clear = false) => {- const dialogElement = hasClosestByClassName(element, "b3-dialog--open");- if (dialogElement && dialogElement.getAttribute("data-key") === Constants.DIALOG_SEARCH) {- // https://github.com/siyuan-note/siyuan/issues/6828- item.hPath = config.hPath;- item.idPath = [...config.idPath];- }- if (config.hasReplace !== item.hasReplace) {+ edit: Protyle, options?: {+ clear?: boolean,- persistedConfig?: Config.IUILayoutTabSearchConfig,- useCurrentPath?: boolean,++ preserveCurrentPath?: boolean,++ storageConfig?: Config.IUILayoutTabSearchConfig,+ }) => {- const dialogElement = hasClosestByClassName(element, "b3-dialog--open");++ invalidateSearchPathRequests(element);++ const preserveCurrentPath = Boolean(options?.preserveCurrentPath && hasSearchConfigTemporaryPath(config));+ const resolvedConfig = resolveSearchConfigUpdate({+ selectedConfig: item,+ currentConfig: config,- useCurrentPath: options?.useCurrentPath ??- Boolean(dialogElement && dialogElement.getAttribute("data-key") === Constants.DIALOG_SEARCH),- persistedConfig: options?.persistedConfig,++ useCurrentPath: preserveCurrentPath,++ persistedConfig: options?.storageConfig,+ });+ const runtimeConfig = resolvedConfig.runtimeConfig;+ if (config.hasReplace !== runtimeConfig.hasReplace) {const replaceHeaderElement = element.querySelectorAll(".search__header")[1];- if (item.hasReplace) {+ if (runtimeConfig.hasReplace) {replaceHeaderElement.classList.remove("fn__none");} else {replaceHeaderElement.classList.add("fn__none");@@@ -1125,15 -1133,15 +1166,16 @@@} else {searchIncludeElement.setAttribute("disabled", "disabled");}- if (item.k || clear) {- (element.querySelector("#searchInput") as HTMLInputElement).value = item.k;+ if (runtimeConfig.k || options?.clear) {+ (element.querySelector("#searchInput") as HTMLInputElement).value = runtimeConfig.k;}- (element.querySelector("#replaceInput") as HTMLInputElement).value = item.r;- element.querySelector("#searchSyntaxCheck").outerHTML = genQueryHTML(item.method, "searchSyntaxCheck");- config = JSON.parse(JSON.stringify(item));- window.siyuan.storage[Constants.LOCAL_SEARCHDATA] = JSON.parse(JSON.stringify(item));+ (element.querySelector("#replaceInput") as HTMLInputElement).value = runtimeConfig.r;+ element.querySelector("#searchSyntaxCheck").outerHTML = genQueryHTML(runtimeConfig.method, "searchSyntaxCheck");- config = runtimeConfig;++ syncSearchConfig(config, runtimeConfig);+ window.siyuan.storage[Constants.LOCAL_SEARCHDATA] = resolvedConfig.persistedConfig;setStorageVal(Constants.LOCAL_SEARCHDATA, window.siyuan.storage[Constants.LOCAL_SEARCHDATA]);inputEvent(element, config, edit);++ hydrateSearchPath(element, config);window.siyuan.menus.menu.remove();return config;};