diff --git a/themes/starter/components/ArticleLock.js b/themes/starter/components/ArticleLock.js new file mode 100644 index 00000000..3a5f4b4c --- /dev/null +++ b/themes/starter/components/ArticleLock.js @@ -0,0 +1,53 @@ +import { useGlobal } from '@/lib/global' +import { useEffect, useRef } from 'react' + +/** + * 加密文章校验组件 + * @param {password, validPassword} props + * @param password 正确的密码 + * @param validPassword(bool) 回调函数,校验正确回调入参为true + * @returns + */ +export const ArticleLock = props => { + const { validPassword } = props + const { locale } = useGlobal() + + const submitPassword = () => { + const p = document.getElementById('password') + if (!validPassword(p?.value)) { + const tips = document.getElementById('tips') + if (tips) { + tips.innerHTML = '' + tips.innerHTML = `
${locale.COMMON.PASSWORD_ERROR}
` + } + } + } + + const passwordInputRef = useRef(null) + useEffect(() => { + // 选中密码输入框并将其聚焦 + passwordInputRef.current.focus() + }, []) + + return
+
+
{locale.COMMON.ARTICLE_LOCK_TIPS}
+
+ { + if (e.key === 'Enter') { + submitPassword() + } + }} + ref={passwordInputRef} // 绑定ref到passwordInputRef变量 + className='outline-none w-full text-sm pl-5 rounded-l transition focus:shadow-lg font-light leading-10 text-black dark:bg-gray-500 bg-gray-50' + > +
+  {locale.COMMON.SUBMIT} +
+
+
+
+
+
+} diff --git a/themes/starter/index.js b/themes/starter/index.js index 21298c42..922c05fe 100644 --- a/themes/starter/index.js +++ b/themes/starter/index.js @@ -30,6 +30,7 @@ import { useGlobal } from '@/lib/global' import { loadWowJS } from '@/lib/plugins/wow' import { SignIn, SignUp } from '@clerk/nextjs' import Link from 'next/link' +import { ArticleLock } from './components/ArticleLock' import { Banner } from './components/Banner' import { CTA } from './components/CTA' import { SignInForm } from './components/SignInForm' @@ -126,7 +127,7 @@ const LayoutIndex = props => { * @returns */ const LayoutSlug = props => { - const { post } = props + const { post, lock, validPassword } = props // 如果 是 /article/[slug] 的文章路径则視情況进行重定向到另一个域名 const router = useRouter() @@ -153,11 +154,15 @@ const LayoutSlug = props => {
-
- - - -
+ {lock && } + + {!lock && ( +
+ + + +
+ )}