siyuan/app/src/search/spread.ts

113 lines
4.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {getNotebookName, pathPosix} from "../util/pathName";
import {Constants} from "../constants";
import {Dialog} from "../dialog";
import {fetchSyncPost} from "../util/fetch";
import {focusByRange} from "../protyle/util/selection";
import {genSearch} from "./util";
import {App} from "../index";
export const openSearch = async (options: {
app: App,
hotkey: string,
key?: string,
notebookId?: string,
searchPath?: string
}) => {
const exitDialog = window.siyuan.dialogs.find((item) => {
if (item.element.querySelector("#searchList")) {
const lastKey = item.element.getAttribute("data-key");
const replaceHeaderElement = item.element.querySelectorAll(".search__header")[1];
if (lastKey !== options.hotkey && options.hotkey === window.siyuan.config.keymap.general.replace.custom && 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 === window.siyuan.config.keymap.general.globalSearch.custom) {
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 === window.siyuan.config.keymap.general.search.custom) {
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) {
return;
}
const localData = window.siyuan.storage[Constants.LOCAL_SEARCHDATA];
let hPath = "";
let idPath: string[] = [];
if (options.notebookId) {
hPath = getNotebookName(options.notebookId);
idPath.push(options.notebookId);
if (options.searchPath && options.searchPath !== "/") {
const response = await fetchSyncPost("/api/filetree/getHPathByPath", {
notebook: options.notebookId,
path: options.searchPath.endsWith(".sy") ? options.searchPath : options.searchPath + ".sy"
});
hPath = pathPosix().join(hPath, response.data);
idPath[0] = pathPosix().join(idPath[0], options.searchPath);
}
} else if (window.siyuan.config.keymap.general.globalSearch.custom === options.hotkey) {
if (localData.removed) {
hPath = "";
idPath = [];
} else {
hPath = localData.hPath;
idPath = localData.idPath;
}
}
let range: Range;
if (getSelection().rangeCount > 0) {
range = getSelection().getRangeAt(0);
}
const dialog = new Dialog({
content: "",
width: "80vw",
height: "90vh",
destroyCallback(options: IObject) {
if (range && !options) {
focusByRange(range);
}
if (edit) {
edit.destroy();
}
}
});
dialog.element.setAttribute("data-key", options.hotkey);
const edit = genSearch(options.app, {
removed: localData.removed,
k: options.key || localData.k,
r: localData.r,
hasReplace: options.hotkey === window.siyuan.config.keymap.general.replace.custom,
method: localData.method,
hPath,
idPath,
group: localData.group,
sort: localData.sort,
types: Object.assign({}, localData.types),
page: options.key ? 1 : localData.page
}, dialog.element.querySelector(".b3-dialog__body"), () => {
dialog.destroy({focus: "false"});
});
// 搜索面板层级需高于 201.protyle-hint 且小于205.block__popover
dialog.element.firstElementChild.setAttribute("style", "z-index:202"); // https://github.com/siyuan-note/siyuan/issues/3515
};