isBrowser方法调整

This commit is contained in:
tangly1024.com
2023-08-01 12:47:33 +08:00
parent 115b43b429
commit 6eb50b1138
28 changed files with 41 additions and 42 deletions

View File

@@ -62,7 +62,7 @@ const ValineComponent = dynamic(() => import('@/components/ValineComponent'), {
const Comment = ({ frontMatter, className }) => {
const router = useRouter()
if (isBrowser() && ('giscus' in router.query || router.query.target === 'comment')) {
if (isBrowser && ('giscus' in router.query || router.query.target === 'comment')) {
setTimeout(() => {
const url = router.asPath.replace('?target=comment', '')
history.replaceState({}, '', url)

View File

@@ -8,7 +8,7 @@ import { isBrowser, loadExternalResource } from '@/lib/utils'
* @returns
*/
const ExternalScript = () => {
if (isBrowser()) {
if (isBrowser) {
// 静态导入本地自定义样式
loadExternalResource('/css/custom.css', 'css')
loadExternalResource('/js/custom.js', 'js')

View File

@@ -9,7 +9,7 @@ const FullScreenButton = () => {
const [isFullScreen, setIsFullScreen] = useState(false)
const handleFullScreenClick = () => {
if (!isBrowser()) {
if (!isBrowser) {
return
}
const element = document.documentElement

View File

@@ -65,7 +65,7 @@ const NotionPage = ({ post, className }) => {
// 将相册gallery下的图片加入放大功能
if (JSON.parse(BLOG.POST_DISABLE_GALLERY_CLICK)) {
setTimeout(() => {
if (isBrowser()) {
if (isBrowser) {
const imgList = document?.querySelectorAll('.notion-collection-card-cover img')
if (imgList && zoomRef.current) {
for (let i = 0; i < imgList.length; i++) {

View File

@@ -13,8 +13,7 @@ import { useEffect } from 'react'
const Twikoo = ({ isDarkMode }) => {
const loadTwikoo = async () => {
try {
const url = await loadExternalResource(BLOG.COMMENT_TWIKOO_CDN_URL, 'js')
console.log('twikoo 加载成功', url)
await loadExternalResource(BLOG.COMMENT_TWIKOO_CDN_URL, 'js')
const twikoo = window.twikoo
twikoo.init({
envId: BLOG.COMMENT_TWIKOO_ENV_ID, // 腾讯云环境填 envIdVercel 环境填地址https://xxx.vercel.app

View File

@@ -91,7 +91,7 @@ export function GlobalContextProvider(props) {
* @param {*} setTheme
*/
const initTheme = () => {
if (isBrowser()) {
if (isBrowser) {
setTimeout(() => {
const elements = document.querySelectorAll('[id^="theme-"]')
if (elements?.length > 1) {

View File

@@ -64,7 +64,7 @@ export function generateLocaleDict(langString) {
* 根据用户当前浏览器语言进行切换
*/
export function initLocale(lang, locale, changeLang, changeLocale) {
if (isBrowser()) {
if (isBrowser) {
const queryLang = getQueryVariable('lang') || loadLangFromCookies() || window.navigator.language
let currentLang = lang
if (queryLang !== lang) {

View File

@@ -1,6 +1,12 @@
// 封装异步加载资源的方法
import { memo } from 'react'
/**
* 判断是否客户端
* @returns {boolean}
*/
export const isBrowser = typeof window !== 'undefined'
/**
* 组件持久化
*/
@@ -58,7 +64,7 @@ export function loadExternalResource(url, type) {
* @returns
*/
export function getQueryVariable(key) {
const query = isBrowser() ? window.location.search.substring(1) : ''
const query = isBrowser ? window.location.search.substring(1) : ''
const vars = query.split('&')
for (let i = 0; i < vars.length; i++) {
const pair = vars[i].split('=')
@@ -143,12 +149,6 @@ export function deepClone(obj) {
*/
export const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
/**
* 判断是否客户端
* @returns {boolean}
*/
export const isBrowser = () => typeof window !== 'undefined'
/**
* 获取从第1页到指定页码的文章
* @param pageIndex 第几页
@@ -168,7 +168,7 @@ export const getListByPage = function (list, pageIndex, pageSize) {
*/
export const isMobile = () => {
let isMobile = false
if (!isBrowser()) {
if (!isBrowser) {
return isMobile
}

View File

@@ -41,7 +41,7 @@ const Slug = props => {
// 404
if (!post) {
setTimeout(() => {
if (isBrowser()) {
if (isBrowser) {
const article = document.getElementById('notion-article')
if (!article) {
router.push('/404').then(() => {

View File

@@ -15,7 +15,7 @@ const ArchiveIndex = props => {
const Layout = getLayoutByTheme(useRouter())
useEffect(() => {
if (isBrowser()) {
if (isBrowser) {
const anchor = window.location.hash
if (anchor) {
setTimeout(() => {

View File

@@ -177,7 +177,7 @@ const LayoutSearch = props => {
const slotTop = <div className='pb-12'><SearchInput {...props} /></div>
const router = useRouter()
useEffect(() => {
if (isBrowser()) {
if (isBrowser) {
// 高亮搜索到的结果
const container = document.getElementById('posts-wrapper')
if (keyword && container) {

View File

@@ -77,7 +77,7 @@ const BlogListPage = ({ page = 1, posts = [], postCount, siteInfo }) => {
* @returns
*/
const calculateColumns = () => {
if (!isBrowser()) {
if (!isBrowser) {
return 3
} else {
if (window.innerWidth >= 1024) {

View File

@@ -57,7 +57,7 @@ const LayoutBase = (props) => {
// 在组件卸载时保存 open 状态到本地存储中
useEffect(() => {
if (isBrowser()) {
if (isBrowser) {
localStorage.setItem('fukasawa-sidebar-collapse', isCollapsed)
}
}, [isCollapsed])
@@ -149,7 +149,7 @@ const LayoutSearch = props => {
const { keyword } = props
const router = useRouter()
useEffect(() => {
if (isBrowser()) {
if (isBrowser) {
replaceSearchResult({
doms: document.getElementById('posts-wrapper'),
search: keyword,

View File

@@ -49,7 +49,7 @@ const Catalog = ({ post }) => {
}
setActiveSection(currentSectionId)
const index = tocIds.indexOf(currentSectionId) || 0
if (isBrowser() && tocIds?.length > 0) {
if (isBrowser && tocIds?.length > 0) {
for (const tocWrapper of document?.getElementsByClassName('toc-wrapper')) {
tocWrapper?.scrollTo({ top: 28 * index, behavior: 'smooth' })
}

View File

@@ -10,7 +10,7 @@ const Progress = ({ targetRef, showPercent = true }) => {
const currentRef = targetRef?.current || targetRef
const [percent, changePercent] = useState(0)
const scrollListener = () => {
const target = currentRef || (isBrowser() && document.getElementById('posts-wrapper'))
const target = currentRef || (isBrowser && document.getElementById('posts-wrapper'))
if (target) {
const clientHeight = target.clientHeight
const scrollY = window.pageYOffset

View File

@@ -170,7 +170,7 @@ const LayoutIndex = (props) => {
router.push(CONFIG.INDEX_PAGE).then(() => {
// console.log('跳转到指定首页', CONFIG.INDEX_PAGE)
setTimeout(() => {
if (isBrowser()) {
if (isBrowser) {
const article = document.getElementById('notion-article')
if (!article) {
console.log('请检查您的Notion数据库中是否包含此slug页面 ', CONFIG.INDEX_PAGE)

View File

@@ -84,12 +84,12 @@ const NavBar = props => {
}
}
if (isBrowser()) {
if (isBrowser) {
window.addEventListener('scroll', handleScroll)
}
return () => {
if (isBrowser()) {
if (isBrowser) {
window.removeEventListener('scroll', handleScroll)
}
}

View File

@@ -11,7 +11,7 @@ export function Swipe({ items }) {
const [activeIndex, setActiveIndex] = useState(0)
const handleClick = (item) => {
if (isBrowser()) {
if (isBrowser) {
window.open(item?.url)
}
}

View File

@@ -10,7 +10,7 @@ const Progress = ({ targetRef, showPercent = true }) => {
const currentRef = targetRef?.current || targetRef
const [percent, changePercent] = useState(0)
const scrollListener = () => {
const target = currentRef || (isBrowser() && document.getElementById('article-wrapper'))
const target = currentRef || (isBrowser && document.getElementById('article-wrapper'))
if (target) {
const clientHeight = target.clientHeight
const scrollY = window.pageYOffset

View File

@@ -191,7 +191,7 @@ const LayoutSlug = props => {
const { post, lock, validPassword } = props
const drawerRight = useRef(null)
const targetRef = isBrowser() ? document.getElementById('article-wrapper') : null
const targetRef = isBrowser ? document.getElementById('article-wrapper') : null
const floatSlot = <>
{post?.toc?.length > 1 && <div className="block lg:hidden">
@@ -254,7 +254,7 @@ const Layout404 = props => {
useEffect(() => {
// 延时3秒如果加载失败就返回首页
setTimeout(() => {
if (isBrowser()) {
if (isBrowser) {
const article = document.getElementById('notion-article')
if (!article) {
router.push('/').then(() => {

View File

@@ -11,7 +11,7 @@ const Progress = ({ targetRef, showPercent = true }) => {
const [percent, changePercent] = useState(0)
const scrollListener = () => {
requestAnimationFrame(() => {
const target = currentRef || (isBrowser() && document.getElementById('article-wrapper'))
const target = currentRef || (isBrowser && document.getElementById('article-wrapper'))
if (target) {
const clientHeight = target.clientHeight
const scrollY = window.pageYOffset

View File

@@ -10,7 +10,7 @@ const Progress = ({ targetRef, showPercent = true }) => {
const currentRef = targetRef?.current || targetRef
const [percent, changePercent] = useState(0)
const scrollListener = () => {
const target = currentRef || (isBrowser() && document.getElementById('article-wrapper'))
const target = currentRef || (isBrowser && document.getElementById('article-wrapper'))
if (target) {
const clientHeight = target.clientHeight
const scrollY = window.pageYOffset

View File

@@ -206,7 +206,7 @@ const LayoutSearch = (props) => {
const currentSearch = keyword || router?.query?.s
useEffect(() => {
if (isBrowser()) {
if (isBrowser) {
replaceSearchResult({
doms: document.getElementById('posts-wrapper'),
search: keyword,

View File

@@ -10,7 +10,7 @@ const Progress = ({ targetRef, showPercent = true }) => {
const currentRef = targetRef?.current || targetRef
const [percent, changePercent] = useState(0)
const scrollListener = () => {
const target = currentRef || (isBrowser() && document.getElementById('article-wrapper'))
const target = currentRef || (isBrowser && document.getElementById('article-wrapper'))
if (target) {
const clientHeight = target.clientHeight
const scrollY = window.pageYOffset

View File

@@ -161,7 +161,7 @@ const LayoutSearch = (props) => {
const { posts, keyword } = props
useEffect(() => {
if (isBrowser()) {
if (isBrowser) {
replaceSearchResult({
doms: document.getElementById('posts-wrapper'),
search: keyword,
@@ -201,7 +201,7 @@ const Layout404 = props => {
useEffect(() => {
// 延时3秒如果加载失败就返回首页
setTimeout(() => {
const article = isBrowser() && document.getElementById('article-wrapper')
const article = isBrowser && document.getElementById('article-wrapper')
if (!article) {
router.push('/').then(() => {
// console.log('找不到页面', router.asPath)
@@ -253,7 +253,7 @@ const LayoutArchive = (props) => {
const LayoutSlug = (props) => {
const { post, lock, validPassword } = props
const drawerRight = useRef(null)
const targetRef = isBrowser() ? document.getElementById('article-wrapper') : null
const targetRef = isBrowser ? document.getElementById('article-wrapper') : null
const floatSlot = <div className='block lg:hidden'>
<TocDrawerButton onClick={() => {
drawerRight?.current?.handleSwitchVisible()

View File

@@ -134,7 +134,7 @@ const LayoutPostList = props => {
const LayoutSearch = props => {
const { keyword } = props
useEffect(() => {
if (isBrowser()) {
if (isBrowser) {
replaceSearchResult({
doms: document.getElementById('posts-wrapper'),
search: keyword,

View File

@@ -132,7 +132,7 @@ const LayoutSearch = props => {
const { keyword } = props
useEffect(() => {
if (isBrowser()) {
if (isBrowser) {
replaceSearchResult({
doms: document.getElementById('posts-wrapper'),
search: keyword,

View File

@@ -36,7 +36,7 @@ const LayoutBase = props => {
const { children, slotTop, meta } = props
const { onLoading } = useGlobal()
if (isBrowser()) {
if (isBrowser) {
loadExternalResource('/css/theme-simple.css', 'css')
}
return (
@@ -122,7 +122,7 @@ const LayoutSearch = props => {
const { keyword } = props
useEffect(() => {
if (isBrowser()) {
if (isBrowser) {
replaceSearchResult({
doms: document.getElementById('posts-wrapper'),
search: keyword,