| 1 | import type { SlideRoute } from '@slidev/types' |
| 2 | import { tryOnMounted } from '@vueuse/core' |
| 3 | import { computed, watch } from 'vue' |
| 4 | import { slides } from '#slidev/slides' |
| 5 | import { useSlideContext } from '../context' |
| 6 | import { getSlideRoutePath } from './slidePath' |
| 7 | |
| 8 | export { slides } |
| 9 | |
| 10 | export function getSlide(no: number | string) { |
| 11 | return slides.value.find( |
| 12 | s => (s.no === +no || s.meta.slide?.frontmatter.routeAlias === no), |
| 13 | ) |
| 14 | } |
| 15 | |
| 16 | export function getSlidePath( |
| 17 | route: SlideRoute | number | string, |
| 18 | presenter: boolean, |
| 19 | exporting: boolean = false, |
| 20 | ) { |
| 21 | if (typeof route === 'number' || typeof route === 'string') |
| 22 | route = getSlide(route)! |
| 23 | return getSlideRoutePath(route, presenter, exporting) |
| 24 | } |
| 25 | |
| 26 | export function useIsSlideActive() { |
| 27 | const { $page, $nav } = useSlideContext() |
| 28 | // Use `$nav.value.currentSlideNo` rather than `useNav().currentSlideNo` to make it work in print/export mode. See https://github.com/slidevjs/slidev/issues/2310. |
| 29 | return computed(() => $page.value === $nav.value.currentSlideNo) |
| 30 | } |
| 31 | |
| 32 | export function onSlideEnter(cb: (to: number, from: number | undefined) => any) { |
| 33 | const { $page, $nav } = useSlideContext() |
| 34 | |
| 35 | tryOnMounted(() => { |
| 36 | watch(() => $nav.value.currentSlideNo, (to, from) => { |
| 37 | if ($page.value === to) |
| 38 | cb(to, from) |
| 39 | }, { immediate: true }) |
| 40 | }) |
| 41 | } |
| 42 | |
| 43 | export function onSlideLeave(cb: (to: number, from: number | undefined) => any) { |
| 44 | const { $page, $nav } = useSlideContext() |
| 45 | |
| 46 | tryOnMounted(() => { |
| 47 | watch(() => $nav.value.currentSlideNo, (to, from) => { |
| 48 | if ($page.value === from) |
| 49 | cb(to, from) |
| 50 | }) |
| 51 | }) |
| 52 | } |
| 53 |