diff --git a/themes/Medium/LayoutBase.js b/themes/Medium/LayoutBase.js index f3e3b69d..1e06c4aa 100644 --- a/themes/Medium/LayoutBase.js +++ b/themes/Medium/LayoutBase.js @@ -9,6 +9,7 @@ import TopNavBar from './components/TopNavBar' import SearchInput from './components/SearchInput' import BottomMenuBar from './components/BottomMenuBar' import { useGlobal } from '@/lib/global' +import { useRouter } from 'next/router' /** * 基础布局 采用左右两侧布局,移动端使用顶部导航栏 @@ -19,9 +20,10 @@ import { useGlobal } from '@/lib/global' const LayoutBase = props => { const { children, meta, showInfoCard = true, slotRight, slotTop } = props const { locale } = useGlobal() + const router = useRouter() return ( -
+
{/* 桌面端左侧菜单 */} @@ -41,7 +43,7 @@ const LayoutBase = props => { {slotRight}
- + {router.pathname !== '/search' && } {showInfoCard && } {CONFIG_MEDIUM.WIDGET_REVOLVER_MAPS === 'true' && }
diff --git a/themes/Medium/LayoutSearch.js b/themes/Medium/LayoutSearch.js index c29d9637..59a15491 100644 --- a/themes/Medium/LayoutSearch.js +++ b/themes/Medium/LayoutSearch.js @@ -4,21 +4,30 @@ import { useGlobal } from '@/lib/global' import TagGroups from './components/TagGroups' import CategoryGroup from './components/CategoryGroup' import BlogPostListScroll from './components/BlogPostListScroll' +import { useEffect } from 'react' export const LayoutSearch = (props) => { const { locale } = useGlobal() + const { keyword } = props + useEffect(() => { + setTimeout(() => { + const container = document.getElementById('container') + if (container && container.innerHTML) { + const re = new RegExp(`${keyword}`, 'gim') + container.innerHTML = container.innerHTML.replace(re, `${keyword}`) + } + }, + 100) + }) return -
-
- {locale.NAV.SEARCH} -
- - +
{locale.NAV.SEARCH}
+
- - +
+ +
} diff --git a/themes/Medium/components/SearchInput.js b/themes/Medium/components/SearchInput.js index baf6f859..505a085d 100644 --- a/themes/Medium/components/SearchInput.js +++ b/themes/Medium/components/SearchInput.js @@ -1,8 +1,9 @@ import { useRouter } from 'next/router' import { useImperativeHandle, useRef, useState } from 'react' +let lock = false const SearchInput = ({ currentTag, currentSearch, cRef, className }) => { - const [searchKey, setSearchKey] = useState(currentSearch || getSearchKey() || '') + // const [searchKey, setSearchKey] = useState(currentSearch || getSearchKey() || '') const [onLoading, setLoadingState] = useState(false) const router = useRouter() const searchInputRef = useRef() @@ -14,12 +15,15 @@ const SearchInput = ({ currentTag, currentSearch, cRef, className }) => { } }) - const handleSearch = (key) => { + const handleSearch = () => { + const key = searchInputRef.current.value + if (key && key !== '') { setLoadingState(true) - router.push({ pathname: '/search', query: { s: key } }).then(r => { - setLoadingState(false) - }) + // router.push({ pathname: '/search/' + key }).then(r => { + // setLoadingState(false) + // }) + location.href = '/search/' + key } else { router.push({ pathname: '/' }).then(r => { }) @@ -34,13 +38,19 @@ const SearchInput = ({ currentTag, currentSearch, cRef, className }) => { } const cleanSearch = () => { searchInputRef.current.value = '' - setSearchKey('') } - let lock = false + const [showClean, setShowClean] = useState(false) const updateSearchKey = (val) => { - if (!lock) { - setSearchKey(val) + if (lock) { + return + } + searchInputRef.current.value = val + + if (val) { + setShowClean(true) + } else { + setShowClean(false) } } function lockSearchInput () { @@ -61,15 +71,15 @@ const SearchInput = ({ currentTag, currentSearch, cRef, className }) => { onCompositionUpdate={lockSearchInput} onCompositionEnd={unLockSearchInput} onChange={e => updateSearchKey(e.target.value)} - defaultValue={searchKey} + defaultValue={currentSearch} />
{ handleSearch(searchKey) }}> + onClick={handleSearch}>
- {(searchKey && searchKey.length && + {(showClean &&
@@ -77,12 +87,4 @@ const SearchInput = ({ currentTag, currentSearch, cRef, className }) => {
} -function getSearchKey () { - const router = useRouter() - if (router.query && router.query.s) { - return router.query.s - } - return null -} - export default SearchInput