返回 slidev
VSwitch.ts
根目录 / packages / client / builtin / VSwitch.ts
1 import type { PropType, Ref, Slot, TransitionGroupProps, VNode } from 'vue'
2 import { recomputeAllPoppers } from 'floating-vue'
3 import { defineComponent, h, onMounted, onUnmounted, ref, TransitionGroup, watchEffect } from 'vue'
4 import { CLASS_VCLICK_CURRENT, CLASS_VCLICK_DISPLAY_NONE, CLASS_VCLICK_PRIOR, CLASS_VCLICK_TARGET, CLICKS_MAX } from '../constants'
5 import { useSlideContext } from '../context'
6 import { resolveTransition } from '../logic/transition'
7 import { makeId } from '../logic/utils'
8 import { hmrSkipTransition } from '../state'
9
10 export default defineComponent({
11 props: {
12 at: {
13 type: [Number, String],
14 default: '+1',
15 },
16 /**
17 * unmount or hide the content when it's not visible
18 */
19 unmount: {
20 type: Boolean,
21 default: false,
22 },
23 transition: {
24 type: [Object, String, Boolean] as PropType<TransitionGroupProps | string | false>,
25 default: false,
26 },
27 tag: {
28 type: String,
29 default: 'div',
30 },
31 childTag: {
32 type: String,
33 default: 'div',
34 },
35 },
36 setup({ at, unmount, transition, tag, childTag }, { slots }) {
37 const slotEntries = Object.entries(slots).sort((a, b) => -a[0].split('-')[0] + +b[0].split('-')[0])
38 const contents: [start: number, end: number, slot: Slot<any> | undefined, elRef: Ref<HTMLElement | undefined>][] = []
39
40 let lastStart: number | undefined
41 for (const [range, slot] of slotEntries) {
42 const elRef = ref<HTMLElement>()
43 if (Number.isFinite(+range)) {
44 contents.push([
45 +range,
46 lastStart ?? +range + 1,
47 slot,
48 elRef,
49 ])
50 lastStart = +range
51 }
52 else {
53 const [start, end] = range.split('-').map(Number)
54 if (!Number.isFinite(start) || !Number.isFinite(end))
55 throw new TypeError(`Invalid range for v-switch: ${range}`)
56 contents.push([start, end, slot, elRef])
57 lastStart = start
58 }
59 }
60
61 const size = Math.max(...contents.map(c => c[1])) - 1
62 const id = makeId()
63 const offset = ref(0)
64
65 const { $clicksContext: clicks, $nav: nav } = useSlideContext()
66
67 onMounted(() => {
68 const clicksInfo = clicks.calculateSince(at, size)
69 if (!clicksInfo) {
70 offset.value = CLICKS_MAX
71 return
72 }
73 clicks.register(id, clicksInfo)
74 watchEffect(() => {
75 offset.value = clicksInfo.currentOffset.value + 1
76 })
77 })
78
79 onUnmounted(() => {
80 clicks.unregister(id)
81 })
82
83 function onAfterLeave() {
84 // Refer to SlidesShow.vue
85 hmrSkipTransition.value = true
86 recomputeAllPoppers()
87 }
88 const transitionProps = transition && {
89 ...resolveTransition(transition, nav.value.navDirection < 0),
90 tag,
91 onAfterLeave,
92 }
93
94 return () => {
95 const children: VNode[] = []
96 for (let i = contents.length - 1; i >= 0; i--) {
97 const [start, end, slot, ref] = contents[i]
98 const visible = start <= offset.value && offset.value < end
99 if (unmount && !visible)
100 continue
101 children.push(h(childTag, {
102 'key': i,
103 ref,
104 'class': [
105 CLASS_VCLICK_TARGET,
106 offset.value === start && CLASS_VCLICK_CURRENT,
107 offset.value >= end && CLASS_VCLICK_PRIOR,
108 !visible && CLASS_VCLICK_DISPLAY_NONE,
109 ].filter(Boolean),
110 'data-slidev-clicks-start': start,
111 'data-slidev-clicks-end': end,
112 }, slot?.()))
113 }
114 return transitionProps
115 ? h(TransitionGroup, hmrSkipTransition.value
116 ? {}
117 : transitionProps, () => children)
118 : h(tag, children)
119 }
120 },
121 })
122
122 lines TYPESCRIPT