Skip to content

Commit ee86017

Browse files
author
reco_luan
committed
refactor(theme): use VCA
1 parent 1a57da8 commit ee86017

File tree

1 file changed

+64
-89
lines changed

1 file changed

+64
-89
lines changed

packages/vuepress-theme-reco/components/Common.vue

Lines changed: 64 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
<template>
2-
<div
3-
class="theme-container"
4-
:class="pageClasses"
5-
@touchstart="onTouchStart"
6-
@touchend="onTouchEnd">
2+
<div class="theme-container" :class="pageClasses">
73
<div v-if="!absoluteEncryption">
84
<transition name="fade">
95
<LoadingPage v-show="firstLoad" class="loading-wrapper" />
@@ -72,13 +68,14 @@
7268
</template>
7369

7470
<script>
71+
import { defineComponent, computed, ref, onMounted, watch, reactive } from '@vue/composition-api'
7572
import Navbar from '@theme/components/Navbar'
7673
import Sidebar from '@theme/components/Sidebar'
7774
import PersonalInfo from '@theme/components/PersonalInfo'
7875
import Password from '@theme/components/Password'
7976
import { setTimeout } from 'timers'
8077
81-
export default {
78+
export default defineComponent({
8279
components: { Sidebar, Navbar, Password, PersonalInfo },
8380
8481
props: {
@@ -96,119 +93,97 @@ export default {
9693
}
9794
},
9895
99-
data () {
100-
return {
101-
isSidebarOpen: false,
102-
isHasKey: true,
103-
isHasPageKey: true,
104-
firstLoad: true
105-
}
106-
},
96+
setup (props, ctx) {
97+
const { root } = ctx
10798
108-
computed: {
109-
absoluteEncryption () {
110-
return this.$themeConfig.keyPage && this.$themeConfig.keyPage.absoluteEncryption === true
111-
},
99+
const isSidebarOpen = ref(false)
100+
const isHasKey = ref(true)
101+
const isHasPageKey = ref(true)
102+
const firstLoad = ref(true)
112103
113-
shouldShowNavbar () {
114-
const { themeConfig } = this.$site
115-
const { frontmatter } = this.$page
104+
const shouldShowSidebar = computed(() => props.sidebarItems.length > 0)
105+
const absoluteEncryption = computed(() => {
106+
return root.$themeConfig.keyPage && root.$themeConfig.keyPage.absoluteEncryption === true
107+
})
108+
const shouldShowNavbar = computed(() => {
109+
const { themeConfig } = root.$site
110+
const { frontmatter } = root.$page
116111
117112
if (
118113
frontmatter.navbar === false ||
119114
themeConfig.navbar === false
120115
) return false
121116
122117
return (
123-
this.$title ||
118+
root.$title ||
124119
themeConfig.logo ||
125120
themeConfig.repo ||
126121
themeConfig.nav ||
127-
this.$themeLocaleConfig.nav
122+
root.$themeLocaleConfig.nav
128123
)
129-
},
130-
131-
shouldShowSidebar () {
132-
return this.sidebarItems.length > 0
133-
},
134-
135-
pageClasses () {
136-
const userPageClass = this.$frontmatter.pageClass
137-
return [
138-
{
139-
'no-navbar': !this.shouldShowNavbar,
140-
'sidebar-open': this.isSidebarOpen,
141-
'no-sidebar': !this.shouldShowSidebar
124+
})
125+
const pageClasses = computed(() => {
126+
const userPageClass = root.$frontmatter.pageClass
127+
return {
128+
...{
129+
'no-navbar': !shouldShowNavbar.value,
130+
'sidebar-open': isSidebarOpen.value,
131+
'no-sidebar': !shouldShowSidebar.value
142132
},
143-
userPageClass
144-
]
145-
}
146-
},
147-
148-
mounted () {
149-
this.$router.afterEach(() => {
150-
this.isSidebarOpen = false
133+
...userPageClass
134+
}
151135
})
152-
153-
this.hasKey()
154-
this.hasPageKey()
155-
this.handleLoading()
156-
},
157-
158-
methods: {
159-
hasKey () {
160-
const keyPage = this.$themeConfig.keyPage
136+
const hasKey = () => {
137+
const { keyPage } = root.$themeConfig
161138
if (!keyPage || !keyPage.keys || keyPage.keys.length === 0) {
162-
this.isHasKey = true
139+
isHasKey.value = true
163140
return
164141
}
165142
166143
let { keys } = keyPage
167144
keys = keys.map(item => item.toLowerCase())
168-
this.isHasKey = keys && keys.indexOf(sessionStorage.getItem('key')) > -1
169-
},
170-
hasPageKey () {
171-
let pageKeys = this.$frontmatter.keys
145+
isHasKey.value = keys && keys.indexOf(sessionStorage.getItem('key')) > -1
146+
}
147+
const initRouterHandler = () => {
148+
root.$router.afterEach(() => {
149+
isSidebarOpen.value = false
150+
})
151+
}
152+
const hasPageKey = () => {
153+
let pageKeys = root.$frontmatter.keys
172154
if (!pageKeys || pageKeys.length === 0) {
173-
this.isHasPageKey = true
155+
isHasPageKey.value = true
174156
return
175157
}
176158
177159
pageKeys = pageKeys.map(item => item.toLowerCase())
178160
179-
this.isHasPageKey = pageKeys.indexOf(sessionStorage.getItem(`pageKey${window.location.pathname}`)) > -1
180-
},
181-
toggleSidebar (to) {
182-
this.isSidebarOpen = typeof to === 'boolean' ? to : !this.isSidebarOpen
183-
},
184-
185-
// side swipe
186-
onTouchStart (e) {
187-
this.touchStart = {
188-
x: e.changedTouches[0].clientX,
189-
y: e.changedTouches[0].clientY
190-
}
191-
},
192-
193-
onTouchEnd (e) {
194-
const dx = e.changedTouches[0].clientX - this.touchStart.x
195-
const dy = e.changedTouches[0].clientY - this.touchStart.y
196-
if (Math.abs(dx) > Math.abs(dy) && Math.abs(dx) > 40) {
197-
if (dx > 0 && this.touchStart.x <= 80) {
198-
this.toggleSidebar(true)
199-
} else {
200-
this.toggleSidebar(false)
201-
}
202-
}
203-
},
204-
205-
handleLoading () {
206-
const time = this.$frontmatter.home && sessionStorage.getItem('firstLoad') == undefined ? 1000 : 0
161+
isHasPageKey.value = pageKeys.indexOf(sessionStorage.getItem(`pageKey${window.location.pathname}`)) > -1
162+
}
163+
const toggleSidebar = (to) => {
164+
isSidebarOpen.value = typeof to === 'boolean' ? to : !isSidebarOpen.value
165+
}
166+
const handleLoading = () => {
167+
const time = root.$frontmatter.home && sessionStorage.getItem('firstLoad') == undefined ? 1000 : 0
207168
setTimeout(() => {
208-
this.firstLoad = false
169+
firstLoad.value = false
209170
if (sessionStorage.getItem('firstLoad') == undefined) sessionStorage.setItem('firstLoad', false)
210171
}, time)
211172
}
173+
174+
onMounted(() => {
175+
initRouterHandler()
176+
hasKey()
177+
hasPageKey()
178+
handleLoading()
179+
})
180+
181+
watch(reactive(root.$frontmatter), (newVal, oldVal) => {
182+
hasKey()
183+
hasPageKey()
184+
})
185+
186+
return { isSidebarOpen, absoluteEncryption, shouldShowNavbar, shouldShowSidebar, pageClasses, isHasKey, hasKey, isHasPageKey, toggleSidebar, firstLoad }
212187
},
213188
214189
watch: {
@@ -217,7 +192,7 @@ export default {
217192
this.hasPageKey()
218193
}
219194
}
220-
}
195+
})
221196
</script>
222197

223198
<style lang="stylus" scoped>

0 commit comments

Comments
 (0)