返回 slidev
context.ts
根目录 / packages / client / context.ts
1 import { injectLocal, objectOmit } from '@vueuse/core'
2 import { computed, ref, toRef } from 'vue'
3 import {
4 FRONTMATTER_FIELDS,
5 HEADMATTER_FIELDS,
6 injectionClicksContext,
7 injectionCurrentPage,
8 injectionFrontmatter,
9 injectionRenderContext,
10 injectionRoute,
11 injectionSlideScale,
12 injectionSlidevContext,
13 injectionSlideZoom,
14 } from './constants'
15
16 /**
17 * Get the current slide context, should be called inside the setup function of a component inside slide
18 */
19 export function useSlideContext() {
20 const $slidev = injectLocal(injectionSlidevContext)!
21 const $nav = toRef($slidev, 'nav')
22 const $clicksContext = injectLocal(injectionClicksContext)!.value
23 const $clicks = toRef($clicksContext, 'current')
24 const $page = injectLocal(injectionCurrentPage)!
25 const $renderContext = injectLocal(injectionRenderContext)!
26 const $frontmatter = injectLocal(injectionFrontmatter, {})
27 const $route = injectLocal(injectionRoute, undefined)
28 const $scale = injectLocal(injectionSlideScale, ref(1))
29 const $zoom = injectLocal(injectionSlideZoom, computed(() => 1))
30
31 return {
32 $slidev,
33 $nav,
34 $clicksContext,
35 $clicks,
36 $page,
37 $route,
38 $renderContext,
39 $frontmatter,
40 $scale,
41 $zoom,
42 }
43 }
44
45 export type SlideContext = ReturnType<typeof useSlideContext>
46
47 /**
48 * Convert frontmatter options to props for v-bind
49 * It removes known options fields, and expose an extra `frontmatter` field that contains full frontmatter
50 *
51 * @internal
52 */
53 export function frontmatterToProps(frontmatter: Record<string, any>, pageNo: number) {
54 return {
55 ...objectOmit(frontmatter, pageNo === 0 ? HEADMATTER_FIELDS : FRONTMATTER_FIELDS),
56 frontmatter,
57 }
58 }
59
59 lines TYPESCRIPT