| 1 | import type { SlideRoute } from '@slidev/types' |
| 2 | import type { DirectiveBinding, InjectionKey } from 'vue' |
| 3 | import { configs } from './env' |
| 4 | |
| 5 | export function getSlideClass(route?: SlideRoute, extra = '') { |
| 6 | const classes = ['slidev-page', extra] |
| 7 | |
| 8 | const no = route?.meta?.slide?.no |
| 9 | if (no != null) |
| 10 | classes.push(`slidev-page-${no}`) |
| 11 | |
| 12 | return classes.filter(Boolean).join(' ') |
| 13 | } |
| 14 | |
| 15 | export async function downloadPDF() { |
| 16 | const { saveAs } = await import('file-saver') |
| 17 | saveAs( |
| 18 | typeof configs.download === 'string' |
| 19 | ? configs.download |
| 20 | : configs.exportFilename |
| 21 | ? `${configs.exportFilename}.pdf` |
| 22 | : `${import.meta.env.BASE_URL}slidev-exported.pdf`, |
| 23 | `${configs.title}.pdf`, |
| 24 | ) |
| 25 | } |
| 26 | |
| 27 | export function directiveInject<T = unknown>(dir: DirectiveBinding<any>, key: InjectionKey<T> | string, defaultValue?: T): T | undefined { |
| 28 | return (dir.instance?.$ as any).provides[key as any] ?? defaultValue |
| 29 | } |
| 30 | |
| 31 | export function directiveProvide<T = unknown>(dir: DirectiveBinding<any>, key: InjectionKey<T> | string, value?: T) { |
| 32 | const instance = dir.instance?.$ as any |
| 33 | if (instance) { |
| 34 | let provides = instance.provides |
| 35 | const parentProvides = instance.parent?.provides |
| 36 | if (provides === parentProvides) |
| 37 | provides = instance.provides = Object.create(parentProvides) |
| 38 | provides[key as any] = value |
| 39 | } |
| 40 | } |
| 41 |