Skip to content

Commit

Permalink
Merge pull request tangly1024#1762 from tangly1024/feat/algolia-suppo…
Browse files Browse the repository at this point in the history
…rt-for-theme-simple

Simple主题支持Algolia搜索
  • Loading branch information
tangly1024 authored Jan 9, 2024
2 parents 1a92097 + 88a9913 commit 0acf2d7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
6 changes: 3 additions & 3 deletions lib/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ export const loadLangFromCookies = () => {
}

/**
* 保存语言
* @param newTheme
*/
* 保存语言
* @param newTheme
*/
export const saveLangToCookies = (lang) => {
cookie.save('lang', lang, { path: '/' })
}
10 changes: 9 additions & 1 deletion themes/simple/components/NavBar.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { siteConfig } from '@/lib/config'
import { useRouter } from 'next/router'
import { useState } from 'react'
import { useSimpleGlobal } from '..'
import { MenuList } from './MenuList'

/**
Expand All @@ -10,9 +12,15 @@ import { MenuList } from './MenuList'
export default function NavBar (props) {
const [showSearchInput, changeShowSearchInput] = useState(false)
const router = useRouter()
const { searchModal } = useSimpleGlobal()

// 展示搜索框
const toggleShowSearchInput = () => {
changeShowSearchInput(!showSearchInput)
if (siteConfig('ALGOLIA_APP_ID')) {
searchModal.current.openSearch()
} else {
changeShowSearchInput(!showSearchInput)
}
}

const onKeyUp = (e) => {
Expand Down
18 changes: 15 additions & 3 deletions themes/simple/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import CONFIG from './config'
import { useEffect } from 'react'
import { createContext, useContext, useEffect, useRef } from 'react'
import { isBrowser } from '@/lib/utils'
import { useGlobal } from '@/lib/global'
import { AdSlot } from '@/components/GoogleAdsense'
Expand All @@ -10,7 +10,7 @@ import { Style } from './style'
import replaceSearchResult from '@/components/Mark'
import dynamic from 'next/dynamic'
import NotionPage from '@/components/NotionPage'
// const NotionPage = dynamic(() => import('@/components/NotionPage'), { ssr: false });
import AlgoliaSearchModal from '@/components/AlgoliaSearchModal'

// 主题组件
const BlogListScroll = dynamic(() => import('./components/BlogListScroll'), { ssr: false });
Expand All @@ -31,6 +31,10 @@ const CommonHead = dynamic(() => import('@/components/CommonHead'), { ssr: false
const WWAds = dynamic(() => import('@/components/WWAds'), { ssr: false });
const BlogListPage = dynamic(() => import('./components/BlogListPage'), { ssr: false })

// 主题全局状态
const ThemeGlobalSimple = createContext()
export const useSimpleGlobal = () => useContext(ThemeGlobalSimple)

/**
* 基础布局
*
Expand All @@ -40,8 +44,10 @@ const BlogListPage = dynamic(() => import('./components/BlogListPage'), { ssr: f
const LayoutBase = props => {
const { children, slotTop, meta } = props
const { onLoading, fullWidth } = useGlobal()
const searchModal = useRef(null)

return (
<ThemeGlobalSimple.Provider value={{ searchModal }}>
<div id='theme-simple' className='min-h-screen flex flex-col dark:text-gray-300 bg-white dark:bg-black'>
{/* SEO相关 */}
<CommonHead meta={meta}/>
Expand Down Expand Up @@ -88,9 +94,13 @@ const LayoutBase = props => {
<JumpToTopButton />
</div>

{/* 搜索框 */}
<AlgoliaSearchModal cRef={searchModal} {...props}/>

<Footer {...props} />

</div>
</ThemeGlobalSimple.Provider>
)
}

Expand Down Expand Up @@ -138,7 +148,9 @@ const LayoutSearch = props => {
}
}, [])

return <LayoutPostList {...props} slotTop={<SearchInput {...props} />} />
const slotTop = siteConfig('ALGOLIA_APP_ID') ? null : <SearchInput {...props} />

return <LayoutPostList {...props} slotTop={slotTop} />
}

/**
Expand Down

1 comment on commit 0acf2d7

@vercel
Copy link

@vercel vercel bot commented on 0acf2d7 Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.