An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: Improve saved search path handling between Ctrl+F and Ctrl+P https://github.com/siyuan-note/siyuan/issues/16469
Daniel311642a811b4
diff --git a/app/src/search/config.test.ts b/app/src/search/config.test.tsnew file mode 100644index 0000000000..dc39ce4649--- /dev/null+++ b/app/src/search/config.test.ts@@ -0,0 +1,83 @@+import {describe, it} from "node:test";+import * as assert from "node:assert/strict";+import {resolveGlobalSearchScope, resolveSearchConfigUpdate} 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"],+ };++ const scope = resolveGlobalSearchScope(config);++ assert.deepEqual(scope, {+ hPath: "Notebook/Document A",+ idPath: ["notebook/document-a"],+ });+ assert.notStrictEqual(scope.idPath, config.idPath);+ });++ it("uses an empty path for global search without a criterion", () => {+ const scope = resolveGlobalSearchScope({+ removed: true,+ hPath: "Notebook/Document B",+ idPath: ["notebook/document-b"],+ });++ assert.deepEqual(scope, {+ hPath: "",+ 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 --git a/app/src/search/config.ts b/app/src/search/config.tsnew file mode 100644index 0000000000..136bb97a1d--- /dev/null+++ b/app/src/search/config.ts@@ -0,0 +1,33 @@+const cloneSearchConfig = (config: Config.IUILayoutTabSearchConfig) => {+ return JSON.parse(JSON.stringify(config)) as Config.IUILayoutTabSearchConfig;+};++export const resolveGlobalSearchScope = (config: Config.IUILayoutTabSearchConfig) => {+ if (config.removed) {+ return {+ hPath: "",+ idPath: [] as string[],+ };+ }+ return {+ hPath: config.hPath || "",+ idPath: [...(config.idPath || [])],+ };+};++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 --git a/app/src/search/spread.ts b/app/src/search/spread.tsindex 05334ea514..c04ff8f6f3 100644--- a/app/src/search/spread.ts+++ b/app/src/search/spread.ts@@ -6,6 +6,7 @@ import {focusByRange} from "../protyle/util/selection";import {genSearch, updateConfig} from "./util";import type {App} from "../index";import {cancelSearchRequest} from "./request";+import {resolveGlobalSearchScope} from "./config";export const openSearch = async (options: {app: App,@@ -36,13 +37,9 @@ export const openSearch = async (options: {idPath[0] = pathPosix().join(idPath[0], options.searchPath);}} else if (Constants.DIALOG_GLOBALSEARCH === options.hotkey) {- if (localData.removed) {- hPath = "";- idPath = [];- } else {- hPath = localData.hPath;- idPath = [...localData.idPath];- }+ const globalScope = resolveGlobalSearchScope(localData);+ hPath = globalScope.hPath;+ idPath = globalScope.idPath;}const config = {removed: localData.removed,@@ -70,19 +67,23 @@ export const openSearch = async (options: {cloneData.k = selectText;}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);} 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);} 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);} else if (options.hotkey === Constants.DIALOG_SEARCH) {cloneData.hasReplace = false;const toPath = item.editors.edit.protyle.path;@@ -92,9 +93,7 @@ export const openSearch = async (options: {}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);});}return true;diff --git a/app/src/search/util.ts b/app/src/search/util.tsindex 744bb96535..64c5e15d31 100644--- a/app/src/search/util.ts+++ b/app/src/search/util.ts@@ -53,6 +53,7 @@ import {electronUndo} from "../protyle/undo";import {getContenteditableElement} from "../protyle/wysiwyg/getBlock";import {IDatabaseItemOpenData, openDatabaseItem} from "../protyle/render/av/openDatabaseItem";import {scheduleSearchRequest} from "./request";+import {resolveSearchConfigUpdate} from "./config";export const openGlobalSearch = (app: App, text: string, replace: boolean, searchData?: Config.IUILayoutTabSearchConfig) => {text = text.trim();@@ -373,7 +374,7 @@ export const genSearch = (app: App, config: Config.IUILayoutTabSearchConfig, eletypes: getDefaultType(),subTypes: getDefaultSubType(),replaceTypes: Object.assign({}, Constants.SIYUAN_DEFAULT_REPLACETYPES),- }, config, edit, true);+ }, config, edit, {clear: true});element.querySelector(".b3-chip--current")?.classList.remove("b3-chip--current");event.stopPropagation();event.preventDefault();@@ -404,12 +405,12 @@ export const genSearch = (app: App, config: Config.IUILayoutTabSearchConfig, eleevent.preventDefault();break;} else if (target.classList.contains("b3-chip") && type === "set-criteria") {- config.removed = false;target.parentElement.querySelector(".b3-chip--current")?.classList.remove("b3-chip--current");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});return true;}});@@ -440,7 +441,7 @@ export const genSearch = (app: App, config: Config.IUILayoutTabSearchConfig, eletypes: getDefaultType(),subTypes: getDefaultSubType(),replaceTypes: Object.assign({}, Constants.SIYUAN_DEFAULT_REPLACETYPES),- }, config, edit, true);+ }, config, edit, {clear: true});}target.parentElement.remove();event.stopPropagation();@@ -641,7 +642,7 @@ export const genSearch = (app: App, config: Config.IUILayoutTabSearchConfig, eletypes: getDefaultType(),subTypes: getDefaultSubType(),replaceTypes: Object.assign({}, Constants.SIYUAN_DEFAULT_REPLACETYPES),- }, config, edit, true);+ }, config, edit, {clear: true});element.querySelector("#criteria .b3-chip--current")?.classList.remove("b3-chip--current");}, () => {const localData = window.siyuan.storage[Constants.LOCAL_SEARCHKEYS];@@ -1074,31 +1075,38 @@ export const genQueryHTML = (method: number, id: string) => {};export const updateConfig = (element: Element, item: Config.IUILayoutTabSearchConfig, config: Config.IUILayoutTabSearchConfig,- edit: Protyle, clear = false) => {+ edit: Protyle, options?: {+ clear?: boolean,+ persistedConfig?: Config.IUILayoutTabSearchConfig,+ useCurrentPath?: boolean,+ }) => {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) {+ const resolvedConfig = resolveSearchConfigUpdate({+ selectedConfig: item,+ currentConfig: config,+ useCurrentPath: options?.useCurrentPath ??+ Boolean(dialogElement && dialogElement.getAttribute("data-key") === Constants.DIALOG_SEARCH),+ persistedConfig: options?.persistedConfig,+ });+ 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");}}const searchPathInputElement = element.querySelector("#searchPathInput");- if (item.hPath) {- searchPathInputElement.innerHTML = `${escapeHtml(item.hPath)}<svg class="search__rmpath"><use xlink:href="#iconCloseRound"></use></svg>`;- searchPathInputElement.setAttribute("aria-label", escapeHtml(item.hPath));+ if (runtimeConfig.hPath) {+ searchPathInputElement.innerHTML = `${escapeHtml(runtimeConfig.hPath)}<svg class="search__rmpath"><use xlink:href="#iconCloseRound"></use></svg>`;+ searchPathInputElement.setAttribute("aria-label", escapeHtml(runtimeConfig.hPath));} else {searchPathInputElement.innerHTML = "";searchPathInputElement.setAttribute("aria-label", "");}- if (config.group !== item.group) {- if (item.group === 0) {+ if (config.group !== runtimeConfig.group) {+ if (runtimeConfig.group === 0) {element.querySelector("#searchExpand").parentElement.classList.add("fn__none");} else {element.querySelector("#searchExpand").parentElement.classList.remove("fn__none");@@ -1106,7 +1114,7 @@ export const updateConfig = (element: Element, item: Config.IUILayoutTabSearchCo}let includeChild = true;let enableIncludeChild = false;- item.idPath.forEach(pathItem => {+ runtimeConfig.idPath.forEach(pathItem => {if (pathItem.endsWith(".sy")) {includeChild = false;}@@ -1125,13 +1133,13 @@ export const updateConfig = (element: Element, item: Config.IUILayoutTabSearchCo} 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;+ window.siyuan.storage[Constants.LOCAL_SEARCHDATA] = resolvedConfig.persistedConfig;setStorageVal(Constants.LOCAL_SEARCHDATA, window.siyuan.storage[Constants.LOCAL_SEARCHDATA]);inputEvent(element, config, edit);window.siyuan.menus.menu.remove();