mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-06-04 07:26:47 +00:00
Next主题支持algolia全文搜索
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import { useGlobal } from '@/lib/global'
|
import { useGlobal } from '@/lib/global'
|
||||||
import { useImperativeHandle, useRef, useState } from 'react'
|
import { useImperativeHandle, useRef, useState } from 'react'
|
||||||
|
import { useNextGlobal } from '..'
|
||||||
|
import { siteConfig } from '@/lib/config'
|
||||||
|
|
||||||
let lock = false
|
let lock = false
|
||||||
|
|
||||||
@@ -9,6 +11,8 @@ const SearchInput = ({ currentTag, keyword, cRef }) => {
|
|||||||
const [onLoading, setLoadingState] = useState(false)
|
const [onLoading, setLoadingState] = useState(false)
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const searchInputRef = useRef()
|
const searchInputRef = useRef()
|
||||||
|
const { searchModal } = useNextGlobal()
|
||||||
|
|
||||||
useImperativeHandle(cRef, () => {
|
useImperativeHandle(cRef, () => {
|
||||||
return {
|
return {
|
||||||
focus: () => {
|
focus: () => {
|
||||||
@@ -16,7 +20,21 @@ const SearchInput = ({ currentTag, keyword, cRef }) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const handleFocus = () => {
|
||||||
|
// 使用Algolia
|
||||||
|
if (siteConfig('ALGOLIA_APP_ID')) {
|
||||||
|
searchModal.current.openSearch()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const handleSearch = () => {
|
const handleSearch = () => {
|
||||||
|
// 使用Algolia
|
||||||
|
if (siteConfig('ALGOLIA_APP_ID')) {
|
||||||
|
searchModal.current.openSearch()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const key = searchInputRef.current.value
|
const key = searchInputRef.current.value
|
||||||
if (key && key !== '') {
|
if (key && key !== '') {
|
||||||
setLoadingState(true)
|
setLoadingState(true)
|
||||||
@@ -60,18 +78,20 @@ const SearchInput = ({ currentTag, keyword, cRef }) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return <div data-aos="fade-down"
|
return <div className='flex w-full bg-gray-100'
|
||||||
data-aos-duration="500"
|
data-aos="fade-down"
|
||||||
data-aos-delay="200"
|
data-aos-duration="500"
|
||||||
data-aos-once="true"
|
data-aos-delay="200"
|
||||||
data-aos-anchor-placement="top-bottom"
|
data-aos-once="true"
|
||||||
className='flex w-full bg-gray-100'>
|
data-aos-anchor-placement="top-bottom"
|
||||||
|
>
|
||||||
<input
|
<input
|
||||||
ref={searchInputRef}
|
ref={searchInputRef}
|
||||||
type='text'
|
type='text'
|
||||||
placeholder={currentTag ? `${locale.SEARCH.TAGS} #${currentTag}` : `${locale.SEARCH.ARTICLES}`}
|
placeholder={currentTag ? `${locale.SEARCH.TAGS} #${currentTag}` : `${locale.SEARCH.ARTICLES}`}
|
||||||
className={'outline-none w-full text-sm pl-4 transition focus:shadow-lg font-light leading-10 text-black bg-gray-100 dark:bg-gray-800 dark:text-white'}
|
className={'outline-none w-full text-sm pl-4 transition focus:shadow-lg font-light leading-10 text-black bg-gray-100 dark:bg-gray-800 dark:text-white'}
|
||||||
onKeyUp={handleKeyUp}
|
onKeyUp={handleKeyUp}
|
||||||
|
onFocus={handleFocus}
|
||||||
onCompositionStart={lockSearchInput}
|
onCompositionStart={lockSearchInput}
|
||||||
onCompositionUpdate={lockSearchInput}
|
onCompositionUpdate={lockSearchInput}
|
||||||
onCompositionEnd={unLockSearchInput}
|
onCompositionEnd={unLockSearchInput}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import SearchDrawer from './SearchDrawer'
|
|||||||
import TagGroups from './TagGroups'
|
import TagGroups from './TagGroups'
|
||||||
import CONFIG from '../config'
|
import CONFIG from '../config'
|
||||||
import { siteConfig } from '@/lib/config'
|
import { siteConfig } from '@/lib/config'
|
||||||
|
import { useNextGlobal } from '..'
|
||||||
|
|
||||||
let windowTop = 0
|
let windowTop = 0
|
||||||
|
|
||||||
@@ -54,6 +55,16 @@ const TopNav = (props) => {
|
|||||||
changeShow(!isOpen)
|
changeShow(!isOpen)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { searchModal } = useNextGlobal()
|
||||||
|
const showSeachModal = () => {
|
||||||
|
if (siteConfig('ALGOLIA_APP_ID')) {
|
||||||
|
searchModal?.current?.openSearch()
|
||||||
|
} else {
|
||||||
|
searchDrawer?.current?.show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 搜索栏
|
||||||
const searchDrawerSlot = <>
|
const searchDrawerSlot = <>
|
||||||
{categories && (
|
{categories && (
|
||||||
<section className='mt-8'>
|
<section className='mt-8'>
|
||||||
@@ -112,7 +123,7 @@ const TopNav = (props) => {
|
|||||||
|
|
||||||
{/* 右侧功能 */}
|
{/* 右侧功能 */}
|
||||||
<div className='mr-1 flex justify-end items-center text-sm space-x-4 font-serif dark:text-gray-200'>
|
<div className='mr-1 flex justify-end items-center text-sm space-x-4 font-serif dark:text-gray-200'>
|
||||||
<div className="cursor-pointer block lg:hidden" onClick={() => { searchDrawer?.current?.show() }}>
|
<div className="cursor-pointer block lg:hidden" onClick={showSeachModal}>
|
||||||
<i className="mr-2 fas fa-search" />{locale.NAV.SEARCH}
|
<i className="mr-2 fas fa-search" />{locale.NAV.SEARCH}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import SideAreaLeft from './components/SideAreaLeft'
|
|||||||
import SideAreaRight from './components/SideAreaRight'
|
import SideAreaRight from './components/SideAreaRight'
|
||||||
import TopNav from './components/TopNav'
|
import TopNav from './components/TopNav'
|
||||||
import { useGlobal } from '@/lib/global'
|
import { useGlobal } from '@/lib/global'
|
||||||
import { useEffect, useRef, useState } from 'react'
|
import { createContext, useContext, useEffect, useRef, useState } from 'react'
|
||||||
import BlogPostListScroll from './components/BlogPostListScroll'
|
import BlogPostListScroll from './components/BlogPostListScroll'
|
||||||
import BlogPostListPage from './components/BlogPostListPage'
|
import BlogPostListPage from './components/BlogPostListPage'
|
||||||
import StickyBar from './components/StickyBar'
|
import StickyBar from './components/StickyBar'
|
||||||
@@ -26,6 +26,11 @@ import { Style } from './style'
|
|||||||
import replaceSearchResult from '@/components/Mark'
|
import replaceSearchResult from '@/components/Mark'
|
||||||
import CommonHead from '@/components/CommonHead'
|
import CommonHead from '@/components/CommonHead'
|
||||||
import { siteConfig } from '@/lib/config'
|
import { siteConfig } from '@/lib/config'
|
||||||
|
import AlgoliaSearchModal from '@/components/AlgoliaSearchModal'
|
||||||
|
|
||||||
|
// 主题全局状态
|
||||||
|
const ThemeGlobalNext = createContext()
|
||||||
|
export const useNextGlobal = () => useContext(ThemeGlobalNext)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 基础布局 采用左中右三栏布局,移动端使用顶部导航栏
|
* 基础布局 采用左中右三栏布局,移动端使用顶部导航栏
|
||||||
@@ -74,9 +79,13 @@ const LayoutBase = (props) => {
|
|||||||
drawerRight?.current?.handleSwitchVisible()
|
drawerRight?.current?.handleSwitchVisible()
|
||||||
}} />
|
}} />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
const tocRef = isBrowser ? document.getElementById('article-wrapper') : null
|
const tocRef = isBrowser ? document.getElementById('article-wrapper') : null
|
||||||
|
|
||||||
|
const searchModal = useRef(null)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<ThemeGlobalNext.Provider value={{ searchModal }}>
|
||||||
<div id='theme-next'>
|
<div id='theme-next'>
|
||||||
{/* SEO相关 */}
|
{/* SEO相关 */}
|
||||||
<CommonHead meta={meta}/>
|
<CommonHead meta={meta}/>
|
||||||
@@ -85,6 +94,8 @@ const LayoutBase = (props) => {
|
|||||||
{/* 移动端顶部导航栏 */}
|
{/* 移动端顶部导航栏 */}
|
||||||
<TopNav {...props} />
|
<TopNav {...props} />
|
||||||
|
|
||||||
|
<AlgoliaSearchModal cRef={searchModal} {...props}/>
|
||||||
|
|
||||||
<>{headerSlot}</>
|
<>{headerSlot}</>
|
||||||
|
|
||||||
{/* 顶部黑线装饰 */}
|
{/* 顶部黑线装饰 */}
|
||||||
@@ -135,6 +146,7 @@ const LayoutBase = (props) => {
|
|||||||
{/* 页脚 */}
|
{/* 页脚 */}
|
||||||
<Footer title={siteConfig('TITLE')} />
|
<Footer title={siteConfig('TITLE')} />
|
||||||
</div>
|
</div>
|
||||||
|
</ThemeGlobalNext.Provider>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user