返回 slidev
export.vue
根目录 / packages / client / pages / export.vue
1 <script setup lang="ts">
2 import type { ScreenshotSession } from '../logic/screenshot'
3 import { sleep } from '@antfu/utils'
4 import { parseRangeString } from '@slidev/parser/utils'
5 import { useHead } from '@unhead/vue'
6 import { provideLocal, useElementSize, useStyleTag, watchDebounced } from '@vueuse/core'
7 import { computed, ref, useTemplateRef, watch } from 'vue'
8 import { useRouter } from 'vue-router'
9 import { useDarkMode } from '../composables/useDarkMode'
10 import { useNav } from '../composables/useNav'
11 import { patchMonacoColors } from '../composables/usePrintStyles'
12 import { injectionSlideScale } from '../constants'
13 import { configs, slideHeight, slidesTitle, slideWidth } from '../env'
14 import ExportPdfTip from '../internals/ExportPdfTip.vue'
15 import FormCheckbox from '../internals/FormCheckbox.vue'
16 import FormItem from '../internals/FormItem.vue'
17 import PrintSlide from '../internals/PrintSlide.vue'
18 import SegmentControl from '../internals/SegmentControl.vue'
19 import { isScreenshotSupported, startScreenshotSession } from '../logic/screenshot'
20 import { captureDelay, skipExportPdfTip } from '../state'
21 import Play from './play.vue'
22
23 const { slides, isPrintWithClicks, hasNext, go, next, currentSlideNo, clicks, printRange } = useNav()
24 const router = useRouter()
25 const { isColorSchemaConfigured, isDark } = useDarkMode()
26 const { width: containerWidth } = useElementSize(useTemplateRef('export-container'))
27 const { height: contentHeight } = useElementSize(useTemplateRef('export-content'))
28 const scale = computed(() => containerWidth.value / slideWidth.value)
29 const contentMarginBottom = computed(() => `${contentHeight.value * (scale.value - 1)}px`)
30 const rangesRaw = ref('')
31 const initialWait = ref(1000)
32 type ScreenshotResult = { slideIndex: number, clickIndex: number, dataUrl: string }[]
33 const screenshotSession = ref<ScreenshotSession | null>(null)
34 const capturedImages = ref<ScreenshotResult | null>(null)
35 const title = ref(configs.exportFilename || slidesTitle)
36
37 useHead({
38 title,
39 })
40
41 provideLocal(injectionSlideScale, scale)
42
43 const showExportPdfTip = ref(false)
44 function pdf() {
45 if (skipExportPdfTip.value) {
46 doPrint()
47 }
48 else {
49 showExportPdfTip.value = true
50 }
51 }
52
53 function doPrint() {
54 patchMonacoColors()
55 setTimeout(window.print, 100)
56 }
57
58 async function capturePngs() {
59 if (screenshotSession.value) {
60 screenshotSession.value.dispose()
61 screenshotSession.value = null
62 }
63 if (capturedImages.value)
64 return capturedImages.value
65 try {
66 const scale = 2
67 screenshotSession.value = await startScreenshotSession(slideWidth.value * scale, slideHeight.value * scale)
68 const result: ScreenshotResult = []
69
70 go(1, 0, true)
71
72 await sleep(initialWait.value + captureDelay.value)
73 while (true) {
74 if (!screenshotSession.value) {
75 break
76 }
77 result.push({
78 slideIndex: currentSlideNo.value - 1,
79 clickIndex: clicks.value,
80 dataUrl: screenshotSession.value.screenshot(document.getElementById('slide-content')!),
81 })
82 if (hasNext.value) {
83 await sleep(captureDelay.value)
84 next()
85 await sleep(captureDelay.value)
86 }
87 else {
88 break
89 }
90 }
91
92 if (screenshotSession.value) {
93 screenshotSession.value.dispose()
94 capturedImages.value = result
95 screenshotSession.value = null
96 }
97 }
98 catch (e) {
99 console.error(e)
100 capturedImages.value = null
101 }
102 finally {
103 router.push('/export')
104 }
105 return capturedImages.value
106 }
107
108 async function pptx() {
109 const pngs = await capturePngs()
110 if (!pngs)
111 return
112 const pptx = await import('pptxgenjs')
113 .then(r => r.default)
114 .then(PptxGen => new PptxGen())
115
116 const layoutName = `${slideWidth.value}x${slideHeight.value}`
117 pptx.defineLayout({
118 name: layoutName,
119 width: slideWidth.value / 96,
120 height: slideHeight.value / 96,
121 })
122 pptx.layout = layoutName
123 if (configs.author)
124 pptx.author = configs.author
125 pptx.company = 'Created using Slidev'
126 pptx.title = title.value
127 if (typeof configs.info === 'string')
128 pptx.subject = configs.info
129
130 pngs.forEach(({ slideIndex, dataUrl }) => {
131 const slide = pptx.addSlide()
132 slide.background = {
133 data: dataUrl,
134 }
135
136 const note = slides.value[slideIndex].meta.slide.note
137 if (note)
138 slide.addNotes(note)
139 })
140
141 const blob = await pptx.write({
142 outputType: 'blob',
143 compression: true,
144 }) as Blob
145 const url = URL.createObjectURL(blob)
146 const a = document.createElement('a')
147 a.href = url
148 a.download = `${title.value}.pptx`
149 a.click()
150 }
151
152 async function pngsGz() {
153 const pngs = await capturePngs()
154 if (!pngs)
155 return
156 const { createTarGzip } = await import('nanotar')
157 const data = await createTarGzip(
158 pngs.map(({ slideIndex, dataUrl }) => ({
159 name: `${slideIndex}.png`,
160 data: new Uint8Array(atob(dataUrl.split(',')[1]).split('').map(char => char.charCodeAt(0))),
161 })),
162 )
163 const a = document.createElement('a')
164 const blob = new Blob([data as any], { type: 'application/gzip' })
165 a.href = URL.createObjectURL(blob)
166 a.download = `${title.value}.tar.gz`
167 a.click()
168 }
169
170 useStyleTag(computed(() => screenshotSession.value?.isActive
171 ? `
172 html {
173 cursor: none;
174 margin-bottom: 20px;
175 }
176 body {
177 pointer-events: none;
178 }`
179 : `
180 :root {
181 --slidev-slide-scale: ${scale.value};
182 }
183 `))
184
185 // clear captured images when settings changed
186 watch(
187 [
188 isDark,
189 printRange,
190 isPrintWithClicks,
191 ],
192 () => capturedImages.value = null,
193 )
194
195 watchDebounced(
196 [slides, rangesRaw],
197 () => printRange.value = parseRangeString(slides.value.length, rangesRaw.value),
198 { debounce: 300 },
199 )
200
201 // clear captured images when HMR
202 if (import.meta.hot) {
203 import.meta.hot.on('vite:beforeUpdate', () => {
204 capturedImages.value = null
205 })
206 }
207 </script>
208
209 <template>
210 <Play v-if="screenshotSession?.isActive" />
211 <div
212 v-else
213 class="fixed inset-0 flex flex-col md:flex-row md:gap-8 print:position-unset print:inset-0 print:block print:min-h-max justify-center of-hidden bg-main"
214 >
215 <div class="print:hidden min-w-fit flex flex-wrap md:flex-nowrap md:of-y-auto md:flex-col gap-2 p-6 max-w-100">
216 <h1 class="text-3xl md:my-4 flex items-center gap-2 w-full">
217 <RouterLink to="/" class="i-carbon:previous-outline op-70 hover:op-100" />
218 Browser Exporter
219 <sup op50 italic text-sm>Experimental</sup>
220 </h1>
221 <div flex="~ col gap-2">
222 <h2>Options</h2>
223 <FormItem title="Title">
224 <input v-model="title" type="text">
225 </FormItem>
226 <FormItem title="Range">
227 <input v-model="rangesRaw" type="text" :placeholder="`1-${slides.length}`">
228 </FormItem>
229 <FormItem title="Color Mode">
230 <SegmentControl
231 v-model="isDark"
232 :options="[
233 { value: false, label: 'Light' },
234 { value: true, label: 'Dark' },
235 ]"
236 :disabled="isColorSchemaConfigured"
237 />
238 </FormItem>
239 <FormItem title="With clicks">
240 <FormCheckbox v-model="isPrintWithClicks" />
241 </FormItem>
242 </div>
243 <div class="flex-grow" />
244 <div class="min-w-fit" flex="~ col gap-3">
245 <div border="~ main rounded-lg" p3 flex="~ col gap-2">
246 <h2>Export as Vector File</h2>
247 <div class="flex flex-col gap-2 min-w-max">
248 <button class="slidev-form-button" @click="pdf">
249 PDF
250 </button>
251 </div>
252 </div>
253
254 <div border="~ main rounded-lg" p3 flex="~ col gap-2" :class="isScreenshotSupported ? '' : 'border-orange'">
255 <h2>Export as Images</h2>
256 <div v-if="!isScreenshotSupported" class="min-w-full w-0 text-orange/100 p-1 mb-2 bg-orange/10 rounded">
257 <span class="i-carbon:warning-alt inline-block mb--.5" />
258 Your browser may not support image capturing.
259 If you encounter issues, please use a modern Chromium-based browser,
260 or export via the CLI.
261 </div>
262 <div class="flex flex-col gap-2 min-w-max">
263 <button class="slidev-form-button" @click="pptx">
264 PPTX
265 </button>
266 <button class="slidev-form-button" @click="pngsGz">
267 PNGs.gz
268 </button>
269 </div>
270 <div w-full h-1px border="t main" my2 />
271 <div class="relative flex flex-col gap-2 flex-nowrap">
272 <div class="flex flex-col gap-2 min-w-max">
273 <button v-if="capturedImages" class="slidev-form-button flex justify-center items-center gap-2" @click="capturedImages = null">
274 <span class="i-carbon:trash-can inline-block text-xl" />
275 Clear Captured Images
276 </button>
277 <button v-else class="slidev-form-button flex justify-center items-center gap-2" @click="capturePngs">
278 <div class="i-carbon:drop-photo inline-block text-xl" />
279 Pre-capture slides as Images
280 </button>
281 <FormItem title="Delay" description="Delay between capturing each slide in milliseconds.<br>Increase this value if slides are captured incompletely. <br>(Not related to PDF export)">
282 <input v-model="captureDelay" type="number" step="50" min="50">
283 </FormItem>
284 </div>
285 </div>
286 </div>
287 </div>
288 </div>
289 <div id="export-container" ref="export-container" relative>
290 <div print:hidden fixed right-5 bottom-5 px2 py0 z-label slidev-glass-effect>
291 <span op75>Rendering as {{ capturedImages ? 'Captured Images' : 'DOM' }} </span>
292 </div>
293 <div v-show="!capturedImages" id="export-content" ref="export-content">
294 <PrintSlide v-for="route, index in slides" :key="index" :hidden="!printRange.includes(index + 1)" :route />
295 </div>
296 <div v-if="capturedImages" id="export-content-images" class="print:hidden grid">
297 <div v-for="png, i of capturedImages" :key="i" class="print-slide-container">
298 <img :src="png.dataUrl">
299 </div>
300 </div>
301 </div>
302 <div id="twoslash-container" />
303 <ExportPdfTip v-model="showExportPdfTip" @print="doPrint" />
304 </div>
305 </template>
306
307 <style scoped>
308 @media not print {
309 #export-container {
310 scrollbar-width: thin;
311 scroll-behavior: smooth;
312 --uno: w-full overflow-x-hidden overflow-y-auto max-h-full max-w-300 p-6;
313 }
314
315 #export-content {
316 transform: v-bind('`scale(${scale})`');
317 margin-bottom: v-bind('contentMarginBottom');
318 --uno: origin-tl;
319 }
320
321 #export-content,
322 #export-content-images {
323 --uno: flex flex-col gap-2;
324 }
325 }
326
327 @media print {
328 #export-content {
329 transform: scale(1);
330 display: block !important;
331 }
332 }
333
334 label {
335 --uno: text-xl flex gap-2 items-center select-none;
336
337 span {
338 --uno: flex-grow;
339 }
340
341 input[type='text'],
342 input[type='number'] {
343 --uno: border border-main rounded px-2 py-1;
344 }
345 }
346
347 h2 {
348 --uno: font-500 op-70;
349 }
350
351 #export-content {
352 --uno: pointer-events-none;
353 }
354
355 #export-container {
356 --slidev-slide-container-scale: var(--slidev-slide-scale);
357 }
358 </style>
359
360 <style>
361 @media print {
362 html,
363 body,
364 #app {
365 overflow: unset !important;
366 }
367 }
368
369 @media not print {
370 #export-content-images .print-slide-container,
371 #export-content .print-slide-container {
372 --uno: border border-main rounded-md shadow of-hidden;
373 }
374 }
375 </style>
376
376 lines Plain Text