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, replaceFilterMenu, saveCriterion } from "./menu"; import {App} from "../index"; import { assetFilterMenu, assetInputEvent, assetMethodMenu, assetMoreMenu, openSearchAsset, renderNextAssetMark, renderPreview, toggleAssetHistory } from "./assets"; import {resize} from "../protyle/util/resize"; import {Menu} from "../plugin/Menu"; import {addClearButton} from "../util/addClearButton"; import {checkFold} from "../util/noRelyPCFunction"; import {getUnRefList, openSearchUnRef, unRefMoreMenu} from "./unRef"; import {getDefaultType} from "./getDefault"; export const toggleReplaceHistory = (searchElement: Element) => { const list = window.siyuan.storage[Constants.LOCAL_SEARCHKEYS]; if (!list.replaceKeys || list.replaceKeys.length === 0) { return; } const menu = new Menu("search-replace-history"); if (menu.isOpen) { return; } menu.element.classList.add("b3-menu--list"); menu.addItem({ iconHTML: "", label: window.siyuan.languages.clearHistory, click() { window.siyuan.storage[Constants.LOCAL_SEARCHKEYS].replaceKeys = []; setStorageVal(Constants.LOCAL_SEARCHKEYS, window.siyuan.storage[Constants.LOCAL_SEARCHKEYS]); } }); const separatorElement = menu.addSeparator(1); let current = true; const replaceInputElement = searchElement.querySelector("#replaceInput") as HTMLInputElement; list.replaceKeys.forEach((s: string) => { if (s !== replaceInputElement.value && s) { const menuItem = menu.addItem({ iconHTML: "", label: escapeHtml(s), action: "iconCloseRound", bind(element) { element.addEventListener("click", (itemEvent) => { if (hasClosestByClassName(itemEvent.target as Element, "b3-menu__action")) { list.replaceKeys.find((item: string, index: number) => { if (item === s) { list.replaceKeys.splice(index, 1); return true; } }); window.siyuan.storage[Constants.LOCAL_SEARCHKEYS].replaceKeys = list.replaceKeys; setStorageVal(Constants.LOCAL_SEARCHKEYS, window.siyuan.storage[Constants.LOCAL_SEARCHKEYS]); if (element.previousElementSibling?.classList.contains("b3-menu__separator") && !element.nextElementSibling) { window.siyuan.menus.menu.remove(); } else { element.remove(); } } else { replaceInputElement.value = element.textContent; window.siyuan.menus.menu.remove(); } itemEvent.preventDefault(); itemEvent.stopPropagation(); }); } }); if (current) { menuItem.classList.add("b3-menu__item--current"); } current = false; } }); if (current) { separatorElement.remove(); } const rect = replaceInputElement.getBoundingClientRect(); menu.open({ x: rect.left, y: rect.bottom }); }; export const toggleSearchHistory = (searchElement: Element, config: Config.IUILayoutTabSearchConfig, edit: Protyle) => { const list = window.siyuan.storage[Constants.LOCAL_SEARCHKEYS]; if (!list.keys || list.keys.length === 0) { return; } const menu = new Menu("search-history"); if (menu.isOpen) { return; } menu.element.classList.add("b3-menu--list"); menu.addItem({ iconHTML: "", label: window.siyuan.languages.clearHistory, click() { window.siyuan.storage[Constants.LOCAL_SEARCHKEYS].keys = []; setStorageVal(Constants.LOCAL_SEARCHKEYS, window.siyuan.storage[Constants.LOCAL_SEARCHKEYS]); } }); const separatorElement = menu.addSeparator(1); let current = true; const searchInputElement = searchElement.querySelector("#searchInput") as HTMLInputElement; list.keys.forEach((s: string) => { if (s !== searchInputElement.value && s) { const menuItem = menu.addItem({ iconHTML: "", label: escapeHtml(s), action: "iconCloseRound", bind(element) { element.addEventListener("click", (itemEvent) => { if (hasClosestByClassName(itemEvent.target as Element, "b3-menu__action")) { list.keys.find((item: string, index: number) => { if (item === s) { list.keys.splice(index, 1); return true; } }); window.siyuan.storage[Constants.LOCAL_SEARCHKEYS].keys = list.keys; setStorageVal(Constants.LOCAL_SEARCHKEYS, window.siyuan.storage[Constants.LOCAL_SEARCHKEYS]); if (element.previousElementSibling?.classList.contains("b3-menu__separator") && !element.nextElementSibling) { window.siyuan.menus.menu.remove(); } else { element.remove(); } } else { searchInputElement.value = element.textContent; config.page = 1; inputEvent(searchElement, config, edit, true); window.siyuan.menus.menu.remove(); } itemEvent.preventDefault(); itemEvent.stopPropagation(); }); } }); if (current) { menuItem.classList.add("b3-menu__item--current"); } current = false; } }); if (current) { separatorElement.remove(); } const rect = searchInputElement.getBoundingClientRect(); menu.open({ x: rect.left, y: rect.bottom }); }; 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), replaceTypes: Object.assign({}, localData.replaceTypes), 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: Config.IUILayoutTabSearchConfig, 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]; const unRefLocal = window.siyuan.storage[Constants.LOCAL_SEARCHUNREF]; element.innerHTML = `
${escapeHtml(config.hPath)}
↑/↓/PageUp/PageDown ${window.siyuan.languages.searchTip1} ${updateHotkeyTip(window.siyuan.config.keymap.general.newFile.custom)} ${window.siyuan.languages.new} ${window.siyuan.languages.enterKey}/${window.siyuan.languages.doubleClick} ${window.siyuan.languages.searchTip2} ${window.siyuan.languages.click} ${window.siyuan.languages.searchTip3} ${updateHotkeyTip(window.siyuan.config.keymap.editor.general.insertRight.custom)}/${updateHotkeyTip("⌥" + window.siyuan.languages.click)} ${window.siyuan.languages.searchTip4} Esc ${window.siyuan.languages.searchTip5}
↑/↓/PageUp/PageDown ${window.siyuan.languages.searchTip1} ${window.siyuan.languages.enterKey}/${window.siyuan.languages.doubleClick} ${window.siyuan.languages.searchTip2} ${updateHotkeyTip(window.siyuan.config.keymap.editor.general.insertRight.custom)}/${updateHotkeyTip("⌥" + window.siyuan.languages.click)} ${window.siyuan.languages.searchTip4} Esc ${window.siyuan.languages.searchTip5}
`; const criteriaData: Config.IUILayoutTabSearchConfig[] = []; 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 edit = new Protyle(app, element.querySelector("#searchPreview") as HTMLElement, { blockId: "", render: { gutter: true, breadcrumbDocName: true }, }); edit.resize(); const unRefEdit = new Protyle(app, element.querySelector("#searchUnRefPreview") as HTMLElement, { blockId: "", render: { gutter: true, breadcrumbDocName: true }, }); unRefEdit.resize(); 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"); const unRefPanelElement = element.querySelector("#searchUnRefPanel"); 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: getDefaultType(), replaceTypes: Object.assign({}, Constants.SIYUAN_DEFAULT_REPLACETYPES), }, config, edit, true); 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; } }); if (target.parentElement.classList.contains("b3-chip--current")) { updateConfig(element, { removed: true, sort: 0, group: 0, hasReplace: false, method: 0, hPath: "", idPath: [], k: "", r: "", page: 1, types: getDefaultType(), replaceTypes: Object.assign({}, Constants.SIYUAN_DEFAULT_REPLACETYPES), }, config, edit, true); } target.parentElement.remove(); event.stopPropagation(); event.preventDefault(); break; } else if (target.classList.contains("search__rmpath")) { config.idPath = []; config.hPath = ""; config.page = 1; searchPathInputElement.textContent = ""; searchPathInputElement.setAttribute("aria-label", ""); inputEvent(element, config, edit, true); const includeElement = element.querySelector("#searchInclude"); includeElement.firstElementChild.classList.add("ft__primary"); 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 = `${escapeGreat(config.hPath)}`; searchPathInputElement.setAttribute("aria-label", escapeHtml(config.hPath)); const includeElement = element.querySelector("#searchInclude"); includeElement.firstElementChild.classList.add("ft__primary"); 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") { const svgElement = target.firstElementChild; svgElement.classList.toggle("ft__primary"); if (!svgElement.classList.contains("ft__primary")) { 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.querySelectorAll(".search__header")[1].classList.toggle("fn__none"); event.stopPropagation(); event.preventDefault(); break; } else if (target.id === "searchUnRef") { openSearchUnRef(unRefPanelElement, unRefEdit); event.stopPropagation(); event.preventDefault(); break; } else if (target.id === "unRefMore") { unRefMoreMenu(target, unRefPanelElement, unRefEdit); event.stopPropagation(); event.preventDefault(); break; } else if (target.id === "searchUnRefClose") { window.siyuan.menus.menu.remove(); unRefPanelElement.classList.add("fn__none"); assetsElement.previousElementSibling.classList.remove("fn__none"); searchInputElement.select(); event.stopPropagation(); event.preventDefault(); break; } else if (type === "unRefPrevious") { if (!target.getAttribute("disabled")) { let currentPage = parseInt(unRefPanelElement.querySelector("#searchUnRefResult").textContent); if (currentPage > 1) { currentPage--; getUnRefList(unRefPanelElement, unRefEdit, currentPage); } } event.stopPropagation(); event.preventDefault(); break; } else if (type === "unRefNext") { if (!target.getAttribute("disabled")) { let currentPage = parseInt(unRefPanelElement.querySelector("#searchUnRefResult").textContent); if (currentPage < parseInt(unRefPanelElement.querySelector("#searchUnRefResult").textContent.split("/")[1])) { currentPage++; getUnRefList(unRefPanelElement, unRefEdit, currentPage); } } 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: getDefaultType(), replaceTypes: Object.assign({}, Constants.SIYUAN_DEFAULT_REPLACETYPES), }, config, edit, true); 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: "", label: window.siyuan.languages.layout, type: "submenu", submenu: [{ iconHTML: "", 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: "", 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 (target.id === "replaceFilter") { window.siyuan.menus.menu.remove(); replaceFilterMenu(config); 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(element, config, edit); event.stopPropagation(); event.preventDefault(); return; } else if (target.id === "assetHistoryBtn") { toggleAssetHistory(assetsElement); event.stopPropagation(); event.preventDefault(); return; } else if (target.id === "replaceHistoryBtn") { toggleReplaceHistory(element); 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 (type === "search-new") { if (config.method == 0) { newFileByName(app, searchInputElement.value); } } else if (type === "search-item") { const searchType = target.dataset.id ? "asset" : (unRefPanelElement.classList.contains("fn__none") ? "doc" : "unRef"); 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 (searchType === "asset") { 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"); checkFold(id, (zoomIn, action) => { openFileById({ app, id, action, zoomIn, position: "right" }); if (closeCB) { closeCB(); } }); } else if (!target.classList.contains("b3-list-item--focus")) { (searchType === "doc" ? searchPanelElement : unRefPanelElement).querySelector(".b3-list-item--focus").classList.remove("b3-list-item--focus"); target.classList.add("b3-list-item--focus"); getArticle({ edit: searchType === "doc" ? edit : unRefEdit, id: target.getAttribute("data-node-id"), config: searchType === "doc" ? config : null, value: searchType === "doc" ? searchInputElement.value : null, }); searchInputElement.focus(); } else if (searchType === "doc" && 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 (searchType === "asset") { /// #if !BROWSER showFileInFolder(path.join(window.siyuan.config.system.dataDir, target.lastElementChild.getAttribute("aria-label"))); /// #endif } else { const id = target.getAttribute("data-node-id"); checkFold(id, (zoomIn, action) => { openFileById({ app, id, action, zoomIn }); 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; } }, 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); }); addClearButton({ inputElement: searchInputElement, right: 8, height: searchInputElement.clientHeight, clearCB() { config.page = 1; inputEvent(element, config, edit); } }); addClearButton({ right: 8, inputElement: replaceInputElement, height: searchInputElement.clientHeight, }); inputEvent(element, config, edit); return {edit, unRefEdit}; }; 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; }; 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) { // https://github.com/siyuan-note/siyuan/issues/6828 item.hPath = config.hPath; item.idPath = config.idPath.join(",").split(","); } if (config.hasReplace !== item.hasReplace) { const replaceHeaderElement = element.querySelectorAll(".search__header")[1]; if (item.hasReplace) { replaceHeaderElement.classList.remove("fn__none"); } else { replaceHeaderElement.classList.add("fn__none"); } } const searchPathInputElement = element.querySelector("#searchPathInput"); if (item.hPath) { searchPathInputElement.innerHTML = `${escapeGreat(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.firstElementChild.classList.add("ft__primary"); } else { searchIncludeElement.firstElementChild.classList.remove("ft__primary"); } if (enableIncludeChild) { searchIncludeElement.removeAttribute("disabled"); } else { searchIncludeElement.setAttribute("disabled", "disabled"); } if (item.k || clear) { (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?: Config.IUILayoutTabSearchConfig, edit: Protyle value?: string, }) => { checkFold(options.id, (zoomIn) => { options.edit.protyle.scroll.lastScrollTop = 0; addLoading(options.edit.protyle); fetchPost("/api/block/getDocInfo", { id: options.id, }, (response) => { options.edit.protyle.wysiwyg.renderCustom(response.data.ial); fetchPost("/api/filetree/getDoc", { id: options.id, query: options.value || null, queryMethod: options.config?.method || null, queryTypes: options.config?.types || null, mode: zoomIn ? 0 : 3, size: zoomIn ? Constants.SIZE_GET_MAX : window.siyuan.config.editor.dynamicLoadBlocks, zoom: zoomIn, }, getResponse => { options.edit.protyle.query = { key: options.value || null, method: options.config?.method || null, types: options.config?.types || null, }; onGet({ updateReadonly: true, data: getResponse, protyle: options.edit.protyle, action: zoomIn ? [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(); const matchRectTop = matchElement.getBoundingClientRect().top; // 需前置,否则代码高亮后会移除该元素 setTimeout(() => { // 等待 scrollCenter 定位后再滚动 options.edit.protyle.contentElement.scrollTop = options.edit.protyle.contentElement.scrollTop + matchRectTop - contentRect.top - contentRect.height / 2; }); } }); }); }); }; export const replace = (element: Element, config: Config.IUILayoutTabSearchConfig, 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 = element.querySelector("svg.fn__rotate"); 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")], replaceTypes: config.replaceTypes }, (response) => { loadElement.classList.add("fn__none"); if (response.code === 1) { showMessage(response.msg); return; } if (isAll) { inputEvent(element, config, edit, true); 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: Config.IUILayoutTabSearchConfig, 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()); }; export 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: Config.IUILayoutTabSearchConfig) => { let resultHTML = ""; data.forEach((item, index) => { const title = getNotebookName(item.box) + getDisplayName(item.hPath, false); const tags = item.tag ? `${item.tag.split("# #").map(tag => `${tag.replace("#", "")}`).join(" ").replace("#", "")}` : ""; 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)} ${tags} ${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 || ( config.method === 0 ? `
${window.siyuan.languages.newFile} ${escapeHtml((element.querySelector("#searchInput") as HTMLInputElement).value)} ${window.siyuan.languages.enterNew}
${window.siyuan.languages.enterNewTip}
` : `
${window.siyuan.languages.emptyContent}
`); };