2022-11-28 00:39:51 +08:00
|
|
|
import {getNotebookName, pathPosix} from "../util/pathName";
|
2022-05-26 15:18:53 +08:00
|
|
|
import {Constants} from "../constants";
|
|
|
|
|
import {Dialog} from "../dialog";
|
2024-06-09 15:46:31 +08:00
|
|
|
import {fetchPost, fetchSyncPost} from "../util/fetch";
|
2022-05-26 15:18:53 +08:00
|
|
|
import {focusByRange} from "../protyle/util/selection";
|
2024-06-09 15:46:31 +08:00
|
|
|
import {genSearch, updateConfig} from "./util";
|
2023-05-18 19:27:21 +08:00
|
|
|
import {App} from "../index";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
2023-05-18 19:27:21 +08:00
|
|
|
export const openSearch = async (options: {
|
|
|
|
|
app: App,
|
|
|
|
|
hotkey: string,
|
|
|
|
|
key?: string,
|
|
|
|
|
notebookId?: string,
|
|
|
|
|
searchPath?: string
|
|
|
|
|
}) => {
|
2023-03-22 11:56:58 +08:00
|
|
|
const localData = window.siyuan.storage[Constants.LOCAL_SEARCHDATA];
|
2022-12-20 20:25:30 +08:00
|
|
|
let hPath = "";
|
2022-12-12 00:02:26 +08:00
|
|
|
let idPath: string[] = [];
|
2023-05-18 19:27:21 +08:00
|
|
|
if (options.notebookId) {
|
|
|
|
|
hPath = getNotebookName(options.notebookId);
|
|
|
|
|
idPath.push(options.notebookId);
|
|
|
|
|
if (options.searchPath && options.searchPath !== "/") {
|
2022-11-28 00:06:06 +08:00
|
|
|
const response = await fetchSyncPost("/api/filetree/getHPathByPath", {
|
2023-05-18 19:27:21 +08:00
|
|
|
notebook: options.notebookId,
|
|
|
|
|
path: options.searchPath.endsWith(".sy") ? options.searchPath : options.searchPath + ".sy"
|
2022-11-28 00:06:06 +08:00
|
|
|
});
|
2023-04-03 12:29:34 +08:00
|
|
|
hPath = pathPosix().join(hPath, response.data);
|
2023-05-18 19:27:21 +08:00
|
|
|
idPath[0] = pathPosix().join(idPath[0], options.searchPath);
|
2022-11-28 00:06:06 +08:00
|
|
|
}
|
2023-12-22 12:30:12 +08:00
|
|
|
} else if (Constants.DIALOG_GLOBALSEARCH === options.hotkey) {
|
2023-07-20 23:43:04 +08:00
|
|
|
if (localData.removed) {
|
|
|
|
|
hPath = "";
|
|
|
|
|
idPath = [];
|
|
|
|
|
} else {
|
|
|
|
|
hPath = localData.hPath;
|
2025-11-15 18:16:48 +08:00
|
|
|
idPath = [...localData.idPath];
|
2022-12-21 22:27:46 +08:00
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
2024-06-09 15:46:31 +08:00
|
|
|
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")) {
|
2024-06-10 10:44:58 +08:00
|
|
|
const searchElement = item.element.querySelector(".b3-dialog__body");
|
2024-06-09 15:46:31 +08:00
|
|
|
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;
|
2025-11-27 18:20:45 +08:00
|
|
|
item.data = updateConfig(searchElement, cloneData, item.data, item.editors.edit);
|
2024-06-09 15:46:31 +08:00
|
|
|
} else if (options.hotkey === Constants.DIALOG_GLOBALSEARCH) {
|
|
|
|
|
cloneData.hasReplace = false;
|
|
|
|
|
cloneData.hPath = "";
|
|
|
|
|
cloneData.idPath = [];
|
2025-11-27 18:20:45 +08:00
|
|
|
item.data = updateConfig(searchElement, cloneData, item.data, item.editors.edit);
|
2024-06-09 15:46:31 +08:00
|
|
|
} else if (options.hotkey === Constants.DIALOG_SEARCH) {
|
|
|
|
|
cloneData.hasReplace = false;
|
2024-06-10 10:44:58 +08:00
|
|
|
const toPath = item.editors.edit.protyle.path;
|
2024-06-09 15:46:31 +08:00
|
|
|
fetchPost("/api/filetree/getHPathsByPaths", {paths: [toPath]}, (response) => {
|
|
|
|
|
cloneData.idPath = [pathPosix().join(item.editors.edit.protyle.notebookId, toPath)];
|
|
|
|
|
cloneData.hPath = response.data[0];
|
2024-06-10 10:44:58 +08:00
|
|
|
item.data.idPath = cloneData.idPath;
|
|
|
|
|
item.data.hPath = cloneData.hPath;
|
2025-11-27 18:20:45 +08:00
|
|
|
item.data = updateConfig(searchElement, cloneData, item.data, item.editors.edit);
|
2024-06-09 15:46:31 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (exitDialog) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
let range: Range;
|
|
|
|
|
if (getSelection().rangeCount > 0) {
|
|
|
|
|
range = getSelection().getRangeAt(0);
|
|
|
|
|
}
|
|
|
|
|
const dialog = new Dialog({
|
2023-12-22 13:09:44 +08:00
|
|
|
positionId: options.hotkey,
|
2022-11-28 00:06:06 +08:00
|
|
|
content: "",
|
2022-05-26 15:18:53 +08:00
|
|
|
width: "80vw",
|
2023-03-23 18:47:05 +08:00
|
|
|
height: "90vh",
|
2023-04-28 22:08:57 +08:00
|
|
|
destroyCallback(options: IObject) {
|
|
|
|
|
if (range && !options) {
|
2022-05-26 15:18:53 +08:00
|
|
|
focusByRange(range);
|
|
|
|
|
}
|
2024-02-29 00:33:11 +08:00
|
|
|
dialog.editors.edit.destroy();
|
|
|
|
|
dialog.editors.unRefEdit.destroy();
|
2023-09-14 09:56:13 +08:00
|
|
|
},
|
|
|
|
|
resizeCallback(type: string) {
|
2024-02-29 00:33:11 +08:00
|
|
|
if (type !== "d" && type !== "t") {
|
|
|
|
|
if (dialog.element.querySelector("#searchUnRefPanel").classList.contains("fn__none")) {
|
|
|
|
|
dialog.editors.edit.resize();
|
|
|
|
|
} else {
|
|
|
|
|
dialog.editors.unRefEdit.resize();
|
|
|
|
|
}
|
2023-09-14 09:56:13 +08:00
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
|
});
|
2023-05-18 19:27:21 +08:00
|
|
|
dialog.element.setAttribute("data-key", options.hotkey);
|
2024-02-29 00:33:11 +08:00
|
|
|
dialog.editors = genSearch(options.app, config, dialog.element.querySelector(".b3-dialog__body"), () => {
|
2023-04-28 22:08:57 +08:00
|
|
|
dialog.destroy({focus: "false"});
|
2022-11-28 00:39:51 +08:00
|
|
|
});
|
2023-11-01 17:02:42 +08:00
|
|
|
dialog.data = config;
|
2022-05-26 15:18:53 +08:00
|
|
|
};
|