-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathContent.vue
114 lines (100 loc) · 2.55 KB
/
Content.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
<template>
<transition :name="disableTransition ? null : 'fade'">
<component
v-if="layout"
:is="layout"
:slot-key="slotKey || 'default'"
/>
<div v-else class="content"></div>
</transition>
</template>
<script>
/* global CONTENT_LOADING */
import Vue from 'vue'
import ContentLoading from './ContentLoading'
const CONTENT_LOADING_COMPONENT = typeof CONTENT_LOADING === 'string'
? CONTENT_LOADING
: 'ContentLoading'
export default {
components: { ContentLoading },
props: {
pageKey: String,
slotKey: {
type: String,
default: 'default'
}
},
data () {
return {
layout: CONTENT_LOADING_COMPONENT,
noTransition: true
}
},
computed: {
$pageKey () {
return this.pageKey || this.$page.key
},
disableTransition () {
return !this.layout || this.layout === CONTENT_LOADING_COMPONENT || this.noTransition
}
},
created () {
this.loadContent(this.$pageKey)
},
watch: {
$pageKey (key) {
this.$vuepress.$set('contentMounted', false)
this.reloadContent(key)
}
},
methods: {
loadContent (pageKey) {
this.layout = null
if (this.$vuepress.isPageExists(pageKey)) {
if (!this.$ssrContext) {
this.$vuepress.registerPageAsyncComponent(pageKey)
this.layout = pageKey
}
}
},
reloadContent (pageKey) {
// When page has been loaded, disable transition.
if (this.$vuepress.isPageLoaded(pageKey)) {
this.layout = pageKey
this.noTransition = true
return
}
// Start to load unfetched page component.
this.layout = CONTENT_LOADING_COMPONENT
if (this.$vuepress.isPageExists(pageKey)) {
this.noTransition = false
if (!this.$ssrContext) {
Promise.all([
this.$vuepress.loadPageAsyncComponent(pageKey),
new Promise(resolve => setTimeout(resolve, 1000))
]).then(([comp]) => {
this.$vuepress.$emit('AsyncMarkdownAssetLoaded', this.pageKey)
Vue.component(pageKey, comp.default)
this.layout = null
setTimeout(() => {
this.layout = pageKey
setTimeout(() => {
this.$vuepress.$set('contentMounted', true)
this.$vuepress.$emit('contentMounted', this.slotKey)
})
})
})
}
}
}
}
}
</script>
<style>
.fade-enter-active, .fade-leave-active {
transition: opacity .2s;
}
.fade-enter, .fade-leave-to {
opacity: 0;
}
</style>