Vanessa 2024-06-09 15:46:31 +08:00
parent 6acf780269
commit 80ebf8586c
4 changed files with 55 additions and 100 deletions

View file

@ -1,9 +1,9 @@
import {getNotebookName, pathPosix} from "../util/pathName";
import {Constants} from "../constants";
import {Dialog} from "../dialog";
import {fetchSyncPost} from "../util/fetch";
import {fetchPost, fetchSyncPost} from "../util/fetch";
import {focusByRange} from "../protyle/util/selection";
import {genSearch} from "./util";
import {genSearch, updateConfig} from "./util";
import {App} from "../index";
export const openSearch = async (options: {
@ -13,49 +13,6 @@ export const openSearch = async (options: {
notebookId?: string,
searchPath?: string
}) => {
// 全局搜索中使用 ctrl+F 需继续执行 https://ld246.com/article/1716632837934
let globalToPath = false;
const exitDialog = window.siyuan.dialogs.find((item) => {
if (item.element.querySelector("#searchList")) {
const lastKey = item.element.getAttribute("data-key");
if (lastKey === Constants.DIALOG_GLOBALSEARCH && options.hotkey === Constants.DIALOG_SEARCH) {
globalToPath = true;
}
const replaceHeaderElement = item.element.querySelectorAll(".search__header")[1];
if (lastKey !== options.hotkey && options.hotkey === Constants.DIALOG_REPLACE && replaceHeaderElement.classList.contains("fn__none")) {
replaceHeaderElement.classList.remove("fn__none");
item.element.setAttribute("data-key", options.hotkey);
return true;
}
const searchPathElement = item.element.querySelector("#searchPathInput");
if (lastKey !== options.hotkey && options.hotkey === Constants.DIALOG_GLOBALSEARCH) {
if (searchPathElement.textContent !== "") {
item.destroy();
return false;
} else if (!replaceHeaderElement.classList.contains("fn__none")) {
replaceHeaderElement.classList.add("fn__none");
item.element.setAttribute("data-key", options.hotkey);
return true;
}
}
if (lastKey !== options.hotkey && options.hotkey === Constants.DIALOG_SEARCH) {
if (searchPathElement.textContent === "") {
item.destroy();
return false;
} else if (!replaceHeaderElement.classList.contains("fn__none")) {
replaceHeaderElement.classList.add("fn__none");
item.element.setAttribute("data-key", options.hotkey);
return true;
}
}
// 切换关闭
item.destroy();
return true;
}
});
if (exitDialog && !globalToPath) {
return;
}
const localData = window.siyuan.storage[Constants.LOCAL_SEARCHDATA];
let hPath = "";
let idPath: string[] = [];
@ -79,7 +36,56 @@ export const openSearch = async (options: {
idPath = localData.idPath;
}
}
const config = {
removed: localData.removed,
k: options.key || localData.k,
r: localData.r,
hasReplace: options.hotkey === Constants.DIALOG_REPLACE,
method: localData.method,
hPath,
idPath,
group: localData.group,
sort: localData.sort,
types: Object.assign({}, localData.types),
replaceTypes: Object.assign({}, localData.replaceTypes),
page: options.key ? 1 : localData.page
};
// 搜索中继续执行 ctrl+F/P 不退出 https://github.com/siyuan-note/siyuan/issues/11637
const exitDialog = window.siyuan.dialogs.find((item) => {
// 再次打开
if (item.element.querySelector("#searchList")) {
const searchElement = item.element.querySelector(".b3-dialog__body")
const cloneData = JSON.parse(JSON.stringify(item.data)) as Config.IUILayoutTabSearchConfig;
const selectText = getSelection().rangeCount > 0 ? getSelection().getRangeAt(0).toString() : undefined;
if (selectText) {
cloneData.k = selectText;
}
item.element.setAttribute("data-key", options.hotkey);
if (options.hotkey === Constants.DIALOG_REPLACE) {
cloneData.hasReplace = true;
updateConfig(searchElement, cloneData, item.data, item.editors.edit);
} else if (options.hotkey === Constants.DIALOG_GLOBALSEARCH) {
cloneData.hasReplace = false;
cloneData.hPath = "";
cloneData.idPath = [];
updateConfig(searchElement, cloneData, item.data, item.editors.edit);
} else if (options.hotkey === Constants.DIALOG_SEARCH) {
cloneData.hasReplace = false;
const toPath = item.editors.edit.protyle.path
fetchPost("/api/filetree/getHPathsByPaths", {paths: [toPath]}, (response) => {
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
updateConfig(searchElement, cloneData, item.data, item.editors.edit);
});
}
return true;
}
});
if (exitDialog) {
return;
}
let range: Range;
if (getSelection().rangeCount > 0) {
range = getSelection().getRangeAt(0);
@ -107,20 +113,6 @@ export const openSearch = async (options: {
}
});
dialog.element.setAttribute("data-key", options.hotkey);
const config = {
removed: localData.removed,
k: options.key || localData.k,
r: localData.r,
hasReplace: options.hotkey === Constants.DIALOG_REPLACE,
method: localData.method,
hPath,
idPath,
group: localData.group,
sort: localData.sort,
types: Object.assign({}, localData.types),
replaceTypes: Object.assign({}, localData.replaceTypes),
page: options.key ? 1 : localData.page
};
dialog.editors = genSearch(options.app, config, dialog.element.querySelector(".b3-dialog__body"), () => {
dialog.destroy({focus: "false"});
});

View file

@ -1072,7 +1072,7 @@ export const getQueryTip = (method: number) => {
return methodTip;
};
const updateConfig = (element: Element, item: Config.IUILayoutTabSearchConfig, config: Config.IUILayoutTabSearchConfig,
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) {

View file

@ -1925,7 +1925,7 @@ declare namespace Config {
* - `1`: Group by document
*/
group: number;
hasReplace: any;
hasReplace: boolean;
/**
* Readable path list
*/
@ -1961,6 +1961,7 @@ declare namespace Config {
r: string;
/**
* Whether to clear the search box after removing the currently used query condition group
* https://github.com/siyuan-note/siyuan/issues/7745
*/
removed?: boolean;
replaceTypes: IUILayoutTabSearchConfigReplaceTypes;

View file

@ -259,44 +259,6 @@ interface ISearchAssetOption {
k: string,
}
interface ISearchOption {
page: number
removed?: boolean // 移除后需记录搜索内容 https://github.com/siyuan-note/siyuan/issues/7745
name?: string
sort: number, // 0按块类型默认1按创建时间升序2按创建时间降序3按更新时间升序4按更新时间降序5按内容顺序仅在按文档分组时6按相关度升序7按相关度降序
group: number, // 0不分组1按文档分组
hasReplace: boolean,
method: number // 0文本1查询语法2SQL3正则表达式
hPath: string
idPath: string[]
k: string
r: string
types: ISearchType,
replaceTypes: {
[key: string]: boolean;
},
}
interface ISearchType {
audioBlock: boolean
videoBlock: boolean
iframeBlock: boolean
widgetBlock: boolean
mathBlock: boolean
table: boolean
blockquote: boolean
superBlock: boolean
paragraph: boolean
document: boolean
heading: boolean
list: boolean
listItem: boolean
codeBlock: boolean
htmlBlock: boolean
embedBlock: boolean
databaseBlock: boolean
}
interface ITextOption {
color?: string,
type: string