2021-09-18 00:34:56 +08:00
|
|
|
<script setup lang="ts">
|
|
|
|
import { onMounted } from 'vue'
|
2021-09-27 16:45:42 +08:00
|
|
|
import { ElMessageBox } from 'element-plus'
|
2021-09-18 00:34:56 +08:00
|
|
|
import nprogress from 'nprogress'
|
2022-01-16 08:59:40 +08:00
|
|
|
import dayjs from 'dayjs'
|
2022-03-25 15:35:56 +08:00
|
|
|
import { isClient, useStorage, useToggle } from '@vueuse/core'
|
2021-09-23 14:16:37 +08:00
|
|
|
import { useSidebar } from '../composables/sidebar'
|
|
|
|
import { useToggleWidgets } from '../composables/toggle-widgets'
|
2021-09-27 16:45:42 +08:00
|
|
|
import { useLang } from '../composables/lang'
|
2021-09-23 14:16:37 +08:00
|
|
|
import { breakpoints } from '../constant'
|
2021-09-17 00:18:50 +08:00
|
|
|
import VPOverlay from './vp-overlay.vue'
|
|
|
|
import VPNav from './vp-nav.vue'
|
|
|
|
import VPSubNav from './vp-subnav.vue'
|
|
|
|
import VPSidebar from './vp-sidebar.vue'
|
|
|
|
import VPContent from './vp-content.vue'
|
|
|
|
import VPSponsors from './vp-sponsors.vue'
|
2022-04-10 13:56:01 +08:00
|
|
|
import VPReloadPrompt from './vp-reload-prompt.vue'
|
2021-09-17 00:18:50 +08:00
|
|
|
|
2022-01-16 08:59:40 +08:00
|
|
|
const USER_PREFER_GITHUB_PAGE = 'USER_PREFER_GITHUB_PAGE'
|
2021-09-17 00:18:50 +08:00
|
|
|
const [isSidebarOpen, toggleSidebar] = useToggle(false)
|
|
|
|
const { hasSidebar } = useSidebar()
|
2021-09-27 16:45:42 +08:00
|
|
|
const lang = useLang()
|
2021-09-17 00:18:50 +08:00
|
|
|
|
2022-04-28 12:48:41 +08:00
|
|
|
const mirrorUrl = 'element-plus.gitee.io'
|
|
|
|
const isMirrorUrl = () => {
|
|
|
|
if (!isClient) return
|
|
|
|
return window.location.hostname === mirrorUrl
|
|
|
|
}
|
|
|
|
|
2021-09-17 00:18:50 +08:00
|
|
|
useToggleWidgets(isSidebarOpen, () => {
|
2022-03-15 22:13:01 +08:00
|
|
|
if (!isClient) return
|
2021-09-17 00:18:50 +08:00
|
|
|
if (window.outerWidth >= breakpoints.lg) {
|
|
|
|
toggleSidebar(false)
|
|
|
|
}
|
|
|
|
})
|
2021-09-18 00:34:56 +08:00
|
|
|
|
2022-03-24 14:00:32 +08:00
|
|
|
const userPrefer = useStorage<boolean | string>(USER_PREFER_GITHUB_PAGE, null)
|
|
|
|
|
2021-09-27 16:45:42 +08:00
|
|
|
onMounted(async () => {
|
2022-03-15 22:13:01 +08:00
|
|
|
if (!isClient) return
|
2021-09-18 00:34:56 +08:00
|
|
|
window.addEventListener(
|
|
|
|
'click',
|
|
|
|
(e) => {
|
2021-12-30 19:00:11 +08:00
|
|
|
const link = (e.target as HTMLElement).closest('a')
|
2021-09-18 00:34:56 +08:00
|
|
|
if (!link) return
|
|
|
|
|
2021-09-23 14:16:37 +08:00
|
|
|
const { protocol, hostname, pathname, target } = link
|
2021-09-18 00:34:56 +08:00
|
|
|
const currentUrl = window.location
|
|
|
|
const extMatch = pathname.match(/\.\w+$/)
|
|
|
|
// only intercept inbound links
|
|
|
|
if (
|
|
|
|
!e.ctrlKey &&
|
|
|
|
!e.shiftKey &&
|
|
|
|
!e.altKey &&
|
|
|
|
!e.metaKey &&
|
|
|
|
target !== `_blank` &&
|
|
|
|
protocol === currentUrl.protocol &&
|
|
|
|
hostname === currentUrl.hostname &&
|
|
|
|
!(extMatch && extMatch[0] !== '.html')
|
|
|
|
) {
|
|
|
|
e.preventDefault()
|
|
|
|
if (pathname !== currentUrl.pathname) {
|
|
|
|
nprogress.start()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ capture: true }
|
|
|
|
)
|
2021-09-27 16:45:42 +08:00
|
|
|
|
|
|
|
if (lang.value === 'zh-CN') {
|
2022-05-04 13:03:09 +08:00
|
|
|
// disable cn redirect temporarily
|
|
|
|
if (Math.random() < 1000) return
|
2022-04-28 12:48:41 +08:00
|
|
|
if (isMirrorUrl()) return
|
2022-03-24 14:00:32 +08:00
|
|
|
|
2022-03-15 22:13:01 +08:00
|
|
|
if (userPrefer.value) {
|
2022-01-16 08:59:40 +08:00
|
|
|
// no alert in the next 90 days
|
|
|
|
if (
|
|
|
|
dayjs
|
2022-03-15 22:13:01 +08:00
|
|
|
.unix(Number(userPrefer.value))
|
2022-01-16 08:59:40 +08:00
|
|
|
.add(90, 'day')
|
|
|
|
.diff(dayjs(), 'day', true) > 0
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
2021-09-27 16:45:42 +08:00
|
|
|
try {
|
|
|
|
await ElMessageBox.confirm(
|
|
|
|
'建议大陆用户访问部署在国内的站点,是否跳转?',
|
|
|
|
'提示',
|
|
|
|
{
|
|
|
|
confirmButtonText: '跳转',
|
|
|
|
cancelButtonText: '取消',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
const toLang = '/zh-CN/'
|
|
|
|
location.href = `https://element-plus.gitee.io${toLang}${location.pathname.slice(
|
|
|
|
toLang.length
|
|
|
|
)}`
|
2022-01-16 08:59:40 +08:00
|
|
|
} catch {
|
2022-03-15 22:13:01 +08:00
|
|
|
userPrefer.value = String(dayjs().unix())
|
2022-01-16 08:59:40 +08:00
|
|
|
}
|
2021-09-27 16:45:42 +08:00
|
|
|
}
|
2022-04-28 14:03:11 +08:00
|
|
|
if (isMirrorUrl()) {
|
|
|
|
// unregister sw on mirror site
|
|
|
|
navigator?.serviceWorker?.getRegistrations().then((registrations) => {
|
|
|
|
for (const registration of registrations) {
|
|
|
|
registration.unregister()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-09-18 00:34:56 +08:00
|
|
|
})
|
2021-09-17 00:18:50 +08:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<div class="App">
|
|
|
|
<VPOverlay
|
|
|
|
class="overlay"
|
|
|
|
:show="isSidebarOpen"
|
|
|
|
@click="toggleSidebar(false)"
|
|
|
|
/>
|
|
|
|
<VPNav />
|
|
|
|
<VPSubNav v-if="hasSidebar" @open-menu="toggleSidebar(true)" />
|
|
|
|
<VPSidebar :open="isSidebarOpen" @close="toggleSidebar(false)">
|
|
|
|
<template #top>
|
|
|
|
<VPSponsors />
|
|
|
|
</template>
|
|
|
|
<template #bottom>
|
|
|
|
<slot name="sidebar-bottom" />
|
|
|
|
</template>
|
|
|
|
</VPSidebar>
|
2021-09-18 00:34:56 +08:00
|
|
|
<VPContent :is-sidebar-open="isSidebarOpen">
|
2021-09-17 00:18:50 +08:00
|
|
|
<template #content-top>
|
|
|
|
<slot name="content-top" />
|
|
|
|
</template>
|
|
|
|
<template #content-bottom>
|
|
|
|
<slot name="content-bottom" />
|
|
|
|
</template>
|
|
|
|
<template #aside-top>
|
|
|
|
<slot name="aside-top" />
|
|
|
|
</template>
|
|
|
|
<template #aside-mid>
|
|
|
|
<slot name="aside-mid" />
|
|
|
|
</template>
|
|
|
|
<template #aside-bottom>
|
|
|
|
<slot name="aside-bottom" />
|
|
|
|
</template>
|
|
|
|
</VPContent>
|
2022-04-10 13:56:01 +08:00
|
|
|
<ClientOnly>
|
2022-04-28 12:48:41 +08:00
|
|
|
<VPReloadPrompt v-if="!isMirrorUrl()" />
|
2022-04-10 13:56:01 +08:00
|
|
|
</ClientOnly>
|
2021-09-17 00:18:50 +08:00
|
|
|
<Debug />
|
|
|
|
</div>
|
|
|
|
</template>
|