/
StorySourceCode.vue
246 lines (219 loc) · 7.04 KB
/
StorySourceCode.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<script lang="ts" setup>
import { computed, markRaw, nextTick, onMounted, ref, shallowRef, watch, watchEffect } from 'vue'
import { Icon } from '@iconify/vue'
import { getHighlighter, Highlighter, setCDN } from 'shiki'
import { HstCopyIcon } from '@histoire/controls'
import { unindent } from '@histoire/shared'
// @ts-expect-error virtual module
import { clientSupportPlugins } from 'virtual:$histoire-support-plugins-client'
import type { Variant, Story } from '../../types'
import { isDark } from '../../util/dark'
import BaseEmpty from '../base/BaseEmpty.vue'
const props = defineProps<{
story: Story
variant: Variant
}>()
const generateSourceCodeFn = ref(null)
watchEffect(async () => {
const clientPlugin = clientSupportPlugins[props.story.file?.supportPluginId]
if (clientPlugin) {
const pluginModule = await clientPlugin()
generateSourceCodeFn.value = markRaw(pluginModule.generateSourceCode)
}
})
const highlighter = shallowRef<Highlighter>()
const dynamicSourceCode = ref('')
const error = ref<string>(null)
watch(() => [props.variant, generateSourceCodeFn.value], async () => {
if (!generateSourceCodeFn.value) return
error.value = null
dynamicSourceCode.value = ''
try {
if (props.variant.source) {
dynamicSourceCode.value = props.variant.source
} else if (props.variant.slots?.().source) {
const source = props.variant.slots?.().source()[0].children
if (source) {
dynamicSourceCode.value = await unindent(source)
}
} else {
dynamicSourceCode.value = await generateSourceCodeFn.value(props.variant)
}
} catch (e) {
console.error(e)
error.value = e.message
}
// Auto-switch
if (!dynamicSourceCode.value) {
displayedSource.value = 'static'
}
}, {
deep: true,
immediate: true,
})
// Static file source
const staticSourceCode = ref('')
watch(() => [props.story, props.story?.file?.source], async () => {
staticSourceCode.value = ''
const sourceLoader = props.story.file?.source
if (sourceLoader) {
staticSourceCode.value = (await sourceLoader()).default
}
}, {
immediate: true,
})
const displayedSource = ref<'dynamic' | 'static'>('dynamic')
const displayedSourceCode = computed(() => {
if (displayedSource.value === 'dynamic') {
return dynamicSourceCode.value
}
return staticSourceCode.value
})
// HTML render
onMounted(async () => {
setCDN('https://unpkg.com/shiki@0.10.1/')
highlighter.value = await getHighlighter({
langs: [
'html',
'jsx',
],
themes: [
'github-light',
'github-dark',
],
})
})
const sourceHtml = computed(() => displayedSourceCode.value
? highlighter.value?.codeToHtml(displayedSourceCode.value, {
lang: 'html',
theme: isDark.value ? 'github-dark' : 'github-light',
})
: '')
// Scrolling
let lastScroll = 0
// Reset
watch(() => props.variant, () => {
lastScroll = 0
})
const scroller = ref<HTMLElement>()
function onScroll (event) {
if (sourceHtml.value) {
lastScroll = event.target.scrollTop
}
}
watch(sourceHtml, async () => {
await nextTick()
if (scroller.value) {
scroller.value.scrollTop = lastScroll
}
})
</script>
<template>
<div
class="histoire-story-source-code htw-bg-gray-50 dark:htw-bg-gray-750 htw-h-full htw-overflow-hidden htw-flex htw-flex-col"
>
<!-- Toolbar -->
<div
v-if="!error"
class="htw-h-10 htw-flex-none htw-border-b htw-border-solid htw-border-gray-500/5 htw-px-4 htw-flex htw-items-center htw-gap-2"
>
<div class="htw-text-gray-900 dark:htw-text-gray-100">
Source
</div>
<div class="htw-flex-1" />
<!-- Display source modes -->
<div class="htw-flex htw-flex-none htw-gap-px htw-h-full htw-py-2">
<button
v-tooltip="!dynamicSourceCode ? 'Dynamic source code is not available' : displayedSource !== 'dynamic' ? 'Switch to dynamic source' : null"
class="htw-flex htw-items-center htw-gap-1 htw-h-full htw-px-1 htw-bg-gray-500/10 htw-rounded-l htw-transition-all htw-ease-[cubic-bezier(0,1,.6,1)] htw-duration-300 htw-overflow-hidden"
:class="[
displayedSource !== 'dynamic' ? 'htw-max-w-6 htw-opacity-70' : 'htw-max-w-[82px] htw-text-primary-600 dark:htw-text-primary-400',
dynamicSourceCode ? 'htw-cursor-pointer hover:htw-bg-gray-500/30 active:htw-bg-gray-600/50' : 'htw-opacity-50',
]"
@click="dynamicSourceCode && (displayedSource = 'dynamic')"
>
<Icon
icon="carbon:flash"
class="htw-w-4 htw-h-4 htw-flex-none"
/>
<span
class="transition-opacity duration-300"
:class="{
'opacity-0': displayedSource !== 'dynamic',
}"
>
Dynamic
</span>
</button>
<button
v-tooltip="!staticSourceCode ? 'Static source code is not available' : displayedSource !== 'static' ? 'Switch to static source' : null"
class="htw-flex htw-items-center htw-gap-1 htw-h-full htw-px-1 htw-bg-gray-500/10 htw-rounded-r htw-transition-all htw-ease-[cubic-bezier(0,1,.6,1)] htw-duration-300 htw-overflow-hidden"
:class="[
displayedSource !== 'static' ? 'htw-max-w-6 htw-opacity-70' : 'htw-max-w-[63px] htw-text-primary-600 dark:htw-text-primary-400',
staticSourceCode ? 'htw-cursor-pointer hover:htw-bg-gray-500/30 active:htw-bg-gray-600/50' : 'htw-opacity-50',
]"
@click="staticSourceCode && (displayedSource = 'static')"
>
<Icon
icon="carbon:document"
class="htw-w-4 htw-h-4 htw-flex-none"
/>
<span
class="transition-opacity duration-300"
:class="{
'opacity-0': displayedSource !== 'static',
}"
>
Static
</span>
</button>
</div>
<HstCopyIcon
:content="displayedSourceCode"
class="htw-flex-none"
/>
</div>
<div
v-if="error"
class="htw-text-red-500 htw-h-full htw-p-2 htw-overflow-auto htw-font-mono htw-text-sm"
>
Error: {{ error }}
</div>
<BaseEmpty v-else-if="!displayedSourceCode">
<Icon
icon="carbon:code-hide"
class="htw-w-8 htw-h-8 htw-opacity-50 htw-mb-6"
/>
<span>Not available</span>
</BaseEmpty>
<textarea
v-else-if="!sourceHtml"
ref="scroller"
class="__histoire-code-placeholder htw-w-full htw-h-full htw-p-4 htw-outline-none htw-bg-transparent htw-resize-none htw-m-0"
:value="displayedSourceCode"
readonly
data-test-id="story-source-code"
@scroll="onScroll"
/>
<!-- eslint-disable vue/no-v-html -->
<div
v-else
ref="scroller"
class="htw-w-full htw-h-full htw-overflow-auto"
data-test-id="story-source-code"
@scroll="onScroll"
>
<div
class="__histoire-code htw-p-4 htw-w-fit"
v-html="sourceHtml"
/>
</div>
<!-- eslint-enable vue/no-v-html -->
</div>
</template>
<style scoped>
.__histoire-code-placeholder {
color: inherit;
font-size: inherit;
}
</style>