Skip to content

Commit

Permalink
Gitbook 主题微调
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed Jun 22, 2024
1 parent 96d5e2e commit c33ec22
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 92 deletions.
134 changes: 87 additions & 47 deletions components/ThemeSwitch.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,126 @@
import { useGlobal } from '@/lib/global'
import { useState } from 'react'
import { Draggable } from './Draggable'
import LANGS from '@/lib/lang'
import { getQueryParam } from '@/lib/utils'
import { THEMES } from '@/themes/theme'
import { useRouter } from 'next/router'
import { useState } from 'react'
import DarkModeButton from './DarkModeButton'
import { getQueryParam } from '@/lib/utils'
import LANGS from '@/lib/lang'
import { Draggable } from './Draggable'
/**
*
* @returns 主题切换
*/
const ThemeSwitch = () => {
const { theme, lang, changeLang, locale, isDarkMode, toggleDarkMode } = useGlobal()
const { theme, lang, changeLang, locale, isDarkMode, toggleDarkMode } =
useGlobal()
const router = useRouter()
const currentTheme = getQueryParam(router.asPath, 'theme') || theme
// const currentLang = getQueryParam(router.asPath, 'lang') || lang
const [isLoading, setIsLoading] = useState(false)

// 修改当前路径url中的 theme 参数
// 例如 http://localhost?theme=hexo 跳转到 http://localhost?theme=newTheme
const onThemeSelectChange = (e) => {
document.ontouchmove = document.ontouchend = document.onmousemove = document.onmouseup = null
const onThemeSelectChange = e => {
document.ontouchmove =
document.ontouchend =
document.onmousemove =
document.onmouseup =
null
setIsLoading(true)
const newTheme = e.target.value
const query = router.query
query.theme = newTheme
router.push({ pathname: router.pathname, query }).then(() => {
setTimeout(() => {
setIsLoading(false)
}, 500);
}, 500)
})
}

