diff --git a/src/components/search-engine/index.ts b/src/components/search-engine/index.ts index d5be9072f89c5b981b7b8a5ad3462c93c531bc23..39680b8fbf190aacb55aefa5416bf31a850c67a3 100644 --- a/src/components/search-engine/index.ts +++ b/src/components/search-engine/index.ts @@ -4,4 +4,5 @@ export enum SearchType { Title, Desc, Url, + Current } diff --git a/src/components/search-engine/search-engine.component.html b/src/components/search-engine/search-engine.component.html index 40191f90a1bf4083b3a1fae2ed1e38c80744dffb..3e355b4392a4be14ccb72955a718a94c1af74eeb 100644 --- a/src/components/search-engine/search-engine.component.html +++ b/src/components/search-engine/search-engine.component.html @@ -30,9 +30,11 @@ + + diff --git a/src/components/search-engine/search-engine.component.ts b/src/components/search-engine/search-engine.component.ts index 22182757a3506548e637d027928cb47eb3849312..61e0e404a829f37808c3974abe7d3b3a170b6fb1 100644 --- a/src/components/search-engine/search-engine.component.ts +++ b/src/components/search-engine/search-engine.component.ts @@ -17,7 +17,7 @@ export class SearchEngineComponent { searchEngineList: ISearchEngineProps[] = (searchEngineList as any).default currentEngine: ISearchEngineProps = getDefaultSearchEngine() SearchType = SearchType - searchTypeValue = SearchType.Title + searchTypeValue = SearchType.All showEngine = false keyword = queryString().q diff --git a/src/utils/index.ts b/src/utils/index.ts index 699ca994dbc5c1a365f4625fa9faae9796afebe9..5a54efcbc9d1742688b8da66a679449b22dd3401 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -22,10 +22,12 @@ export function randomInt(max: number) { return Math.floor(Math.random() * max) } -export function fuzzySearch(navList: INavProps[], keyword: string) { - const sType = Number(queryString().type) || SearchType.Title +export function fuzzySearch(navList: INavProps[], keyword: string): INavThreeProp[] { + const { type, page, id } = queryString() + const sType = Number(type) || SearchType.Title const navData = [] const resultList = [{ nav: navData }] + const urlRecordMap = {} function f(arr?: any[]) { arr = arr || navList @@ -45,44 +47,73 @@ export function fuzzySearch(navList: INavProps[], keyword: string) { const search = keyword.toLowerCase() const urls = Object.values(item.urls || {}) + function searchTitle(): boolean { + if (name.includes(search)) { + let result = { ...item } + const regex = new RegExp(`(${keyword})`, 'i') + result.name = result.name.replace(regex, `$1`.bold()) + + if (!urlRecordMap[result.url]) { + urlRecordMap[result.url] = true + navData.push(result) + return true + } + } + return false + } + + function searchUrl() { + if (url?.includes?.(keyword.toLowerCase())) { + if (!urlRecordMap[item.url]) { + urlRecordMap[item.url] = true + navData.push(item) + return true + } + } + + const find = urls.some((item: string) => item.includes(keyword)) + if (find) { + if (!urlRecordMap[item.url]) { + urlRecordMap[item.url] = true + navData.push(item) + return true + } + } + } + + function searchDesc(): boolean { + if (desc.includes(search)) { + let result = { ...item } + const regex = new RegExp(`(${keyword})`, 'i') + result.desc = result.desc.replace(regex, `$1`.bold()) + + if (!urlRecordMap[result.url]) { + urlRecordMap[result.url] = true + navData.push(result) + return true + } + } + return false + } + try { switch (sType) { case SearchType.Url: - if (url?.includes?.(keyword.toLowerCase())) { - navData.push(item) - } - - const find = urls.some((item: string) => item.includes(keyword)) - if (find) { - navData.push(item) - } + searchUrl() break case SearchType.Title: - if (name.includes(search)) { - let result = { ...item } - const regex = new RegExp(`(${keyword})`, 'i') - result.name = result.name.replace(regex, `$1`.bold()) - - const exists = navData.some(item => item.name === result.name) - if (!exists) { - navData.push(result) - } - } + searchTitle() break case SearchType.Desc: - if (desc.includes(search)) { - let result = { ...item } - const regex = new RegExp(`(${keyword})`, 'i') - result.desc = result.desc.replace(regex, `$1`.bold()) - - const exists = navData.some(item => item.desc === result.desc) - if (!exists) { - navData.push(result) - } - } + searchDesc() break + + default: + searchTitle() + searchDesc() + searchUrl() } } catch (error) { console.error(error) @@ -91,7 +122,11 @@ export function fuzzySearch(navList: INavProps[], keyword: string) { } } - f() + if (sType === SearchType.Current) { + f(navList[page].nav[id].nav) + } else { + f() + } if (navData.length <= 0) { return []