From 0dedf8c55f66cde27c253d6b7aa63bea7632c34d Mon Sep 17 00:00:00 2001 From: Vanessa Date: Sat, 3 Dec 2022 23:50:22 +0800 Subject: [PATCH] :art: https://github.com/siyuan-note/siyuan/issues/6589 --- app/appearance/langs/zh_CN.json | 2 + app/src/search/util.ts | 108 ++++++++++++++++++++++++++++++-- app/src/types/index.d.ts | 25 +++++++- 3 files changed, 127 insertions(+), 8 deletions(-) diff --git a/app/appearance/langs/zh_CN.json b/app/appearance/langs/zh_CN.json index 180bfa518..9d8782b29 100644 --- a/app/appearance/langs/zh_CN.json +++ b/app/appearance/langs/zh_CN.json @@ -1,4 +1,6 @@ { + "saveSearch": "保存查询条件", + "useSearch": "使用查询条件", "group": "分组", "noGroupBy": "不分组", "groupByDoc": "按文档分组", diff --git a/app/src/search/util.ts b/app/src/search/util.ts index 13e80ea76..aaf7f63db 100644 --- a/app/src/search/util.ts +++ b/app/src/search/util.ts @@ -350,6 +350,9 @@ export const genSearch = (config: ISearchOption, element: Element, closeCB?: () html += `
${escapeHtml(s)}
`; } }); + if (html === "") { + return; + } historyElement.classList.remove("fn__none"); historyElement.innerHTML = html; replaceHistoryElement.classList.add("fn__none"); @@ -366,6 +369,9 @@ export const genSearch = (config: ISearchOption, element: Element, closeCB?: () html += `
${escapeHtml(s)}
`; } }); + if (html === "") { + return; + } replaceHistoryElement.classList.remove("fn__none"); replaceHistoryElement.innerHTML = html; historyElement.classList.add("fn__none"); @@ -466,7 +472,7 @@ export const genSearch = (config: ISearchOption, element: Element, closeCB?: () } config.list = searches; config.k = searchInputElement.value; - if (!element.parentElement.getAttribute("data-id")) { + if (!element.getAttribute("data-id")) { localStorage.setItem(Constants.LOCAL_SEARCHEDATA, JSON.stringify(config)); } }); @@ -640,7 +646,7 @@ const addConfigMoreMenu = (config: ISearchOption, edit: Protyle, element: Elemen element.querySelector(".search__layout").classList.remove("search__layout--row"); setPadding(edit.protyle); config.layout = 0; - if (!element.parentElement.getAttribute("data-id")) { + if (!element.getAttribute("data-id")) { localStorage.setItem(Constants.LOCAL_SEARCHEDATA, JSON.stringify(config)); } } @@ -651,12 +657,104 @@ const addConfigMoreMenu = (config: ISearchOption, edit: Protyle, element: Elemen element.querySelector(".search__layout").classList.add("search__layout--row"); setPadding(edit.protyle); config.layout = 1; - if (!element.parentElement.getAttribute("data-id")) { + if (!element.getAttribute("data-id")) { localStorage.setItem(Constants.LOCAL_SEARCHEDATA, JSON.stringify(config)); } } }] }).element); + window.siyuan.menus.menu.append(new MenuItem({type: "separator"}).element); + window.siyuan.menus.menu.append(new MenuItem({ + label: window.siyuan.languages.saveSearch, + click() { + const saveDialog = new Dialog({ + title: window.siyuan.languages.saveSearch, + content: `
+ +
+
+
+ +
`, + width: "520px", + }); + const btnsElement = saveDialog.element.querySelectorAll(".b3-button"); + btnsElement[0].addEventListener("click", () => { + saveDialog.destroy(); + }); + btnsElement[1].addEventListener("click", () => { + fetchPost("/api/setting/setCriterion", {criterion: Object.assign({name: saveDialog.element.querySelector("input").value}, config)}, () => { + saveDialog.destroy(); + }) + }); + } + }).element) + const searchSubMenu: IMenu[] = [] + window.siyuan.config.criteria.forEach(item => { + searchSubMenu.push({ + label: item.name, + click() { + if (config.layout !== item.layout) { + if (item.layout === 0) { + element.querySelector(".search__layout").classList.remove("search__layout--row"); + } else { + element.querySelector(".search__layout").classList.add("search__layout--row"); + } + setPadding(edit.protyle); + } + 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"); + } + } + if (item.hPath) { + element.querySelector("#searchPathInput").innerHTML = `${item.hPath}`; + } else { + element.querySelector("#searchPathInput").innerHTML = ""; + } + 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; + inputEvent(element, Object.assign({}, item), undefined, edit); + } + }) + }) + if (searchSubMenu.length > 0) { + window.siyuan.menus.menu.append(new MenuItem({ + label: window.siyuan.languages.useSearch, + type: "submenu", + submenu: searchSubMenu + }).element) + } }; const addConfigFilterMenu = (config: ISearchOption, edit: Protyle, element: Element) => { @@ -851,7 +949,7 @@ const replace = (element: Element, config: ISearchOption, edit: Protyle, isAll: } config.replaceList = searches; config.r = replaceInputElement.value; - if (!element.parentElement.getAttribute("data-id")) { + if (!element.getAttribute("data-id")) { localStorage.setItem(Constants.LOCAL_SEARCHEDATA, JSON.stringify(config)); } let currentList: HTMLElement = searchPanelElement.querySelector(".b3-list-item--focus"); @@ -947,7 +1045,7 @@ const inputEvent = (element: Element, config: ISearchOption, inputTimeout: numbe } if (saveConfig) { config.k = inputValue; - if (!element.parentElement.getAttribute("data-id")) { + if (!element.getAttribute("data-id")) { localStorage.setItem(Constants.LOCAL_SEARCHEDATA, JSON.stringify(config)); } } diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts index fbe8ac034..a5e490c1e 100644 --- a/app/src/types/index.d.ts +++ b/app/src/types/index.d.ts @@ -1,12 +1,30 @@ type TLayout = "normal" | "top" | "bottom" | "left" | "right" | "center" -type TSearchFilter = "mathBlock" | "table" | "blockquote" | "superBlock" | "paragraph" | "document" | "heading" +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" @@ -34,6 +52,7 @@ interface Window { } interface ISearchOption { + name?: string sort: number, // 0:按块类型(默认),1:按创建时间升序,2:按创建时间降序,3:按更新时间升序,4:按更新时间降序,5:按内容顺序(仅在按文档分组时) group: number, // 0:不分组,1:按文档分组 layout: number // 0:上下,1:左右