From 457e96929c37f06a69f212522e040553bd56f5c9 Mon Sep 17 00:00:00 2001 From: Vanessa Date: Fri, 2 Dec 2022 00:05:40 +0800 Subject: [PATCH] :art: fix https://github.com/siyuan-note/siyuan/issues/6759 --- app/src/search/util.ts | 208 +++++++++++++++++++++++++++------------ app/src/types/index.d.ts | 23 +---- 2 files changed, 148 insertions(+), 83 deletions(-) diff --git a/app/src/search/util.ts b/app/src/search/util.ts index 130680a84..644949581 100644 --- a/app/src/search/util.ts +++ b/app/src/search/util.ts @@ -16,6 +16,7 @@ import {onGet} from "../protyle/util/onGet"; import {addLoading, setPadding} from "../protyle/ui/initUI"; import {getIconByType} from "../editor/getIcon"; import {unicode2Emoji} from "../emoji"; +import {Dialog} from "../dialog"; export const openGlobalSearch = (text: string, replace: boolean) => { text = text.trim(); @@ -86,6 +87,16 @@ export const genSearch = (config: ISearchOption, element: Element, closeCB?: () } else if (config.method === 3) { methodText = window.siyuan.languages.regex; } + let includeChild = true; + let enableIncludeChild = false; + config.idPath.forEach(item => { + if (!item.endsWith(".sy") && item.split("/").length > 1) { + includeChild = false; + } + if (item.split("/").length > 1) { + enableIncludeChild = true; + } + }); element.innerHTML = `
@@ -141,6 +152,8 @@ export const genSearch = (config: ISearchOption, element: Element, closeCB?: () + + @@ -190,6 +203,9 @@ export const genSearch = (config: ISearchOption, element: Element, closeCB?: () config.hPath = ""; element.querySelector("#searchPathInput").innerHTML = config.hPath; inputTimeout = inputEvent(element, config, inputTimeout, edit, false); + const includeElement = element.querySelector("#searchInclude") + includeElement.classList.remove("b3-button--cancel") + includeElement.setAttribute("disabled", "disabled") event.stopPropagation(); event.preventDefault(); break; @@ -218,11 +234,13 @@ export const genSearch = (config: ISearchOption, element: Element, closeCB?: () 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(escapeHtml(getNotebookName(toNotebook[index]))); } else { + enableIncludeChild = true; config.idPath.push(pathPosix().join(toNotebook[index], item)); } }); @@ -231,12 +249,38 @@ export const genSearch = (config: ISearchOption, element: Element, closeCB?: () } config.hPath = escapeHtml(hPathList.join(" ")); element.querySelector("#searchPathInput").innerHTML = `${config.hPath}`; + const includeElement = element.querySelector("#searchInclude") + includeElement.classList.remove("b3-button--cancel") + if (enableIncludeChild) { + includeElement.removeAttribute("disabled") + } else { + includeElement.setAttribute("disabled", "disabled") + } inputTimeout = inputEvent(element, config, inputTimeout, edit, false); }); }, [], 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", ""); + } + }); + } + inputTimeout = inputEvent(element, config, inputTimeout, edit); + event.stopPropagation(); + event.preventDefault(); + break; } else if (target.id === "searchReplace") { // ctrl+P 不需要保存 config.hasReplace = !config.hasReplace; @@ -284,53 +328,7 @@ export const genSearch = (config: ISearchOption, element: Element, closeCB?: () event.preventDefault(); break; } else if (target.id === "searchFilter") { - window.siyuan.menus.menu.remove(); - let includeChild = true; - config.idPath.find(item => { - if (!item.endsWith(".sy")) { - includeChild = false; - return true; - } - }); - window.siyuan.menus.menu.append(new MenuItem({ - label: `
${window.siyuan.languages.includeChildDoc} -
`, - bind(menuItemElement) { - menuItemElement.addEventListener("click", (event: MouseEvent & { target: HTMLElement }) => { - const inputElement = menuItemElement.querySelector("input"); - if (event.target.tagName !== "INPUT") { - inputElement.checked = !inputElement.checked; - } - if (!inputElement.checked) { - 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", ""); - } - }); - } - inputTimeout = inputEvent(element, config, inputTimeout, edit); - window.siyuan.menus.menu.remove(); - }); - } - }).element); - addConfigMenu(config, window.siyuan.languages.math, "mathBlock", edit, element); - addConfigMenu(config, window.siyuan.languages.table, "table", edit, element); - addConfigMenu(config, window.siyuan.languages.quote, "blockquote", edit, element); - addConfigMenu(config, window.siyuan.languages.superBlock, "superBlock", edit, element); - addConfigMenu(config, window.siyuan.languages.paragraph, "paragraph", edit, element); - addConfigMenu(config, window.siyuan.languages.doc, "document", edit, element); - addConfigMenu(config, window.siyuan.languages.headings, "heading", edit, element); - addConfigMenu(config, window.siyuan.languages.list1, "list", edit, element); - addConfigMenu(config, window.siyuan.languages.listItem, "listItem", edit, element); - addConfigMenu(config, window.siyuan.languages.code, "codeBlock", edit, element); - addConfigMenu(config, "HTML", "htmlBlock", edit, element); - window.siyuan.menus.menu.popup({x: event.clientX - 16, y: event.clientY - 16}, true); + addConfigFilterMenu(config, edit, element); event.stopPropagation(); event.preventDefault(); break; @@ -601,23 +599,105 @@ const addConfigGroupMenu = (config: ISearchOption, edit: Protyle, element: Eleme }).element); }; -const addConfigMenu = (config: ISearchOption, lang: string, key: "mathBlock" | "table" | "blockquote" | "superBlock" | "paragraph" | "document" | "heading" | "list" | "listItem" | "codeBlock" | "htmlBlock", - edit: Protyle, element: Element) => { - window.siyuan.menus.menu.append(new MenuItem({ - label: `
${lang} -
`, - bind(menuItemElement) { - menuItemElement.addEventListener("click", (event: MouseEvent & { target: HTMLElement }) => { - const inputElement = menuItemElement.querySelector("input"); - if (event.target.tagName !== "INPUT") { - inputElement.checked = !inputElement.checked; - } - config.types[key] = inputElement.checked; - inputEvent(element, config, undefined, edit); - window.siyuan.menus.menu.remove(); - }); - } - }).element); +const addConfigFilterMenu = (config: ISearchOption, edit: Protyle, element: Element) => { + const filterDialog = new Dialog({ + title: window.siyuan.languages.type, + content: `
+ + + + + + + + + + + +
+
+
+ +
`, + width: "520px", + }) + const btnsElement = filterDialog.element.querySelectorAll(".b3-button"); + btnsElement[0].addEventListener("click", () => { + filterDialog.destroy(); + }); + btnsElement[1].addEventListener("click", () => { + filterDialog.element.querySelectorAll(".b3-switch").forEach((item: HTMLInputElement) => { + config.types[item.getAttribute("data-type") as TSearchFilter] = item.checked; + }) + inputEvent(element, config, undefined, edit); + filterDialog.destroy(); + }); }; const addQueryMenu = (config: ISearchOption, edit: Protyle, element: Element) => { diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts index b7558d2c7..fe6203c2a 100644 --- a/app/src/types/index.d.ts +++ b/app/src/types/index.d.ts @@ -1,27 +1,12 @@ type TLayout = "normal" | "top" | "bottom" | "left" | "right" | "center" +type TSearchFilter = "mathBlock" | "table" | "blockquote" | "superBlock" | "paragraph" | "document" | "heading" + | "list" | "listItem" | "codeBlock" | "htmlBlock" type TDirection = "lr" | "tb" -type TDockType = - "file" - | "outline" - | "bookmark" - | "tag" - | "graph" - | "globalGraph" - | "backlink" - | "backlinkOld" - | "inbox" +type TDockType = "file" | "outline" | "bookmark" | "tag" | "graph" | "globalGraph" | "backlink" | "backlinkOld" | "inbox" type TDockPosition = "Left" | "Right" | "Top" | "Bottom" type TWS = "main" | "filetree" | "protyle" type TEditorMode = "preview" | "wysiwyg" -type TOperation = "insert" - | "update" - | "delete" - | "move" - | "foldHeading" - | "unfoldHeading" - | "setAttrs" - | "updateAttrs" - | "append" +type TOperation = "insert" | "update" | "delete" | "move" | "foldHeading" | "unfoldHeading" | "setAttrs" | "updateAttrs" | "append" type TBazaarType = "templates" | "icons" | "widgets" | "themes" declare module "blueimp-md5"