Skip to content

Commit

Permalink
登录结合OpenWrite
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed Sep 24, 2024
1 parent 32a113f commit 964513b
Show file tree
Hide file tree
Showing 18 changed files with 244 additions and 24 deletions.
28 changes: 19 additions & 9 deletions components/OpenWrite.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { siteConfig } from '@/lib/config'
import { useGlobal } from '@/lib/global'
import { isBrowser, loadExternalResource } from '@/lib/utils'
import { useRouter } from 'next/router'
import { useEffect } from 'react'
Expand All @@ -24,14 +25,10 @@ const OpenWrite = () => {
// 白名单
const whiteList = siteConfig('OPEN_WRITE_WHITE_LIST', '')

const loadOpenWrite = async () => {
const existWhite = existedWhiteList(router.asPath, whiteList)

// 如果当前页面在白名单中,则屏蔽加锁
if (existWhite) {
return
}
// 登录信息
const { isLoaded, isSignedIn } = useGlobal()

const loadOpenWrite = async () => {
try {
await loadExternalResource(
'https://readmore.openwrite.cn/js/readmore-2.0.js',
Expand Down Expand Up @@ -74,11 +71,24 @@ const OpenWrite = () => {
}
}
useEffect(() => {
const existWhite = existedWhiteList(router.asPath, whiteList)
// 白名单中,免检
if (existWhite) {
return
}
if (isSignedIn) {
// 用户已登录免检
console.log('用户已登录')
return
}

// 开发环境免检
if (process.env.NODE_ENV === 'development') {
console.log('开发环境:屏蔽OpenWrite')
return
}
if (isBrowser && blogId) {

if (isBrowser && blogId && !isSignedIn) {
toggleTocItems(true) // 禁止目录项的点击

// Check if the element with id 'read-more-wrap' already exists
Expand All @@ -89,7 +99,7 @@ const OpenWrite = () => {
loadOpenWrite()
}
}
})
}, [isLoaded, router])

// 启动一个监听器,当页面上存在#read-more-wrap对象时,所有的 a .notion-table-of-contents-item 对象都禁止点击

Expand Down
14 changes: 14 additions & 0 deletions lib/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
initDarkMode,
saveDarkModeToLocalStorage
} from '@/themes/theme'
import { useUser } from '@clerk/nextjs'
import { useRouter } from 'next/router'
import { createContext, useContext, useEffect, useState } from 'react'
import {
Expand All @@ -13,6 +14,10 @@ import {
redirectUserLang,
saveLangToLocalStorage
} from './lang'

/**
* 全局上下文
*/
const GlobalContext = createContext()

export function GlobalContextProvider(props) {
Expand All @@ -37,6 +42,12 @@ export function GlobalContextProvider(props) {
const [onLoading, setOnLoading] = useState(true) // 抓取文章数据
const router = useRouter()

// 登录验证相关
const enableClerk = process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY
const { isLoaded, isSignedIn, user } = enableClerk
? useUser()
: { isLoaded: true, isSignedIn: false, user: false }

// 是否全屏
const fullWidth = post?.fullWidth ?? false

Expand Down Expand Up @@ -119,6 +130,9 @@ export function GlobalContextProvider(props) {
return (
<GlobalContext.Provider
value={{
isLoaded,
isSignedIn,
user,
fullWidth,
NOTION_CONFIG,
THEME_CONFIG,
Expand Down
2 changes: 2 additions & 0 deletions lib/lang/en-US.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export default {
},
COMMON: {
THEME: 'Theme',
SIGN_IN: 'Sign In',
SIGN_OUT: 'Sign Out',
ARTICLE_LIST: 'Article List',
RECOMMEND_POSTS: 'Recommend Posts',
MORE: 'More',
Expand Down
2 changes: 2 additions & 0 deletions lib/lang/zh-CN.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export default {
},
COMMON: {
THEME: 'Theme',
SIGN_IN: '登录',
SIGN_OUT: '登出',
ARTICLE_LIST: '文章列表',
RECOMMEND_POSTS: '推荐文章',
MORE: '更多',
Expand Down
2 changes: 2 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ const nextConfig = {
},
webpack: (config, { dev, isServer }) => {
// 动态主题:添加 resolve.alias 配置,将动态路径映射到实际路径
config.resolve.alias['@'] = path.resolve(__dirname)

if (!isServer) {
console.log('[默认主题]', path.resolve(__dirname, 'themes', THEME))
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"build-all-in-dev": "cross-env VERCEL_ENV=production next build"
},
"dependencies": {
"@clerk/localizations": "^3.0.4",
"@clerk/nextjs": "^5.1.5",
"@headlessui/react": "^1.7.15",
"@next/bundle-analyzer": "^12.1.1",
Expand Down
12 changes: 10 additions & 2 deletions pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import { getQueryParam } from '../lib/utils'
import BLOG from '@/blog.config'
import ExternalPlugins from '@/components/ExternalPlugins'
import GlobalHead from '@/components/GlobalHead'
import { zhCN } from '@clerk/localizations'
import { ClerkProvider } from '@clerk/nextjs'

/**
* App挂载DOM 入口文件
* @param {*} param0
Expand Down Expand Up @@ -57,7 +57,15 @@ const MyApp = ({ Component, pageProps }) => {
<ExternalPlugins {...pageProps} />
</GlobalContextProvider>
)
return <>{enableClerk ? <ClerkProvider>{content}</ClerkProvider> : content}</>
return (
<>
{enableClerk ? (
<ClerkProvider localization={zhCN}>{content}</ClerkProvider>
) : (
content
)}
</>
)
}

export default MyApp
1 change: 0 additions & 1 deletion pages/sign-in/[[...index]].js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// import BLOG from '@/blog.config'
import BLOG from '@/blog.config'
import { siteConfig } from '@/lib/config'
import { getGlobalData } from '@/lib/db/getSiteData'
Expand Down
3 changes: 2 additions & 1 deletion themes/gitbook/components/ArticleInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export default function ArticleInfo({ post }) {
return (
<div className='pt-10 pb-6 text-gray-400 text-sm'>
<i className='fa-regular fa-clock mr-1' />
Last update: {post.date?.start_date}
Last update:{' '}
{post.date?.start_date || post?.publishDay || post?.lastEditedDay}
</div>
)
}
16 changes: 16 additions & 0 deletions themes/gitbook/components/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Collapse from '@/components/Collapse'
import DarkModeButton from '@/components/DarkModeButton'
import { siteConfig } from '@/lib/config'
import { useGlobal } from '@/lib/global'
import { SignInButton, SignedOut, UserButton } from '@clerk/nextjs'
import { useRef, useState } from 'react'
import CONFIG from '../config'
import LogoBar from './LogoBar'
Expand Down Expand Up @@ -59,6 +60,8 @@ export default function Header(props) {
links = customMenu
}

const enableClerk = process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY

return (
<div id='top-nav' className={'fixed top-0 w-full z-20 ' + className}>
{/* PC端菜单 */}
Expand All @@ -79,6 +82,19 @@ export default function Header(props) {

{/* 右侧 */}
<div className='flex items-center gap-4'>
{/* 登录相关 */}
{enableClerk && (
<>
<SignedOut>
<SignInButton mode='modal'>
<button className='bg-green-500 hover:bg-green-600 text-white rounded-lg px-3 py-2'>
{locale.COMMON.SIGN_IN}
</button>
</SignInButton>
</SignedOut>
<UserButton />
</>
)}
<DarkModeButton className='text-sm items-center h-full hidden md:flex' />
<SearchInput className='hidden md:flex md:w-52 lg:w-72' />
{/* 折叠按钮、仅移动端显示 */}
Expand Down
57 changes: 56 additions & 1 deletion themes/gitbook/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { siteConfig } from '@/lib/config'
import { useGlobal } from '@/lib/global'
import { isBrowser } from '@/lib/utils'
import { getShortId } from '@/lib/utils/pageId'
import { SignIn, SignUp } from '@clerk/nextjs'
import dynamic from 'next/dynamic'
import Link from 'next/link'
import { useRouter } from 'next/router'
Expand Down Expand Up @@ -164,7 +165,7 @@ const LayoutBase = props => {
{/* 中间内容区域 */}
<div
id='center-wrapper'
className='dark:bg-hexo-black-gray flex flex-col justify-between w-full relative z-10 pt-14 min-h-screen'>
className='flex flex-col justify-between w-full relative z-10 pt-14 min-h-screen'>
<div
id='container-inner'
className={`w-full ${fullWidth ? 'px-5' : 'max-w-3xl px-3 lg:px-0'} justify-center mx-auto`}>
Expand Down Expand Up @@ -473,6 +474,58 @@ const LayoutTagIndex = props => {
)
}

/**
* 登录页面
* @param {*} props
* @returns
*/
const LayoutSignIn = props => {
const { post } = props
const enableClerk = process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY

return (
<>
<div className='grow mt-20'>
{/* clerk预置表单 */}
{enableClerk && (
<div className='flex justify-center py-6'>
<SignIn />
</div>
)}
<div id='article-wrapper'>
<NotionPage post={post} />
</div>
</div>
</>
)
}

/**
* 注册页面
* @param {*} props
* @returns
*/
const LayoutSignUp = props => {
const { post } = props
const enableClerk = process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY

return (
<>
<div className='grow mt-20'>
{/* clerk预置表单 */}
{enableClerk && (
<div className='flex justify-center py-6'>
<SignUp />
</div>
)}
<div id='article-wrapper'>
<NotionPage post={post} />
</div>
</div>
</>
)
}

export {
Layout404,
LayoutArchive,
Expand All @@ -481,6 +534,8 @@ export {
LayoutIndex,
LayoutPostList,
LayoutSearch,
LayoutSignIn,
LayoutSignUp,
LayoutSlug,
LayoutTagIndex,
CONFIG as THEME_CONFIG
Expand Down
3 changes: 0 additions & 3 deletions themes/magzine/components/Catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ const Catalog = ({ post, toc, className }) => {
actionSectionScrollSpy()
window.addEventListener('scroll', actionSectionScrollSpy)
}
setTimeout(() => {
console.log('目录', post, toc)
}, 1000)
return () => {
window.removeEventListener('scroll', actionSectionScrollSpy)
}
Expand Down
21 changes: 20 additions & 1 deletion themes/magzine/components/Header.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Collapse from '@/components/Collapse'
import { siteConfig } from '@/lib/config'
import { useGlobal } from '@/lib/global'
import { SignInButton, SignedOut, UserButton } from '@clerk/nextjs'
import throttle from 'lodash.throttle'
import { useRouter } from 'next/router'
import { useEffect, useRef, useState } from 'react'
Expand Down Expand Up @@ -112,6 +113,8 @@ export default function Header(props) {
return null
}

const enableClerk = process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY

return (
<div
id='top-navbar-wrapper'
Expand Down Expand Up @@ -154,7 +157,7 @@ export default function Header(props) {
</>
)}

{/* 右侧移动端折叠按钮 */}
{/* 右侧按钮 */}
<div className='flex items-center gap-x-2 pr-2'>
{/* 搜索按钮 */}
<div
Expand All @@ -167,6 +170,8 @@ export default function Header(props) {
: 'fa-solid fa-magnifying-glass' + ' align-middle'
}></i>
</div>

{/* 移动端显示开关 */}
<div className='mr-1 flex md:hidden justify-end items-center text-lg space-x-4 font-serif dark:text-gray-200'>
<div onClick={toggleMenuOpen} className='cursor-pointer p-2'>
{isOpen ? (
Expand All @@ -176,6 +181,20 @@ export default function Header(props) {
)}
</div>
</div>

{/* 登录相关 */}
{enableClerk && (
<>
<SignedOut>
<SignInButton mode='modal'>
<button className='bg-gray-800 hover:bg-gray-900 text-white rounded-lg px-3 py-2'>
{locale.COMMON.SIGN_IN}
</button>
</SignInButton>
</SignedOut>
<UserButton />
</>
)}
</div>
</div>

Expand Down
Loading

0 comments on commit 964513b

Please sign in to comment.