| 1 | import type { ClicksInfo } from '@slidev/types' |
| 2 | import type { App, ObjectDirective } from 'vue' |
| 3 | import { MotionDirective } from '@vueuse/motion' |
| 4 | import { watch } from 'vue' |
| 5 | import { useNav } from '../composables/useNav' |
| 6 | import { injectionClicksContext, injectionCurrentPage, injectionRenderContext } from '../constants' |
| 7 | import { makeId } from '../logic/utils' |
| 8 | import { directiveInject } from '../utils' |
| 9 | import { resolvedClickMap } from './v-click' |
| 10 | |
| 11 | export function createVMotionDirectives() { |
| 12 | return { |
| 13 | install(app: App) { |
| 14 | const original = MotionDirective() as ObjectDirective |
| 15 | app.directive<HTMLElement | SVGElement, string>('motion', { |
| 16 | // @ts-expect-error extra prop |
| 17 | name: 'v-motion', |
| 18 | mounted(el, binding, node, prevNode) { |
| 19 | const clicksContext = directiveInject(binding, injectionClicksContext) |
| 20 | const thisPage = directiveInject(binding, injectionCurrentPage) |
| 21 | const renderContext = directiveInject(binding, injectionRenderContext) |
| 22 | const { currentPage, clicks: currentClicks, isPrintMode } = useNav() |
| 23 | |
| 24 | const props = node.props = { ...node.props } |
| 25 | |
| 26 | const variantInitial = { |
| 27 | ...props.initial, |
| 28 | ...props.variants?.['slidev-initial'], |
| 29 | } |
| 30 | const variantEnter = { |
| 31 | ...props.enter, |
| 32 | ...props.variants?.['slidev-enter'], |
| 33 | } |
| 34 | const variantLeave = { |
| 35 | ...props.leave, |
| 36 | ...props.variants?.['slidev-leave'], |
| 37 | } |
| 38 | delete props.initial |
| 39 | delete props.enter |
| 40 | delete props.leave |
| 41 | |
| 42 | const idPrefix = `${makeId()}-` |
| 43 | const clicks: { |
| 44 | id: string |
| 45 | at: number | [number, number] |
| 46 | variant: Record<string, unknown> |
| 47 | info: ClicksInfo | null | undefined |
| 48 | }[] = [] |
| 49 | |
| 50 | for (const k of Object.keys(props)) { |
| 51 | if (k.startsWith('click-')) { |
| 52 | const s = k.slice(6) |
| 53 | const at = s.includes('-') |
| 54 | ? s.split('-').map(Number) as [number, number] |
| 55 | : +s |
| 56 | const id = idPrefix + s |
| 57 | clicks.push({ |
| 58 | id, |
| 59 | at, |
| 60 | variant: { ...props[k] }, |
| 61 | info: clicksContext?.value.calculate(at), |
| 62 | }) |
| 63 | delete props[k] |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | clicks.sort((a, b) => (Array.isArray(a.at) |
| 68 | ? a.at[0] |
| 69 | : a.at) - (Array.isArray(b.at) |
| 70 | ? b.at[0] |
| 71 | : b.at)) |
| 72 | |
| 73 | original.created!(el, binding, node, prevNode) |
| 74 | original.mounted!(el, binding, node, prevNode) |
| 75 | |
| 76 | // @ts-expect-error extra prop |
| 77 | const motion = el.motionInstance |
| 78 | motion.clickIds = clicks.map(i => i.id) |
| 79 | motion.set(variantInitial) |
| 80 | motion.watchStopHandle = watch( |
| 81 | [thisPage, currentPage, currentClicks].filter(Boolean), |
| 82 | () => { |
| 83 | const visibility = resolvedClickMap.get(el)?.visibilityState.value ?? 'shown' |
| 84 | if (!clicksContext?.value || !['slide', 'presenter'].includes(renderContext?.value ?? '')) { |
| 85 | const mixedVariant: Record<string, unknown> = { |
| 86 | ...variantInitial, |
| 87 | ...variantEnter, |
| 88 | } |
| 89 | for (const { variant } of clicks) |
| 90 | Object.assign(mixedVariant, variant) |
| 91 | |
| 92 | motion.set(mixedVariant) |
| 93 | } |
| 94 | else if (isPrintMode.value || thisPage?.value === currentPage.value) { |
| 95 | if (visibility === 'shown') { |
| 96 | const mixedVariant: Record<string, unknown> = { |
| 97 | ...variantInitial, |
| 98 | ...variantEnter, |
| 99 | } |
| 100 | for (const { variant, info } of clicks) { |
| 101 | if (!info || info.isActive.value) |
| 102 | Object.assign(mixedVariant, variant) |
| 103 | } |
| 104 | if (isPrintMode.value) |
| 105 | motion.set(mixedVariant) // print with clicks |
| 106 | else |
| 107 | motion.apply(mixedVariant) |
| 108 | } |
| 109 | else { |
| 110 | motion.apply(visibility === 'before' |
| 111 | ? variantInitial |
| 112 | : variantLeave) |
| 113 | } |
| 114 | } |
| 115 | else { |
| 116 | motion.apply((thisPage?.value ?? -1) > currentPage.value |
| 117 | ? variantInitial |
| 118 | : variantLeave) |
| 119 | } |
| 120 | }, |
| 121 | { |
| 122 | immediate: true, |
| 123 | }, |
| 124 | ) |
| 125 | }, |
| 126 | unmounted(el) { |
| 127 | // @ts-expect-error extra prop |
| 128 | const motion = el.motionInstance |
| 129 | motion.watchStopHandle() |
| 130 | }, |
| 131 | }) |
| 132 | }, |
| 133 | } |
| 134 | } |
| 135 |