const onLangSelectChange = (e) => {
document.ontouchmove = document.ontouchend = document.onmousemove = document.onmouseup = null
const onLangSelectChange = e => {
document.ontouchmove =
document.ontouchend =
document.onmousemove =
document.onmouseup =
null
const newLang = e.target.value
changeLang(newLang)
}

return (<>
<Draggable>
<div id="draggableBox" style={{ left: '0px', top: '80vh' }} className="fixed group space-y-2 overflow-hidden z-50 p-3 flex flex-col items-start dark:text-white bg-white dark:bg-black rounded-xl shadow-lg ">
{/* 深色按钮 */}
<div className="text-sm flex items-center w-0 group-hover:w-32 transition-all duration-200">
<DarkModeButton />
<div onClick={toggleDarkMode} className='cursor-pointer w-0 group-hover:w-24 transition-all duration-200 overflow-hidden whitespace-nowrap pl-1 h-auto'>{isDarkMode ? locale.MENU.DARK_MODE : locale.MENU.LIGHT_MODE}</div>
</div>
return (
<>
<Draggable>
<div
id='draggableBox'
style={{ left: '0px', top: '80vh' }}
className='fixed group hover:scale-105 transition-all space-y-2 overflow-hidden z-50 p-3 flex flex-col items-start dark:text-white bg-white dark:bg-black rounded-xl shadow-lg '>
{/* 主题切换按钮 */}
<div className='text-sm flex items-center group-hover:w-32 transition-all duration-200'>
<i className='fa-solid fa-palette w-5' />
<div className='w-0 group-hover:w-24 transition-all duration-200 overflow-hidden'>
<label htmlFor='themeSelect' className='sr-only'>
{locale.COMMON.THEME}
</label>
<select
id='themeSelect'
value={currentTheme}
onChange={onThemeSelectChange}
name='themes'
className='pl-1 bg-gray-50 dark:bg-black appearance-none outline-none dark:text-white uppercase cursor-pointer'>
{THEMES?.map(t => {
return (
<option key={t} value={t}>
{t}
</option>
)
})}
</select>
</div>
</div>

{/* 翻译按钮 */}
<div className="text-sm flex items-center group-hover:w-32 transition-all duration-200">
<i className="fa-solid fa-language w-5" />
<div className='w-0 group-hover:w-24 transition-all duration-200 overflow-hidden'>
<label htmlFor="langSelect" className="sr-only">选择语言:</label>
<select id="langSelect" value={lang} onChange={onLangSelectChange} name="themes" className='pl-1 bg-gray-50 dark:bg-black appearance-none outline-none dark:text-white uppercase cursor-pointer'>
{Object.keys(LANGS)?.map(t => {
return <option key={t} value={t}>{LANGS[t].LOCALE}</option>
})}
</select>
</div>
</div>
{/* 深色按钮 */}
<div className='text-sm flex items-center w-0 group-hover:w-32 transition-all duration-200'>
<DarkModeButton />
<div
onClick={toggleDarkMode}
className='cursor-pointer w-0 group-hover:w-24 transition-all duration-200 overflow-hidden whitespace-nowrap pl-1 h-auto'>
{isDarkMode ? locale.MENU.DARK_MODE : locale.MENU.LIGHT_MODE}
</div>
</div>

{/* 主题切换按钮 */}
<div className="text-sm flex items-center group-hover:w-32 transition-all duration-200">
<i className="fa-solid fa-palette w-5" />
<div className='w-0 group-hover:w-24 transition-all duration-200 overflow-hidden'>
<label htmlFor="themeSelect" className="sr-only">选择主题:</label>
<select id="themeSelect" value={currentTheme} onChange={onThemeSelectChange} name="themes" className='pl-1 bg-gray-50 dark:bg-black appearance-none outline-none dark:text-white uppercase cursor-pointer'>
{THEMES?.map(t => {
return <option key={t} value={t}>{t}</option>
})}
</select>
</div>
</div>
{/* 翻译按钮 */}
<div className='text-sm flex items-center group-hover:w-32 transition-all duration-200'>
<i className='fa-solid fa-language w-5' />
<div className='w-0 group-hover:w-24 transition-all duration-200 overflow-hidden'>
<label htmlFor='langSelect' className='sr-only'>
选择语言:
</label>
<select
id='langSelect'
value={lang}
onChange={onLangSelectChange}
name='themes'
className='pl-1 bg-gray-50 dark:bg-black appearance-none outline-none dark:text-white uppercase cursor-pointer'>
{Object.keys(LANGS)?.map(t => {
return (
<option key={t} value={t}>
{LANGS[t].LOCALE}
</option>
)
})}
</select>
</div>
</Draggable>
</div>
</div>
</Draggable>

{/* 切换主题加载时的全屏遮罩 */}
<div className={`${isLoading ? 'opacity-90 ' : 'opacity-0'}
{/* 切换主题加载时的全屏遮罩 */}
<div
className={`${isLoading ? 'opacity-90 ' : 'opacity-0'}
w-screen h-screen glassmorphism bg-black text-white shadow-text flex justify-center items-center
transition-all fixed top-0 left-0 pointer-events-none duration-1000 z-50 shadow-inner`}>
<i className='text-3xl mr-5 fas fa-spinner animate-spin' />
</div>
<i className='text-3xl mr-5 fas fa-spinner animate-spin' />
</div>
</>
)
}
Expand Down
3 changes: 3 additions & 0 deletions lib/lang/en-US.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ export default {
INDEX: 'Home',
RSS: 'RSS',
SEARCH: 'Search',
NAVIGATOR: 'NAV',
ABOUT: 'About',
MAIL: 'E-Mail',
ARCHIVE: 'Archive'
},
COMMON: {
THEME: 'Theme',
ARTICLE_LIST: 'Article List',
MORE: 'More',
NO_MORE: 'No More',
LATEST_POSTS: 'Latest posts',
Expand Down
4 changes: 3 additions & 1 deletion lib/lang/zh-CN.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export default {
ARCHIVE: '归档'
},
COMMON: {
THEME: 'Theme',
ARTICLE_LIST: '文章列表',
MORE: '更多',
NO_MORE: '没有更多了',
LATEST_POSTS: '最新发布',
Expand Down Expand Up @@ -63,7 +65,7 @@ export default {
MINUTE: '分钟',
WORD_COUNT: '字数',
READ_TIME: '阅读时长',
NEXT_POST:'下一篇'
NEXT_POST: '下一篇'
},
PAGINATION: {
PREV: '上页',
Expand Down
36 changes: 34 additions & 2 deletions lib/lang/zh-HK.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,47 @@ export default {
RSS: '訂閱',
SEARCH: '搜尋',
ABOUT: '關於',
MAIL: '電郵'
MAIL: '電郵',
NAVIGATOR: '導航',
ARCHIVE: '封存'
},
COMMON: {
ARTICLE_LIST: '文章列表',
MORE: '更多',
NO_MORE: '沒有更多了',
LATEST_POSTS: '最新文章',
TAGS: '標籤',
NO_TAG: '無標籤',
CATEGORY: '分類',
SHARE: '分享',
SCAN_QR_CODE: 'QRCode',
URL_COPIED: '連結已複製!',
TABLE_OF_CONTENTS: '目錄',
RELATE_POSTS: '相關文章',
COPYRIGHT: '著作權',
AUTHOR: '作者',
URL: '連結',
ANALYTICS: '分析',
POSTS: '篇文章',
ARTICLE: '文章',
VISITORS: '位訪客',
VIEWS: '次查看',
COPYRIGHT_NOTICE: '本文採用 CC BY-NC-SA 4.0 許可協議,轉載請註明出處。',
RESULT_OF_SEARCH: '篇搜尋到的结果',
ARTICLE_DETAIL: '完整文章',
PASSWORD_ERROR: '密碼錯誤!',
ARTICLE_LOCK_TIPS: '文章已上鎖,請輸入訪問密碼',
SUBMIT: '提交',
POST_TIME: '发布于',
LAST_EDITED_TIME: '最后更新'
},
PAGINATION: {
PREV: '上一頁',
NEXT: '下一頁'
},
SEARCH: {
ARTICLES: '搜尋文章',
TAGS: '搜尋標簽'
TAGS: '搜尋標籤'
},
POST: {
BACK: '返回',
Expand Down
1 change: 1 addition & 0 deletions lib/lang/zh-TW.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default {
ARCHIVE: '封存'
},
COMMON: {
ARTICLE_LIST: '文章列表',
MORE: '更多',
NO_MORE: '沒有更多了',
LATEST_POSTS: '最新文章',
Expand Down
36 changes: 20 additions & 16 deletions themes/gitbook/components/BottomMenuBar.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import { useGitBookGlobal } from '@/themes/gitbook'
import JumpToTopButton from './JumpToTopButton'
import MobileButtonCatalog from './MobileButtonCatalog'
import MobileButtonPageNav from './MobileButtonPageNav'

/**
* 移动端底部导航
* @param {*} param0
* @returns
*/
export default function BottomMenuBar({ post, className }) {
const { pageNavVisible, changePageNavVisible } = useGitBookGlobal()

const togglePageNavVisible = () => {
changePageNavVisible(!pageNavVisible)
}
const showTocButton = post?.toc?.length > 1

return (
<div className={'sticky z-10 bottom-0 w-full h-12 bg-white dark:bg-hexo-black-gray ' + className}>
<div className='flex justify-between h-full shadow-card'>
<div onClick={togglePageNavVisible} className='flex w-full items-center justify-center cursor-pointer'>
<i className="fa-solid fa-book"></i>
</div>
<div className='flex w-full items-center justify-center cursor-pointer'>
<JumpToTopButton />
</div>
</div>
<>
{/* 移动端底部导航按钮 */}
<div className='bottom-button-group md:hidden w-screen h-14 px-4 fixed flex items-center justify-between right-left bottom-0 z-30 bg-white border-t dark:border-gray-800'>
<div className='w-full'>
<MobileButtonPageNav />
</div>
{showTocButton && (
<div className='w-full'>
<MobileButtonCatalog />
</div>
)}
</div>
</>
)
}
6 changes: 5 additions & 1 deletion themes/gitbook/components/Footer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { siteConfig } from '@/lib/config'
import SocialButton from './SocialButton'

/**
* 站点也叫
* @param {*} param0
* @returns
*/
const Footer = ({ siteInfo }) => {
const d = new Date()
const currentYear = d.getFullYear()
Expand Down
10 changes: 7 additions & 3 deletions themes/gitbook/components/MobileButtonCatalog.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { useGlobal } from '@/lib/global'
import { useGitBookGlobal } from '@/themes/gitbook'

/**
* 移动端目录按钮
*/
export default function MobileButtonCatalog() {
const { tocVisible, changeTocVisible } = useGitBookGlobal()
const { locale } = useGlobal()

const toggleToc = () => {
changeTocVisible(!tocVisible)
Expand All @@ -19,9 +21,11 @@ export default function MobileButtonCatalog() {
<a
id='toc-button'
className={
'fa-list-ol cursor-pointer fas hover:scale-150 transform duration-200'
}
/>
'space-x-4 cursor-pointer hover:scale-150 transform duration-200'
}>
<i className='fa-list-ol fas' />
<span>{locale.COMMON.TABLE_OF_CONTENTS}</span>
</a>
</div>
)
}
13 changes: 8 additions & 5 deletions themes/gitbook/components/MobileButtonPageNav.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useGlobal } from '@/lib/global'
import { useGitBookGlobal } from '@/themes/gitbook'

/**
* 移动端文章导航按钮
*/
export default function MobileButtonPageNav() {
const { pageNavVisible, changePageNavVisible } = useGitBookGlobal()

const { locale } = useGlobal()
const togglePageNavVisible = () => {
changePageNavVisible(!pageNavVisible)
}
Expand All @@ -17,11 +18,13 @@ export default function MobileButtonPageNav() {
'text-black flex justify-center items-center dark:text-gray-200 dark:bg-hexo-black-gray py-2 px-2'
}>
<a
id='toc-button'
id='nav-button'
className={
'fa-book cursor-pointer fas hover:scale-150 transform duration-200'
}
/>
'space-x-4 cursor-pointer hover:scale-150 transform duration-200'
}>
<i className='fa-book fas' />
<span>{locale.COMMON.ARTICLE_LIST}</span>
</a>
</div>
)
}
2 changes: 1 addition & 1 deletion themes/gitbook/components/PageNavDrawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const PageNavDrawer = props => {
className={`${pageNavVisible ? 'animate__slideInLeft ' : '-ml-80 animate__slideOutLeft'}
overflow-y-hidden shadow-card w-72 duration-200 fixed left-1 bottom-16 rounded py-2 bg-white dark:bg-hexo-black-gray`}>
<div className='px-4 pb-2 flex justify-between items-center border-b font-bold'>
<span>{locale.COMMON.ARTICLE}</span>
<span>{locale.COMMON.ARTICLE_LIST}</span>
<i
className='fas fa-times p-1 cursor-pointer'
onClick={() => {
Expand Down
Loading

0 comments on commit c33ec22

Please sign in to comment.