forked from vuejs/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTutorialRepl.vue
131 lines (110 loc) · 3 KB
/
TutorialRepl.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
<script setup lang="ts">
import { Repl, ReplStore } from '@vue/repl'
import { inject, watch, version, Ref, ref, computed } from 'vue'
import data from './data.json'
import { resolveSFCExample, resolveNoBuildExample, onHashChange } from '../examples/utils'
import '@vue/repl/style.css'
const store = new ReplStore({
defaultVueRuntimeURL: `https://unpkg.com/vue@${version}/dist/vue.esm-browser.js`
})
const preferComposition = inject('prefer-composition') as Ref<boolean>
const preferSFC = inject('prefer-sfc') as Ref<boolean>
const currentStep = ref('')
const currentDescription = computed(() => {
return data[currentStep.value]?.['description.md']
})
const nextStep = computed(() => {
const next = `step-${parseInt(currentStep.value, 10) + 1}`
if (data.hasOwnProperty(next)) {
return next
}
})
const userEditedState = ref<object | null>(null)
const buttonText = computed(() => (userEditedState.value ? 'Reset' : 'Show me!'))
function updateExample() {
console.log('Update example triggered')
let hash = location.hash.slice(1)
if (!data.hasOwnProperty(hash)) {
hash = 'step-1'
location.hash = `#${hash}`
}
currentStep.value = hash
const content = userEditedState.value ? data[nextStep.value] : data[hash]
store.setFiles(
preferSFC.value
? resolveSFCExample(content, preferComposition.value)
: resolveNoBuildExample(content, preferComposition.value),
preferSFC.value ? 'App.vue' : 'index.html'
)
}
function toggleResult() {
if (userEditedState.value) {
store.setFiles(userEditedState.value)
userEditedState.value = null
} else {
userEditedState.value = store.getFiles()
updateExample()
}
}
watch([preferComposition, preferSFC], () => {
userEditedState.value = null
updateExample()
})
onHashChange(() => {
userEditedState.value = null
updateExample()
})
updateExample()
</script>
<template>
<section class="tutorial">
<article class="instruction">
<div class="vt-doc" v-html="currentDescription"></div>
<footer class="footer">
<button @click="toggleResult">{{ buttonText }}</button>
<a v-if="nextStep" :href="`#${nextStep}`">Next Step ></a>
</footer>
</article>
<Repl
:store="store"
:showCompileOutput="false"
:clearConsole="false"
:showImportMap="false"
/>
</section>
</template>
<style scoped>
.tutorial {
--ins-height: min(30vh, 250px);
}
.vue-repl,
.instruction {
max-width: 1105px;
border-right: 1px solid var(--vt-c-divider-light);
}
.instruction {
height: var(--ins-height);
padding: 24px 32px;
border-bottom: 1px solid var(--vt-c-divider-light);
font-size: 15px;
overflow-y: auto;
}
.instruction h1 {
font-weight: 600;
font-size: 18px;
margin-bottom: 0.5em;
}
.vue-repl {
height: calc(100vh - var(--vp-nav-height) - var(--ins-height));
}
@media (max-width: 960px) {
.vue-repl {
border: none;
height: calc(100vh - var(--vp-nav-height) - var(--ins-height) - 48px);
}
}
.vt-doc :deep(h1) {
font-size: 1.5em;
margin-bottom: 1em;
}
</style>