import {getAllModels} from "../layout/getAll"; /// #if !BROWSER import * as path from "path"; /// #endif import {Constants} from "../constants"; import {escapeAriaLabel, escapeGreat, escapeHtml} from "../util/escape"; import {fetchPost} from "../util/fetch"; import {openFile, openFileById} from "../editor/util"; import {showMessage} from "../dialog/message"; import {reloadProtyle} from "../protyle/util/reload"; import {MenuItem} from "../menus/Menu"; import { getDisplayName, getNotebookIcon, getNotebookName, movePathTo, pathPosix, showFileInFolder } from "../util/pathName"; import {Protyle} from "../protyle"; import {onGet} from "../protyle/util/onGet"; import {addLoading} from "../protyle/ui/initUI"; import {getIconByType} from "../editor/getIcon"; import {unicode2Emoji} from "../emoji"; import {hasClosestByClassName} from "../protyle/util/hasClosest"; import {isNotCtrl, setStorageVal, updateHotkeyTip} from "../protyle/util/compatibility"; import {newFileByName} from "../util/newFile"; import {filterMenu, getKeyByLiElement, initCriteriaMenu, moreMenu, queryMenu, saveCriterion} from "./menu"; import {App} from "../index"; import { assetFilterMenu, assetInputEvent, assetMethodMenu, assetMoreMenu, openSearchAsset, renderNextAssetMark, renderPreview, toggleAssetHistory } from "./assets"; import {resize} from "../protyle/util/resize"; export const toggleReplaceHistory = (replaceHistoryElement: Element, historyElement: Element, replaceInputElement: HTMLInputElement) => { if (replaceHistoryElement.classList.contains("fn__none")) { const list = window.siyuan.storage[Constants.LOCAL_SEARCHKEYS]; if (!list.replaceKeys || list.replaceKeys.length === 0) { return; } let html = ""; list.replaceKeys.forEach((s: string) => { if (s !== replaceInputElement.value && s) { html += `
${escapeHtml(s)}
`; } }); if (html === "") { return; } replaceHistoryElement.classList.remove("fn__none"); replaceHistoryElement.innerHTML = html; } else { replaceHistoryElement.classList.add("fn__none"); } historyElement.classList.add("fn__none"); }; export const toggleSearchHistory = (historyElement: Element, replaceHistoryElement: Element, searchInputElement: HTMLInputElement) => { if (historyElement.classList.contains("fn__none")) { const list = window.siyuan.storage[Constants.LOCAL_SEARCHKEYS]; if (!list.keys || list.keys.length === 0) { return; } let html = ""; list.keys.forEach((s: string) => { if (s !== searchInputElement.value && s) { html += `
${escapeHtml(s)}
`; } }); if (html === "") { return; } historyElement.classList.remove("fn__none"); historyElement.innerHTML = html; } else { historyElement.classList.add("fn__none"); } replaceHistoryElement.classList.add("fn__none"); }; const saveKeyList = (type: "keys" | "replaceKeys", value: string) => { let list: string[] = window.siyuan.storage[Constants.LOCAL_SEARCHKEYS][type]; list.splice(0, 0, value); list = Array.from(new Set(list)); if (list.length > window.siyuan.config.search.limit) { list.splice(window.siyuan.config.search.limit, list.length - window.siyuan.config.search.limit); } // new Set 后需重新赋值 window.siyuan.storage[Constants.LOCAL_SEARCHKEYS][type] = list; setStorageVal(Constants.LOCAL_SEARCHKEYS, window.siyuan.storage[Constants.LOCAL_SEARCHKEYS]); }; export const openGlobalSearch = (app: App, text: string, replace: boolean) => { text = text.trim(); const searchModel = getAllModels().search.find((item) => { item.parent.parent.switchTab(item.parent.headElement); item.updateSearch(text, replace); return true; }); if (searchModel) { return; } const localData = window.siyuan.storage[Constants.LOCAL_SEARCHDATA]; openFile({ app, searchData: { k: text, r: "", hasReplace: false, method: localData.method, hPath: "", idPath: [], group: localData.group, sort: localData.sort, types: Object.assign({}, localData.types), removed: localData.removed, page: 1 }, position: (window.siyuan.layout.centerLayout.children.length > 1 || window.innerWidth > 1024) ? "right" : undefined }); }; // closeCB 不存在为页签搜索 export const genSearch = (app: App, config: ISearchOption, element: Element, closeCB?: () => void) => { let methodText = window.siyuan.languages.keyword; if (config.method === 1) { methodText = window.siyuan.languages.querySyntax; } else if (config.method === 2) { methodText = "SQL"; } else if (config.method === 3) { methodText = window.siyuan.languages.regex; } let includeChild = true; let enableIncludeChild = false; config.idPath.forEach(item => { if (item.endsWith(".sy")) { includeChild = false; } if (item.split("/").length > 1) { enableIncludeChild = true; } }); const data = window.siyuan.storage[Constants.LOCAL_SEARCHKEYS]; element.innerHTML = `
${escapeHtml(config.hPath)}
↑/↓/PageUp/PageDown ${window.siyuan.languages.searchTip1} ${updateHotkeyTip(window.siyuan.config.keymap.general.newFile.custom)} ${window.siyuan.languages.new} Enter/Double Click ${window.siyuan.languages.searchTip2} Click ${window.siyuan.languages.searchTip3} ${updateHotkeyTip(window.siyuan.config.keymap.editor.general.insertRight.custom)}/${updateHotkeyTip("⌥Click")} ${window.siyuan.languages.searchTip4} Esc ${window.siyuan.languages.searchTip5}
`; const criteriaData: ISearchOption[] = []; initCriteriaMenu(element.querySelector("#criteria"), criteriaData, config); const searchPanelElement = element.querySelector("#searchList"); const searchInputElement = element.querySelector("#searchInput") as HTMLInputElement; const replaceInputElement = element.querySelector("#replaceInput") as HTMLInputElement; const replaceHistoryElement = element.querySelector("#replaceHistoryList"); const historyElement = element.querySelector("#searchHistoryList"); const edit = new Protyle(app, element.querySelector("#searchPreview") as HTMLElement, { blockId: "", render: { gutter: true, breadcrumbDocName: true }, }); if (closeCB) { if (data.layout === 1) { if (data.col) { edit.protyle.element.style.width = data.col; edit.protyle.element.classList.remove("fn__flex-1"); } } else { if (data.row) { edit.protyle.element.classList.remove("fn__flex-1"); edit.protyle.element.style.height = data.row; } } } else { if (data.layoutTab === 1) { if (data.colTab) { edit.protyle.element.style.width = data.colTab; edit.protyle.element.classList.remove("fn__flex-1"); } } else { if (data.rowTab) { edit.protyle.element.classList.remove("fn__flex-1"); edit.protyle.element.style.height = data.rowTab; } } } let clickTimeout: number; let lastClickTime = new Date().getTime(); searchInputElement.value = config.k || ""; replaceInputElement.value = config.r || ""; searchInputElement.select(); const dragElement = element.querySelector(".search__drag"); dragElement.addEventListener("mousedown", (event: MouseEvent) => { const documentSelf = document; const nextElement = dragElement.nextElementSibling as HTMLElement; const previousElement = dragElement.previousElementSibling as HTMLElement; const direction = window.siyuan.storage[Constants.LOCAL_SEARCHKEYS][closeCB ? "layout" : "layoutTab"] === 1 ? "lr" : "tb"; const x = event[direction === "lr" ? "clientX" : "clientY"]; const previousSize = direction === "lr" ? previousElement.clientWidth : previousElement.clientHeight; const nextSize = direction === "lr" ? nextElement.clientWidth : nextElement.clientHeight; nextElement.classList.remove("fn__flex-1"); nextElement.style[direction === "lr" ? "width" : "height"] = nextSize + "px"; documentSelf.onmousemove = (moveEvent: MouseEvent) => { moveEvent.preventDefault(); moveEvent.stopPropagation(); const previousNowSize = (previousSize + (moveEvent[direction === "lr" ? "clientX" : "clientY"] - x)); const nextNowSize = (nextSize - (moveEvent[direction === "lr" ? "clientX" : "clientY"] - x)); if (previousNowSize < 120 || nextNowSize < 120) { return; } nextElement.style[direction === "lr" ? "width" : "height"] = nextNowSize + "px"; }; documentSelf.onmouseup = () => { documentSelf.onmousemove = null; documentSelf.onmouseup = null; documentSelf.ondragstart = null; documentSelf.onselectstart = null; documentSelf.onselect = null; window.siyuan.storage[Constants.LOCAL_SEARCHKEYS][direction === "lr" ? (closeCB ? "col" : "colTab") : (closeCB ? "row" : "rowTab")] = nextElement[direction === "lr" ? "clientWidth" : "clientHeight"] + "px"; setStorageVal(Constants.LOCAL_SEARCHKEYS, window.siyuan.storage[Constants.LOCAL_SEARCHKEYS]); if (direction === "lr") { resize(edit.protyle); } }; }); const localSearch = window.siyuan.storage[Constants.LOCAL_SEARCHASSET] as ISearchAssetOption; const assetsElement = element.querySelector("#searchAssets"); element.addEventListener("click", (event: MouseEvent) => { let target = event.target as HTMLElement; const searchPathInputElement = element.querySelector("#searchPathInput"); while (target && !target.isSameNode(element)) { const type = target.getAttribute("data-type"); if (type === "removeCriterion") { updateConfig(element, { removed: true, sort: 0, group: 0, hasReplace: false, method: 0, hPath: "", idPath: [], k: "", r: "", page: 1, types: { document: window.siyuan.config.search.document, heading: window.siyuan.config.search.heading, list: window.siyuan.config.search.list, listItem: window.siyuan.config.search.listItem, codeBlock: window.siyuan.config.search.codeBlock, htmlBlock: window.siyuan.config.search.htmlBlock, mathBlock: window.siyuan.config.search.mathBlock, table: window.siyuan.config.search.table, blockquote: window.siyuan.config.search.blockquote, superBlock: window.siyuan.config.search.superBlock, paragraph: window.siyuan.config.search.paragraph, embedBlock: window.siyuan.config.search.embedBlock, databaseBlock: window.siyuan.config.search.databaseBlock, } }, config, edit); element.querySelector(".b3-chip--current")?.classList.remove("b3-chip--current"); event.stopPropagation(); event.preventDefault(); break; } else if (type === "saveCriterion") { saveCriterion(config, criteriaData, element); event.stopPropagation(); event.preventDefault(); break; } else if (type === "next") { if (!target.getAttribute("disabled")) { if (config.page < parseInt(target.parentElement.querySelector("#searchResult").getAttribute("data-pagecount"))) { config.page++; inputEvent(element, config, edit); } } event.stopPropagation(); event.preventDefault(); break; } else if (type === "previous") { if (!target.getAttribute("disabled")) { if (config.page > 1) { config.page--; inputEvent(element, config, edit); } } event.stopPropagation(); event.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()) { updateConfig(element, item, config, edit); return true; } }); event.stopPropagation(); event.preventDefault(); break; } else if (target.classList.contains("b3-chip__close") && type === "remove-criteria") { const name = target.parentElement.textContent; fetchPost("/api/storage/removeCriterion", {name}); criteriaData.find((item, index) => { if (item.name === name) { criteriaData.splice(index, 1); return true; } }); target.parentElement.remove(); event.stopPropagation(); event.preventDefault(); break; } else if (target.classList.contains("search__rmpath")) { config.idPath = []; config.hPath = ""; config.page = 1; searchPathInputElement.innerHTML = config.hPath; searchPathInputElement.setAttribute("aria-label", ""); inputEvent(element, config, edit, true); const includeElement = element.querySelector("#searchInclude"); includeElement.classList.remove("b3-button--cancel"); includeElement.setAttribute("disabled", "disabled"); event.stopPropagation(); event.preventDefault(); break; } else if (target.id === "searchExpand") { Array.from(searchPanelElement.children).forEach(item => { if (item.classList.contains("b3-list-item")) { item.querySelector(".b3-list-item__arrow").classList.add("b3-list-item__arrow--open"); item.nextElementSibling.classList.remove("fn__none"); } }); event.stopPropagation(); event.preventDefault(); break; } else if (target.id === "searchCollapse") { Array.from(searchPanelElement.children).forEach(item => { if (item.classList.contains("b3-list-item")) { item.querySelector(".b3-list-item__arrow").classList.remove("b3-list-item__arrow--open"); item.nextElementSibling.classList.add("fn__none"); } }); event.stopPropagation(); event.preventDefault(); break; } else if (target.id === "searchPath") { movePathTo((toPath, toNotebook) => { fetchPost("/api/filetree/getHPathsByPaths", {paths: toPath}, (response) => { config.idPath = []; const hPathList: string[] = []; let enableIncludeChild = false; toPath.forEach((item, index) => { if (item === "/") { config.idPath.push(toNotebook[index]); hPathList.push(getNotebookName(toNotebook[index])); } else { enableIncludeChild = true; config.idPath.push(pathPosix().join(toNotebook[index], item.replace(".sy", ""))); } }); if (response.data) { hPathList.push(...response.data); } config.hPath = hPathList.join(" "); config.page = 1; searchPathInputElement.innerHTML = `${escapeHtml(config.hPath)}`; searchPathInputElement.setAttribute("aria-label", escapeHtml(config.hPath)); const includeElement = element.querySelector("#searchInclude"); includeElement.classList.remove("b3-button--cancel"); if (enableIncludeChild) { includeElement.removeAttribute("disabled"); } else { includeElement.setAttribute("disabled", "disabled"); } inputEvent(element, config, edit, true); }); }, [], undefined, window.siyuan.languages.specifyPath); event.stopPropagation(); event.preventDefault(); break; } else if (target.id === "searchInclude") { target.classList.toggle("b3-button--cancel"); if (target.classList.contains("b3-button--cancel")) { config.idPath.forEach((item, index) => { if (!item.endsWith(".sy") && item.split("/").length > 1) { config.idPath[index] = item + ".sy"; } }); } else { config.idPath.forEach((item, index) => { if (item.endsWith(".sy")) { config.idPath[index] = item.replace(".sy", ""); } }); } config.page = 1; inputEvent(element, config, edit, true); event.stopPropagation(); event.preventDefault(); break; } else if (target.id === "searchReplace") { // ctrl+P 不需要保存 config.hasReplace = !config.hasReplace; element.querySelector("#replaceHistoryBtn").parentElement.classList.toggle("fn__none"); event.stopPropagation(); event.preventDefault(); break; } else if (target.id === "searchAsset") { openSearchAsset(assetsElement, !closeCB); event.stopPropagation(); event.preventDefault(); break; } else if (target.id === "searchAssetClose") { window.siyuan.menus.menu.remove(); assetsElement.classList.add("fn__none"); assetsElement.previousElementSibling.classList.remove("fn__none"); searchInputElement.select(); event.stopPropagation(); event.preventDefault(); break; } else if (target.id === "searchOpen") { config.k = searchInputElement.value; config.r = replaceInputElement.value; openFile({ app, searchData: config, position: (window.siyuan.layout.centerLayout.children.length > 1 || window.innerWidth > 1024) ? "right" : undefined }); if (closeCB) { closeCB(); } event.stopPropagation(); event.preventDefault(); break; } else if (target.id === "searchRefresh") { inputEvent(element, config, edit); event.stopPropagation(); event.preventDefault(); break; } else if (target.id === "searchMore") { moreMenu(config, criteriaData, element, () => { config.page = 1; inputEvent(element, config, edit, true); }, () => { updateConfig(element, { removed: true, sort: 0, group: 0, hasReplace: false, method: 0, hPath: "", idPath: [], k: "", r: "", page: 1, types: { document: window.siyuan.config.search.document, heading: window.siyuan.config.search.heading, list: window.siyuan.config.search.list, listItem: window.siyuan.config.search.listItem, codeBlock: window.siyuan.config.search.codeBlock, htmlBlock: window.siyuan.config.search.htmlBlock, mathBlock: window.siyuan.config.search.mathBlock, table: window.siyuan.config.search.table, blockquote: window.siyuan.config.search.blockquote, superBlock: window.siyuan.config.search.superBlock, paragraph: window.siyuan.config.search.paragraph, embedBlock: window.siyuan.config.search.embedBlock, databaseBlock: window.siyuan.config.search.databaseBlock, } }, config, edit); element.querySelector("#criteria .b3-chip--current")?.classList.remove("b3-chip--current"); }, () => { const localData = window.siyuan.storage[Constants.LOCAL_SEARCHKEYS]; const isPopover = hasClosestByClassName(element, "b3-dialog__container"); window.siyuan.menus.menu.append(new MenuItem({ iconHTML: Constants.ZWSP, label: window.siyuan.languages.layout, type: "submenu", submenu: [{ iconHTML: Constants.ZWSP, label: window.siyuan.languages.topBottomLayout, current: isPopover ? localData.layout === 0 : localData.layoutTab === 0, click() { element.querySelector(".search__layout").classList.remove("search__layout--row"); edit.protyle.element.style.width = ""; if ((isPopover && localData.row) || (!isPopover && localData.rowTab)) { edit.protyle.element.style.height = isPopover ? localData.row : localData.rowTab; edit.protyle.element.classList.remove("fn__flex-1"); } else { edit.protyle.element.classList.add("fn__flex-1"); } resize(edit.protyle); if (isPopover) { localData.layout = 0; } else { localData.layoutTab = 0; } setStorageVal(Constants.LOCAL_SEARCHKEYS, window.siyuan.storage[Constants.LOCAL_SEARCHKEYS]); } }, { iconHTML: Constants.ZWSP, label: window.siyuan.languages.leftRightLayout, current: isPopover ? localData.layout === 1 : localData.layoutTab === 1, click() { element.querySelector(".search__layout").classList.add("search__layout--row"); edit.protyle.element.style.height = ""; if ((isPopover && localData.col) || (!isPopover && localData.colTab)) { edit.protyle.element.style.width = isPopover ? localData.col : localData.colTab; edit.protyle.element.classList.remove("fn__flex-1"); } else { edit.protyle.element.classList.add("fn__flex-1"); } resize(edit.protyle); if (isPopover) { localData.layout = 1; } else { localData.layoutTab = 1; } setStorageVal(Constants.LOCAL_SEARCHKEYS, window.siyuan.storage[Constants.LOCAL_SEARCHKEYS]); } }] }).element); }); const rect = target.getBoundingClientRect(); window.siyuan.menus.menu.popup({x: rect.right, y: rect.bottom, isLeft: true}); event.stopPropagation(); event.preventDefault(); break; } else if (target.id === "searchFilter") { window.siyuan.menus.menu.remove(); filterMenu(config, () => { config.page = 1; inputEvent(element, config, edit, true); }); event.stopPropagation(); event.preventDefault(); break; } else if (type === "assetPrevious") { if (!target.getAttribute("disabled")) { let currentPage = parseInt(assetsElement.querySelector("#searchAssetResult .fn__flex-center").textContent.split("/")[0]); if (currentPage > 1) { currentPage--; assetInputEvent(assetsElement, localSearch, currentPage); } } event.stopPropagation(); event.preventDefault(); break; } else if (type === "assetNext") { if (!target.getAttribute("disabled")) { let currentPage = parseInt(assetsElement.querySelector("#searchAssetResult .fn__flex-center").textContent.split("/")[0]); if (currentPage < parseInt(assetsElement.querySelector("#searchAssetResult .fn__flex-center").textContent.split("/")[1])) { currentPage++; assetInputEvent(assetsElement, localSearch, currentPage); } } event.stopPropagation(); event.preventDefault(); break; } else if (target.id === "assetMore") { assetMoreMenu(target, assetsElement, () => { assetInputEvent(assetsElement); setStorageVal(Constants.LOCAL_SEARCHASSET, localSearch); }); event.stopPropagation(); event.preventDefault(); break; } else if (target.id === "assetFilter") { assetFilterMenu(assetsElement); event.stopPropagation(); event.preventDefault(); break; } else if (target.id === "assetSyntaxCheck") { assetMethodMenu(target, () => { element.querySelector("#assetSyntaxCheck").setAttribute("aria-label", getQueryTip(localSearch.method)); assetInputEvent(assetsElement, localSearch); setStorageVal(Constants.LOCAL_SEARCHASSET, localSearch); }); event.stopPropagation(); event.preventDefault(); break; } else if (target.id === "searchSyntaxCheck") { queryMenu(config, () => { element.querySelector("#searchSyntaxCheck").setAttribute("aria-label", getQueryTip(config.method)); config.page = 1; inputEvent(element, config, edit, true); }); const rect = target.getBoundingClientRect(); window.siyuan.menus.menu.popup({x: rect.right, y: rect.bottom, isLeft: true}); event.stopPropagation(); event.preventDefault(); break; } else if (target.id === "searchHistoryBtn") { toggleSearchHistory(historyElement, replaceHistoryElement, searchInputElement); event.stopPropagation(); event.preventDefault(); return; } else if (target.id === "assetHistoryBtn") { toggleAssetHistory(target.nextElementSibling.nextElementSibling, target.nextElementSibling as HTMLInputElement); event.stopPropagation(); event.preventDefault(); return; } else if (target.id === "replaceHistoryBtn") { toggleReplaceHistory(replaceHistoryElement, historyElement, replaceInputElement); event.stopPropagation(); event.preventDefault(); return; } else if (target.id === "replaceAllBtn") { replace(element, config, edit, true); event.stopPropagation(); event.preventDefault(); break; } else if (type === "assetRefresh") { assetInputEvent(assetsElement); event.stopPropagation(); event.preventDefault(); break; } else if (target.id === "replaceBtn") { replace(element, config, edit, false); event.stopPropagation(); event.preventDefault(); break; } else if (target.classList.contains("b3-list-item__toggle")) { target.parentElement.nextElementSibling.classList.toggle("fn__none"); target.firstElementChild.classList.toggle("b3-list-item__arrow--open"); event.stopPropagation(); event.preventDefault(); break; } else if (target.classList.contains("b3-list-item")) { const searchAssetInputElement = element.querySelector("#searchAssetInput") as HTMLInputElement; if (target.parentElement.id === "searchHistoryList") { searchInputElement.value = target.textContent; config.page = 1; inputEvent(element, config, edit, true); } else if (target.parentElement.id === "searchAssetHistoryList") { searchAssetInputElement.value = target.textContent; assetInputEvent(assetsElement); } else if (target.parentElement.id === "replaceHistoryList") { replaceInputElement.value = target.textContent; replaceHistoryElement.classList.add("fn__none"); } else if (type === "search-new") { newFileByName(app, searchInputElement.value); } else if (type === "search-item") { const isAsset = target.dataset.id; let isClick = event.detail === 1; let isDblClick = event.detail === 2; /// #if BROWSER const newDate = new Date().getTime(); isClick = newDate - lastClickTime > Constants.TIMEOUT_DBLCLICK; isDblClick = !isClick; lastClickTime = newDate; /// #endif if (isClick) { clickTimeout = window.setTimeout(() => { if (isAsset) { if (!target.classList.contains("b3-list-item--focus")) { assetsElement.querySelector(".b3-list-item--focus").classList.remove("b3-list-item--focus"); target.classList.add("b3-list-item--focus"); renderPreview(element.querySelector("#searchAssetPreview"), target.dataset.id, searchAssetInputElement.value, window.siyuan.storage[Constants.LOCAL_SEARCHASSET].method); searchAssetInputElement.focus(); } else if (target.classList.contains("b3-list-item--focus")) { renderNextAssetMark(element.querySelector("#searchAssetPreview")); searchAssetInputElement.focus(); } } else { if (event.altKey) { const id = target.getAttribute("data-node-id"); fetchPost("/api/block/checkBlockFold", {id}, (foldResponse) => { openFileById({ app, id, action: foldResponse.data ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL] : (id === target.getAttribute("data-root-id") ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ROOTSCROLL] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT]), zoomIn: foldResponse.data, position: "right" }); if (closeCB) { closeCB(); } }); } else if (!target.classList.contains("b3-list-item--focus")) { searchPanelElement.querySelector(".b3-list-item--focus").classList.remove("b3-list-item--focus"); target.classList.add("b3-list-item--focus"); getArticle({ edit, id: target.getAttribute("data-node-id"), config, value: searchInputElement.value, }); searchInputElement.focus(); } else if (target.classList.contains("b3-list-item--focus")) { renderNextSearchMark({ edit, id: target.getAttribute("data-node-id"), target, }); searchInputElement.focus(); } } }, Constants.TIMEOUT_DBLCLICK); } else if (isDblClick && isNotCtrl(event)) { clearTimeout(clickTimeout); if (isAsset) { /// #if !BROWSER showFileInFolder(path.join(window.siyuan.config.system.dataDir, target.lastElementChild.getAttribute("aria-label"))); /// #endif } else { const id = target.getAttribute("data-node-id"); fetchPost("/api/block/checkBlockFold", {id}, (foldResponse) => { openFileById({ app, id, action: foldResponse.data ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL] : (id === target.getAttribute("data-root-id") ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ROOTSCROLL] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT]), zoomIn: foldResponse.data }); if (closeCB) { closeCB(); } }); } } window.siyuan.menus.menu.remove(); } else if (target.querySelector(".b3-list-item__toggle")) { target.nextElementSibling.classList.toggle("fn__none"); target.firstElementChild.firstElementChild.classList.toggle("b3-list-item__arrow--open"); } event.stopPropagation(); event.preventDefault(); break; } target = target.parentElement; } historyElement.classList.add("fn__none"); replaceHistoryElement.classList.add("fn__none"); element.querySelector("#searchAssetHistoryList")?.classList.add("fn__none"); }, false); searchInputElement.addEventListener("compositionend", (event: InputEvent) => { config.page = 1; if (event.isComposing) { return; } inputEvent(element, config, edit, true); }); searchInputElement.addEventListener("input", (event: InputEvent) => { config.page = 1; if (event.isComposing) { return; } inputEvent(element, config, edit, true); }); searchInputElement.addEventListener("blur", () => { if (config.removed) { config.k = searchInputElement.value; window.siyuan.storage[Constants.LOCAL_SEARCHDATA] = Object.assign({}, config); setStorageVal(Constants.LOCAL_SEARCHDATA, window.siyuan.storage[Constants.LOCAL_SEARCHDATA]); } saveKeyList("keys", searchInputElement.value); }); inputEvent(element, config, edit); return edit; }; export const getQueryTip = (method: number) => { let methodTip = window.siyuan.languages.searchMethod + " "; switch (method) { case 0: methodTip += window.siyuan.languages.keyword; break; case 1: methodTip += window.siyuan.languages.querySyntax; break; case 2: methodTip += "SQL"; break; case 3: methodTip += window.siyuan.languages.regex; break; } return methodTip; }; const updateConfig = (element: Element, item: ISearchOption, config: ISearchOption, edit: Protyle) => { const dialogElement = hasClosestByClassName(element, "b3-dialog--open"); if (dialogElement && dialogElement.getAttribute("data-key") === window.siyuan.config.keymap.general.search.custom) { // https://github.com/siyuan-note/siyuan/issues/6828 item.hPath = config.hPath; item.idPath = config.idPath.join(",").split(","); } if (config.hasReplace !== item.hasReplace) { if (item.hasReplace) { element.querySelector("#replaceHistoryBtn").parentElement.classList.remove("fn__none"); } else { element.querySelector("#replaceHistoryBtn").parentElement.classList.add("fn__none"); } } const searchPathInputElement = element.querySelector("#searchPathInput"); if (item.hPath) { searchPathInputElement.innerHTML = `${escapeHtml(item.hPath)}`; searchPathInputElement.setAttribute("aria-label", escapeHtml(item.hPath)); } else { searchPathInputElement.innerHTML = ""; searchPathInputElement.setAttribute("aria-label", ""); } if (config.group !== item.group) { if (item.group === 0) { element.querySelector("#searchExpand").parentElement.classList.add("fn__none"); } else { element.querySelector("#searchExpand").parentElement.classList.remove("fn__none"); } } let includeChild = true; let enableIncludeChild = false; item.idPath.forEach(item => { if (item.endsWith(".sy")) { includeChild = false; } if (item.split("/").length > 1) { enableIncludeChild = true; } }); const searchIncludeElement = element.querySelector("#searchInclude"); if (includeChild) { searchIncludeElement.classList.remove("b3-button--cancel"); } else { searchIncludeElement.classList.add("b3-button--cancel"); } if (enableIncludeChild) { searchIncludeElement.removeAttribute("disabled"); } else { searchIncludeElement.setAttribute("disabled", "disabled"); } (element.querySelector("#searchInput") as HTMLInputElement).value = item.k; (element.querySelector("#replaceInput") as HTMLInputElement).value = item.r; element.querySelector("#searchSyntaxCheck").setAttribute("aria-label", getQueryTip(item.method)); Object.assign(config, item); window.siyuan.storage[Constants.LOCAL_SEARCHDATA] = Object.assign({}, config); setStorageVal(Constants.LOCAL_SEARCHDATA, window.siyuan.storage[Constants.LOCAL_SEARCHDATA]); inputEvent(element, config, edit); window.siyuan.menus.menu.remove(); }; const renderNextSearchMark = (options: { id: string, edit: Protyle, target: Element, }) => { let matchElement; const allMatchElements = Array.from(options.edit.protyle.wysiwyg.element.querySelectorAll(`div[data-node-id="${options.id}"] span[data-type~="search-mark"]`)); allMatchElements.find((item, itemIndex) => { if (item.classList.contains("search-mark--hl")) { item.classList.remove("search-mark--hl"); matchElement = allMatchElements[itemIndex + 1]; return; } }); if (!matchElement) { matchElement = allMatchElements[0]; } if (matchElement) { matchElement.classList.add("search-mark--hl"); const contentRect = options.edit.protyle.contentElement.getBoundingClientRect(); options.edit.protyle.contentElement.scrollTop = options.edit.protyle.contentElement.scrollTop + matchElement.getBoundingClientRect().top - contentRect.top - contentRect.height / 2; } }; export const getArticle = (options: { id: string, config: ISearchOption, edit: Protyle value: string, }) => { fetchPost("/api/block/checkBlockFold", {id: options.id}, (foldResponse) => { options.edit.protyle.scroll.lastScrollTop = 0; addLoading(options.edit.protyle); fetchPost("/api/filetree/getDoc", { id: options.id, query: options.value, queryMethod: options.config.method, queryTypes: options.config.types, mode: foldResponse.data ? 0 : 3, size: foldResponse.data ? Constants.SIZE_GET_MAX : window.siyuan.config.editor.dynamicLoadBlocks, zoom: foldResponse.data, }, getResponse => { onGet({ data: getResponse, protyle: options.edit.protyle, action: foldResponse.data ? [Constants.CB_GET_ALL, Constants.CB_GET_HTML] : [Constants.CB_GET_HL, Constants.CB_GET_HTML], }); const matchElement = options.edit.protyle.wysiwyg.element.querySelector(`div[data-node-id="${options.id}"] span[data-type~="search-mark"]`); if (matchElement) { matchElement.classList.add("search-mark--hl"); const contentRect = options.edit.protyle.contentElement.getBoundingClientRect(); options.edit.protyle.contentElement.scrollTop = options.edit.protyle.contentElement.scrollTop + matchElement.getBoundingClientRect().top - contentRect.top - contentRect.height / 2; } }); }); }; export const replace = (element: Element, config: ISearchOption, edit: Protyle, isAll: boolean) => { if (config.method === 1 || config.method === 2) { showMessage(window.siyuan.languages._kernel[132]); return; } const searchPanelElement = element.querySelector("#searchList"); const replaceInputElement = element.querySelector("#replaceInput") as HTMLInputElement; const searchInputElement = element.querySelector("#searchInput") as HTMLInputElement; const loadElement = replaceInputElement.nextElementSibling; if (!loadElement.classList.contains("fn__none")) { return; } saveKeyList("replaceKeys", replaceInputElement.value); let currentList: HTMLElement = searchPanelElement.querySelector(".b3-list-item--focus"); if (!currentList || currentList.dataset.type === "search-new") { return; } loadElement.classList.remove("fn__none"); fetchPost("/api/search/findReplace", { k: config.method === 0 ? getKeyByLiElement(currentList) : searchInputElement.value, r: replaceInputElement.value, method: config.method, types: config.types, paths: config.idPath || [], groupBy: config.group, orderBy: config.sort, page: config.page, ids: isAll ? [] : [currentList.getAttribute("data-node-id")] }, (response) => { loadElement.classList.add("fn__none"); if (response.code === 1) { showMessage(response.msg); return; } if (isAll) { return; } const rootId = currentList.getAttribute("data-root-id"); getAllModels().editor.forEach(item => { if (rootId === item.editor.protyle.block.rootID) { reloadProtyle(item.editor.protyle, false); } }); if (currentList.nextElementSibling) { currentList.nextElementSibling.classList.add("b3-list-item--focus"); } else if (currentList.previousElementSibling) { currentList.previousElementSibling.classList.add("b3-list-item--focus"); } if (config.group === 1) { if (currentList.nextElementSibling || currentList.previousElementSibling) { currentList.remove(); } else { const nextDocElement = currentList.parentElement.nextElementSibling || currentList.parentElement.previousElementSibling.previousElementSibling?.previousElementSibling; if (nextDocElement) { nextDocElement.nextElementSibling.firstElementChild.classList.add("b3-list-item--focus"); nextDocElement.nextElementSibling.classList.remove("fn__none"); nextDocElement.firstElementChild.firstElementChild.classList.add("b3-list-item__arrow--open"); } currentList.parentElement.previousElementSibling.remove(); currentList.parentElement.remove(); } } else { currentList.remove(); } currentList = searchPanelElement.querySelector(".b3-list-item--focus"); if (!currentList) { searchPanelElement.innerHTML = `
${window.siyuan.languages.emptyContent}
`; edit.protyle.element.classList.add("fn__none"); element.querySelector(".search__drag").classList.add("fn__none"); return; } if (searchPanelElement.scrollTop < currentList.offsetTop - searchPanelElement.clientHeight + 30 || searchPanelElement.scrollTop > currentList.offsetTop) { searchPanelElement.scrollTop = currentList.offsetTop - searchPanelElement.clientHeight + 30; } getArticle({ edit, id: currentList.getAttribute("data-node-id"), config, value: searchInputElement.value, }); }); }; export const inputEvent = (element: Element, config: ISearchOption, edit: Protyle, rmCurrentCriteria = false) => { let inputTimeout = parseInt(element.getAttribute("data-timeout") || "0"); clearTimeout(inputTimeout); inputTimeout = window.setTimeout(() => { if (rmCurrentCriteria) { element.querySelector("#criteria .b3-chip--current")?.classList.remove("b3-chip--current"); } const searchInputElement = element.querySelector("#searchInput") as HTMLInputElement; const loadingElement = element.querySelector(".fn__loading--top"); loadingElement.classList.remove("fn__none"); const inputValue = searchInputElement.value; element.querySelector("#searchList").scrollTo(0, 0); const previousElement = element.querySelector('[data-type="previous"]'); const nextElement = element.querySelector('[data-type="next"]'); edit.protyle?.app.plugins.forEach(item => { item.eventBus.emit("input-search", { protyle: edit, config, searchElement: searchInputElement, }); }); const searchResultElement = element.querySelector("#searchResult"); if (inputValue === "" && (!config.idPath || config.idPath.length === 0)) { fetchPost("/api/block/getRecentUpdatedBlocks", {}, (response) => { onSearch(response.data, edit, element, config); loadingElement.classList.add("fn__none"); searchResultElement.innerHTML = ""; previousElement.setAttribute("disabled", "true"); nextElement.setAttribute("disabled", "true"); }); } else { if (config.page > 1) { previousElement.removeAttribute("disabled"); } else { previousElement.setAttribute("disabled", "disabled"); } fetchPost("/api/search/fullTextSearchBlock", { query: inputValue, method: config.method, types: config.types, paths: config.idPath || [], groupBy: config.group, orderBy: config.sort, page: config.page || 1, }, (response) => { if (!config.page) { config.page = 1; } if (config.page < response.data.pageCount) { nextElement.removeAttribute("disabled"); } else { nextElement.setAttribute("disabled", "disabled"); } onSearch(response.data.blocks, edit, element, config); searchResultElement.innerHTML = `${config.page}/${response.data.pageCount || 1} ${window.siyuan.languages.findInDoc.replace("${x}", response.data.matchedRootCount).replace("${y}", response.data.matchedBlockCount)}`; loadingElement.classList.add("fn__none"); searchResultElement.setAttribute("data-pagecount", response.data.pageCount || 1); }); } }, Constants.TIMEOUT_INPUT); element.setAttribute("data-timeout", inputTimeout.toString()); }; const getAttr = (block: IBlock) => { let attrHTML = ""; if (block.name) { attrHTML += `${block.name}`; } if (block.alias) { attrHTML += `${block.alias}`; } if (block.memo) { attrHTML += `${block.memo}`; } return attrHTML; }; const onSearch = (data: IBlock[], edit: Protyle, element: Element, config: ISearchOption) => { let resultHTML = ""; data.forEach((item, index) => { const title = getNotebookName(item.box) + getDisplayName(item.hPath, false); if (item.children) { resultHTML += `
${unicode2Emoji(getNotebookIcon(item.box) || Constants.SIYUAN_IMAGE_NOTE, "b3-list-item__graphic", true)} ${escapeGreat(title)}
`; item.children.forEach((childItem, childIndex) => { resultHTML += `
${unicode2Emoji(childItem.ial.icon, "b3-list-item__graphic", true)} ${childItem.content} ${getAttr(childItem)}
`; }); resultHTML += "
"; } else { resultHTML += `
${unicode2Emoji(item.ial.icon, "b3-list-item__graphic", true)} ${item.content} ${getAttr(item)} ${escapeGreat(title)}
`; } }); if (data[0]) { edit.protyle.element.classList.remove("fn__none"); element.querySelector(".search__drag").classList.remove("fn__none"); const searchInputElement = element.querySelector("#searchInput") as HTMLInputElement; if (data[0].children) { getArticle({ edit, id: data[0].children[0].id, config, value: searchInputElement.value, }); } else { getArticle({ edit, id: data[0].id, config, value: searchInputElement.value, }); } } else { edit.protyle.element.classList.add("fn__none"); element.querySelector(".search__drag").classList.add("fn__none"); } element.querySelector("#searchList").innerHTML = resultHTML || `
${window.siyuan.languages.newFile} ${(element.querySelector("#searchInput") as HTMLInputElement).value} ${window.siyuan.languages.enterNew}
${window.siyuan.languages.enterNewTip}
`; };