返回 slidev
CodeRunner.vue
根目录 / packages / client / internals / CodeRunner.vue
1 <script setup lang="ts">
2 import type { CodeRunnerOutputs, RawAtValue } from '@slidev/types'
3 import type { CSSProperties } from 'vue'
4 import { debounce, toArray } from '@antfu/utils'
5 import { useVModel } from '@vueuse/core'
6 import { computed, onMounted, onUnmounted, ref, shallowRef, toValue, watch, watchSyncEffect } from 'vue'
7 import { useNav } from '../composables/useNav'
8 import { useSlideContext } from '../context'
9 import { makeId } from '../logic/utils'
10 import setupCodeRunners from '../setup/code-runners'
11 import DomElement from './DomElement.vue'
12 import IconButton from './IconButton.vue'
13
14 const props = defineProps<{
15 modelValue: string
16 lang: string
17 autorun: boolean | 'once'
18 height?: string
19 fontSize?: number | string
20 showOutputAt?: RawAtValue
21 highlightOutput: boolean
22 runnerOptions?: Record<string, unknown>
23 }>()
24
25 const emit = defineEmits(['update:modelValue'])
26
27 const { isEmbedded, isPrintMode } = useNav()
28
29 const code = useVModel(props, 'modelValue', emit)
30
31 const { $renderContext, $clicksContext } = useSlideContext()
32 const disabled = computed(() => !['slide', 'presenter'].includes($renderContext.value) && !($renderContext.value === 'overview' && isEmbedded.value))
33
34 const autorun = isPrintMode.value ? 'once' : props.autorun
35 const isRunning = ref(autorun)
36 const outputs = shallowRef<CodeRunnerOutputs>()
37 const runCount = ref(0)
38 const highlightFn = ref<(code: string, lang: string) => string>()
39
40 const outputStyle = computed<CSSProperties>(() => {
41 if (props.fontSize == null || props.fontSize === '')
42 return {}
43 return {
44 fontSize: typeof props.fontSize === 'number'
45 ? `${props.fontSize}px`
46 : props.fontSize,
47 lineHeight: 1.1,
48 }
49 })
50
51 const hidden = ref(props.showOutputAt)
52 if (props.showOutputAt) {
53 const id = makeId()
54 onMounted(() => {
55 const info = $clicksContext.calculate(props.showOutputAt)
56 if (info) {
57 $clicksContext.register(id, info)
58 watchSyncEffect(() => {
59 hidden.value = !info.isActive.value
60 })
61 }
62 else {
63 hidden.value = false
64 }
65 })
66 onUnmounted(() => $clicksContext.unregister(id))
67 }
68
69 const triggerRun = debounce(200, async () => {
70 if (disabled.value)
71 return
72
73 const { highlight, run } = await setupCodeRunners()
74 highlightFn.value = highlight
75
76 const setAsRunning = setTimeout(() => {
77 isRunning.value = true
78 }, 500)
79
80 outputs.value = await run(code.value, props.lang, props.runnerOptions ?? {})
81 runCount.value += 1
82 isRunning.value = false
83
84 clearTimeout(setAsRunning)
85 })
86
87 if (autorun === 'once')
88 triggerRun()
89 else if (autorun)
90 watch(code, triggerRun, { immediate: true })
91 </script>
92
93 <template>
94 <div
95 v-show="!hidden"
96 class="relative flex flex-col rounded-b border-t border-main"
97 :style="{ height: props.height }"
98 data-waitfor=".slidev-runner-output"
99 >
100 <div v-if="disabled" class="text-sm text-center opacity-50">
101 Code is disabled in the "{{ $renderContext }}" mode
102 </div>
103 <div v-else-if="isRunning" class="text-sm text-center opacity-50">
104 Running...
105 </div>
106 <div v-else-if="!outputs" class="text-sm text-center opacity-50">
107 Click the play button to run the code
108 </div>
109 <div v-else :key="`run-${runCount}`" class="slidev-runner-output" :style="outputStyle">
110 <template v-for="output, _idx1 of toArray(toValue(outputs))" :key="_idx1">
111 <div v-if="'html' in output" v-html="output.html" />
112 <div v-else-if="'error' in output" class="text-red-500">
113 {{ output.error }}
114 </div>
115 <DomElement v-else-if="'element' in output" :element="output.element" />
116 <div v-else class="output-line">
117 <template
118 v-for="item, idx2 in toArray(output)"
119 :key="idx2"
120 >
121 <span
122 v-if="item.highlightLang && highlightFn"
123 class="highlighted"
124 v-html="highlightFn(item.text, item.highlightLang)"
125 />
126 <span v-else :class="item.class">{{ item.text }}</span>
127 <span v-if="idx2 < toArray(output).length - 1" class="separator">,</span>
128 </template>
129 </div>
130 </template>
131 </div>
132 </div>
133 <div v-if="code.trim()" class="absolute right-1 top-1 max-h-full flex gap-1">
134 <IconButton class="w-8 h-8 max-h-full flex justify-center items-center" title="Run code" @click="triggerRun">
135 <div class="i-carbon:play" />
136 </IconButton>
137 </div>
138 </template>
139
140 <style lang="postcss">
141 .slidev-runner-output {
142 @apply px-5 py-3 flex-grow text-xs leading-[.8rem] font-$slidev-code-font-family select-text;
143 }
144
145 .slidev-runner-output .log-type {
146 @apply font-bold op-70;
147
148 &.DBG {
149 @apply text-gray-500;
150 }
151
152 &.LOG {
153 @apply text-blue-500;
154 }
155
156 &.WRN {
157 @apply text-orange-500;
158 }
159
160 &.ERR {
161 @apply text-red-500;
162 }
163 }
164
165 .slidev-runner-output .output-line {
166 @apply flex my-1 w-full;
167 }
168
169 .slidev-runner-output .separator {
170 @apply op-40 mr-1;
171 }
172
173 .slidev-runner-output .highlighted > pre {
174 @apply inline text-wrap !bg-transparent;
175 }
176 </style>
177
177 lines Plain Text