-
Notifications
You must be signed in to change notification settings - Fork 0
/
Common.vue
224 lines (195 loc) · 6.09 KB
/
Common.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<template>
<div class="theme-container" :class="pageClasses">
<div v-if="!absoluteEncryption">
<transition name="fade">
<LoadingPage v-show="firstLoad" class="loading-wrapper" />
</transition>
<transition name="fade">
<Password v-show="!firstLoad && !isHasKey" class="password-wrapper-out" key="out" />
</transition>
<div :class="{ 'hide': firstLoad || !isHasKey }">
<Navbar v-if="shouldShowNavbar" @toggle-sidebar="toggleSidebar" />
<div class="sidebar-mask" @click="toggleSidebar(false)"></div>
<Sidebar :items="sidebarItems" @toggle-sidebar="toggleSidebar">
<PersonalInfo slot="top" />
<slot name="sidebar-bottom" slot="bottom"/>
</Sidebar>
<Password v-show="!isHasPageKey" :isPage="true" class="password-wrapper-in" key="in"></Password>
<div :class="{ 'hide': !isHasPageKey }">
<slot></slot>
</div>
</div>
</div>
<div v-else>
<transition name="fade">
<LoadingPage v-if="firstLoad" />
<Password v-else-if="!isHasKey" />
<div v-else>
<Navbar v-if="shouldShowNavbar" @toggle-sidebar="toggleSidebar"/>
<div class="sidebar-mask" @click="toggleSidebar(false)"></div>
<Sidebar :items="sidebarItems" @toggle-sidebar="toggleSidebar">
<PersonalInfo slot="top" />
<slot name="sidebar-bottom" slot="bottom"/>
</Sidebar>
<Password v-if="!isHasPageKey" :isPage="true"></Password>
<slot v-else></slot>
</div>
</transition>
</div>
</div>
</template>
<script>
import { defineComponent, computed, ref, onMounted, toRefs, getCurrentInstance } from 'vue-demi'
import Navbar from '@theme/components/Navbar'
import Sidebar from '@theme/components/Sidebar'
import PersonalInfo from '@theme/components/PersonalInfo'
import Password from '@theme/components/Password'
import { setTimeout } from 'timers'
export default defineComponent({
components: { Sidebar, Navbar, Password, PersonalInfo },
props: {
sidebar: {
type: Boolean,
default: true
},
sidebarItems: {
type: Array,
default: () => []
},
showModule: {
type: Boolean,
default: false
}
},
setup (props, ctx) {
const instance = getCurrentInstance().proxy
const isSidebarOpen = ref(false)
const isHasKey = ref(true)
const isHasPageKey = ref(true)
const firstLoad = ref(true)
const shouldShowSidebar = computed(() => {
return instance.$frontmatter.leftSidebar !== false && props.sidebarItems.length > 0
})
const absoluteEncryption = computed(() => {
return instance.$themeConfig.keyPage && instance.$themeConfig.keyPage.absoluteEncryption === true
})
const shouldShowNavbar = computed(() => {
const { themeConfig } = instance.$site
const { frontmatter } = instance.$page
if (
frontmatter.navbar === false ||
themeConfig.navbar === false
) return false
return (
instance.$title ||
themeConfig.logo ||
themeConfig.repo ||
themeConfig.nav ||
instance.$themeLocaleConfig.nav
)
})
const pageClasses = computed(() => {
const classValue = {
'no-navbar': !shouldShowNavbar.value,
'sidebar-open': isSidebarOpen.value,
'no-sidebar': !shouldShowSidebar.value
}
const { pageClass: userPageClass } = instance.$frontmatter || {}
if (userPageClass) classValue[userPageClass] = true
return classValue
})
const hasKey = () => {
const { keyPage } = instance.$themeConfig
if (!keyPage || !keyPage.keys || keyPage.keys.length === 0) {
isHasKey.value = true
return
}
let { keys } = keyPage
keys = keys.map(item => item.toLowerCase())
isHasKey.value = keys && keys.indexOf(sessionStorage.getItem('key')) > -1
}
const initRouterHandler = () => {
instance.$router.afterEach(() => {
isSidebarOpen.value = false
})
}
const hasPageKey = () => {
let pageKeys = instance.$frontmatter.keys
if (!pageKeys || pageKeys.length === 0) {
isHasPageKey.value = true
return
}
pageKeys = pageKeys.map(item => item.toLowerCase())
isHasPageKey.value = pageKeys.indexOf(sessionStorage.getItem(`pageKey${window.location.pathname}`)) > -1
}
const toggleSidebar = (to) => {
isSidebarOpen.value = typeof to === 'boolean' ? to : !isSidebarOpen.value
}
const handleLoading = () => {
const time = instance.$frontmatter.home && sessionStorage.getItem('firstLoad') == undefined ? 1000 : 0
setTimeout(() => {
firstLoad.value = false
if (sessionStorage.getItem('firstLoad') == undefined) sessionStorage.setItem('firstLoad', false)
}, time)
}
// 首次渲染时,recoShowModule 直接为 true,否则锚点失效
const { showModule } = toRefs(props)
const recoShowModule = computed(() => {
if (firstLoad.value) {
return true
} else {
return showModule.value
}
})
onMounted(() => {
initRouterHandler()
hasKey()
hasPageKey()
handleLoading()
})
return { isSidebarOpen, absoluteEncryption, shouldShowNavbar, shouldShowSidebar, pageClasses, hasKey, hasPageKey, isHasKey, isHasPageKey, toggleSidebar, firstLoad, recoShowModule }
},
watch: {
$frontmatter (newVal, oldVal) {
this.hasKey()
this.hasPageKey()
}
}
})
</script>
<style lang="stylus" scoped>
.theme-container
.loading-wrapper
position absolute
z-index 22
top 0
bottom 0
left 0
right 0
margin auto
.password-wrapper-out
position absolute
z-index 21
top 0
bottom 0
left 0
right 0
margin auto
.password-wrapper-in
position absolute
z-index 8
top 0
bottom 0
left 0
right 0
.hide
height 100vh
overflow hidden
opacity 0
.fade-enter-active, .fade-leave-active {
transition: opacity .5s ease-in-out .5s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
opacity: 0;
}
</style>