diff --git a/components/AlgoliaSearchModal.js b/components/AlgoliaSearchModal.js index ea727a48..eac9d8bf 100644 --- a/components/AlgoliaSearchModal.js +++ b/components/AlgoliaSearchModal.js @@ -1,11 +1,18 @@ -import { useState, useImperativeHandle, useRef, useEffect, Fragment } from 'react' -import algoliasearch from 'algoliasearch' import replaceSearchResult from '@/components/Mark' -import Link from 'next/link' -import { useGlobal } from '@/lib/global' -import throttle from 'lodash/throttle' import { siteConfig } from '@/lib/config' -import { useHotkeys } from 'react-hotkeys-hook'; +import { useGlobal } from '@/lib/global' +import algoliasearch from 'algoliasearch' +import throttle from 'lodash/throttle' +import Link from 'next/link' +import { useRouter } from 'next/router' +import { + Fragment, + useEffect, + useImperativeHandle, + useRef, + useState +} from 'react' +import { useHotkeys } from 'react-hotkeys-hook' const ShortCutActions = [ { @@ -20,7 +27,6 @@ const ShortCutActions = [ key: 'Esc', action: '关闭' } - ] /** @@ -36,31 +42,49 @@ export default function AlgoliaSearchModal({ cRef }) { const [totalPage, setTotalPage] = useState(0) const [totalHit, setTotalHit] = useState(0) const [useTime, setUseTime] = useState(0) - const inputRef = useRef(null) const [activeIndex, setActiveIndex] = useState(0) const [isLoading, setIsLoading] = useState(false) - useHotkeys('ctrl+k', (e) => { + const inputRef = useRef(null) + const router = useRouter() + + /** + * 快捷键设置 + */ + useHotkeys('ctrl+k', e => { e.preventDefault() setIsModalOpen(true) }) // 方向键调整选中 - useHotkeys('down', (e) => { - e.preventDefault() - if (activeIndex < searchResults.length - 1) { - setActiveIndex(activeIndex + 1) - } - }, { enableOnFormTags: true }) - useHotkeys('up', (e) => { - e.preventDefault() - if (activeIndex > 0) { - setActiveIndex(activeIndex - 1) - } - }, { enableOnFormTags: true }) + useHotkeys( + 'down', + e => { + e.preventDefault() + if (activeIndex < searchResults.length - 1) { + setActiveIndex(activeIndex + 1) + } + }, + { enableOnFormTags: true } + ) + useHotkeys( + 'up', + e => { + e.preventDefault() + if (activeIndex > 0) { + setActiveIndex(activeIndex - 1) + } + }, + { enableOnFormTags: true } + ) // esc关闭 - useHotkeys('esc', (e) => { - e.preventDefault() - setIsModalOpen(false) - }, { enableOnFormTags: true }) + useHotkeys( + 'esc', + e => { + e.preventDefault() + setIsModalOpen(false) + }, + { enableOnFormTags: true } + ) + // 跳转Search结果 const onJumpSearchResult = () => { if (searchResults.length > 0) { @@ -68,11 +92,15 @@ export default function AlgoliaSearchModal({ cRef }) { } } // enter跳转 - useHotkeys('enter', (e) => { - if (searchResults.length > 0) { - onJumpSearchResult(index) - } - }, { enableOnFormTags: true }) + useHotkeys( + 'enter', + e => { + if (searchResults.length > 0) { + onJumpSearchResult(index) + } + }, + { enableOnFormTags: true } + ) const resetSearch = () => { setActiveIndex(0) @@ -84,6 +112,16 @@ export default function AlgoliaSearchModal({ cRef }) { if (inputRef.current) inputRef.current.value = '' } + /** + * 页面路径变化后,自动关闭此modal + */ + useEffect(() => { + setIsModalOpen(false) + }, [router]) + + /** + * 自动聚焦搜索框 + */ useEffect(() => { if (isModalOpen) { setTimeout(() => { @@ -93,9 +131,10 @@ export default function AlgoliaSearchModal({ cRef }) { resetSearch() } }, [isModalOpen]) + /** - * 对外暴露方法 - */ + * 对外暴露方法 + **/ useImperativeHandle(cRef, () => { return { openSearch: () => { @@ -104,7 +143,10 @@ export default function AlgoliaSearchModal({ cRef }) { } }) - const client = algoliasearch(siteConfig('ALGOLIA_APP_ID'), siteConfig('ALGOLIA_SEARCH_ONLY_APP_KEY')) + const client = algoliasearch( + siteConfig('ALGOLIA_APP_ID'), + siteConfig('ALGOLIA_SEARCH_ONLY_APP_KEY') + ) const index = client.initIndex(siteConfig('ALGOLIA_INDEX')) /** @@ -131,7 +173,9 @@ export default function AlgoliaSearchModal({ cRef }) { setTotalHit(nbHits) setSearchResults(hits) setIsLoading(false) - const doms = document.getElementById('search-wrapper').getElementsByClassName('replace') + const doms = document + .getElementById('search-wrapper') + .getElementsByClassName('replace') setTimeout(() => { replaceSearchResult({ @@ -149,33 +193,35 @@ export default function AlgoliaSearchModal({ cRef }) { } // 定义节流函数,确保在用户停止输入一段时间后才会调用处理搜索的方法 - const throttledHandleInputChange = useRef(throttle((query, page = 0) => { - handleSearch(query, page); - }, 1000)); + const throttledHandleInputChange = useRef( + throttle((query, page = 0) => { + handleSearch(query, page) + }, 1000) + ) // 用于存储搜索延迟的计时器 - const searchTimer = useRef(null); + const searchTimer = useRef(null) // 修改input的onChange事件处理函数 - const handleInputChange = (e) => { - const query = e.target.value; + const handleInputChange = e => { + const query = e.target.value // 如果已经有计时器在等待搜索,先清除之前的计时器 if (searchTimer.current) { - clearTimeout(searchTimer.current); + clearTimeout(searchTimer.current) } // 设置新的计时器,在用户停止输入一段时间后触发搜索 searchTimer.current = setTimeout(() => { - throttledHandleInputChange.current(query); - }, 800); - }; + throttledHandleInputChange.current(query) + }, 800) + } /** * 切换页码 * @param {*} page */ - const switchPage = (page) => { + const switchPage = page => { throttledHandleInputChange.current(keyword, page) } @@ -191,58 +237,58 @@ export default function AlgoliaSearchModal({ cRef }) { } return (
无法找到相关结果 - "{keyword}"
-+ {' '} + 无法找到相关结果 + "{keyword}" +
+@@ -266,19 +319,18 @@ export default function AlgoliaSearchModal({ cRef }) {
)}