-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
context.ts
64 lines (58 loc) · 1.97 KB
/
context.ts
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
import { ref, shallowRef, toRef } from 'vue'
import { injectLocal, objectOmit, provideLocal } from '@vueuse/core'
import { useFixedClicks } from './composables/useClicks'
import {
FRONTMATTER_FIELDS,
HEADMATTER_FIELDS,
injectionClicksContext,
injectionCurrentPage,
injectionFrontmatter,
injectionRenderContext,
injectionSlidevContext,
} from './constants'
/**
* Get the current slidev context, should be called inside the setup function of a component inside slide
*/
export function useSlidevContext() {
const $slidev = injectLocal(injectionSlidevContext)!
const $nav = toRef($slidev, 'nav')
const $clicksContext = injectLocal(injectionClicksContext, shallowRef(useFixedClicks()[1]))!.value
const $clicks = toRef($clicksContext, 'current')
const $page = injectLocal(injectionCurrentPage)!
const $renderContext = injectLocal(injectionRenderContext, ref('slide'))
const $frontmatter = injectLocal(injectionFrontmatter, {})
return {
$slidev,
$nav,
$clicks,
$page,
$renderContext,
$frontmatter,
}
}
export function provideFrontmatter(frontmatter: Record<string, any>) {
provideLocal(injectionFrontmatter, frontmatter)
const {
$slidev,
$page,
} = useSlidevContext()
// update frontmatter in router to make HMR work better
const route = $slidev.nav.rawRoutes.find(i => i.path === String($page.value))
if (route?.meta?.slide?.frontmatter) {
for (const key of Object.keys(route.meta.slide.frontmatter)) {
if (!(key in frontmatter))
delete route.meta.slide.frontmatter[key]
}
Object.assign(route.meta.slide.frontmatter, frontmatter)
}
}
/**
* Convert frontmatter options to props for v-bind
* It removes known options fields, and expose an extra `frontmatter` field that contains full frontmatter
*/
export function frontmatterToProps(frontmatter: Record<string, any>, pageNo: number) {
return {
...objectOmit(frontmatter, pageNo === 0 ? HEADMATTER_FIELDS : FRONTMATTER_FIELDS),
frontmatter,
}
}