返回 slidev
ClicksSlider.vue
根目录 / packages / client / internals / ClicksSlider.vue
1 <script setup lang="ts">
2 import type { ClicksContext } from '@slidev/types'
3 import { clamp, range } from '@antfu/utils'
4 import { computed, ref } from 'vue'
5 import { CLICKS_MAX } from '../constants'
6
7 const props = withDefaults(defineProps<{
8 clicksContext: ClicksContext
9 readonly?: boolean
10 active?: boolean
11 resettable?: boolean
12 compact?: boolean
13 attached?: boolean
14 }>(), {
15 active: true,
16 })
17
18 const emit = defineEmits<{
19 (type: 'activate'): void
20 (type: 'reset'): void
21 }>()
22
23 const total = computed(() => props.clicksContext.total)
24 const start = computed(() => clamp(0, props.clicksContext.clicksStart, total.value))
25 const length = computed(() => total.value - start.value + 1)
26 const current = computed({
27 get() {
28 if (props.resettable && !props.active)
29 return -1
30 return props.clicksContext.current > total.value
31 ? -1
32 : props.clicksContext.current
33 },
34 set(value: number) {
35 if (props.resettable && value < 0) {
36 emit('reset')
37 // eslint-disable-next-line vue/no-mutating-props
38 props.clicksContext.current = CLICKS_MAX
39 return
40 }
41 emit('activate')
42 // eslint-disable-next-line vue/no-mutating-props
43 props.clicksContext.current = value
44 },
45 })
46
47 const isReset = computed(() => props.resettable && current.value < 0)
48 const clicksRange = computed(() => range(start.value, total.value + 1))
49 const sliderEl = ref<HTMLElement>()
50 let pointerDown: { id: number, x: number, y: number } | undefined
51
52 function getPointerRatio(event: PointerEvent) {
53 const rect = sliderEl.value!.getBoundingClientRect()
54 return (event.clientX - rect.left) / Math.max(1, rect.width)
55 }
56
57 // Presses snap to a cell; drags switch only after crossing half a cell.
58 function setCurrentFromPointer(event: PointerEvent, snap: boolean) {
59 if (props.readonly || !sliderEl.value || (!snap && !(event.buttons & 1)))
60 return
61 const ratio = getPointerRatio(event)
62 // In resettable mode, dragging left of the rail restores the inactive state.
63 if (props.resettable && ratio < 0) {
64 current.value = -1
65 return
66 }
67 // Keep press-at-right-edge inside the last cell.
68 const position = clamp(0, ratio, snap ? 0.999999 : 1) * length.value
69 const currentOffset = clamp(0, current.value - start.value, length.value - 1)
70 let next = snap
71 ? start.value + Math.floor(position)
72 : current.value
73 if (!snap && position >= currentOffset + 1.5)
74 next = start.value + Math.floor(position - 0.5)
75 else if (!snap && position < currentOffset - 0.5)
76 next = start.value + Math.ceil(position - 0.5)
77 current.value = clamp(start.value, next, total.value)
78 }
79
80 function onPointerDown(event: PointerEvent) {
81 if (props.readonly)
82 return
83 sliderEl.value?.setPointerCapture(event.pointerId)
84 pointerDown = { id: event.pointerId, x: event.clientX, y: event.clientY }
85 setCurrentFromPointer(event, true)
86 }
87
88 function onPointerMove(event: PointerEvent) {
89 if (pointerDown?.id === event.pointerId) {
90 // Treat tiny movement after pointerdown as part of the click.
91 if (Math.abs(event.clientX - pointerDown.x) <= 3 && Math.abs(event.clientY - pointerDown.y) <= 3)
92 return
93 pointerDown = undefined
94 }
95 setCurrentFromPointer(event, false)
96 }
97 </script>
98
99 <template>
100 <div
101 class="flex gap-1 select-none"
102 :title="`Clicks in this slide: ${length}`"
103 :class="[attached ? 'items-end' : 'items-center', length && props.clicksContext.isMounted ? '' : 'op50']"
104 >
105 <div
106 class="flex items-center font-mono"
107 :class="[compact ? 'gap-1 min-w-0 mr0' : 'gap-0.2 min-w-16 mr1', attached ? 'h-[22px]' : '']"
108 >
109 <div class="i-carbon:cursor-1 text-sm op50" :class="compact ? 'ml-1' : ''" />
110 <template v-if="current >= 0 && current !== CLICKS_MAX && active">
111 <div v-if="!compact" flex-auto />
112 <span>
113 <span text-primary>{{ current }}</span>
114 <span op25 text-sm>/</span>
115 <span op50 text-sm>{{ total }}</span>
116 </span>
117 </template>
118 <div
119 v-else
120 op50
121 :class="compact ? '' : 'flex-auto pl1'"
122 >
123 <span
124 :class="compact ? 'inline-block text-center' : ''"
125 :style="compact ? { width: `${String(total).length * 2 + 1}ch`, marginLeft: '-0.25ch' } : undefined"
126 >{{ total }}</span>
127 </div>
128 </div>
129 <div
130 ref="sliderEl"
131 relative flex-auto font-mono flex="~"
132 touch-none
133 :class="[attached ? 'h-[22px]' : 'h5', isReset ? 'op80' : '']"
134 @pointerdown.capture="onPointerDown"
135 @pointermove="onPointerMove"
136 @pointerup="pointerDown = undefined"
137 @pointercancel="pointerDown = undefined"
138 >
139 <div
140 v-for="i of clicksRange" :key="i"
141 border="y main" of-hidden relative
142 :class="[
143 i === 0 ? 'border-l' : '',
144 i === 0 ? attached ? 'rounded-tl' : 'rounded-l' : '',
145 i === total ? 'border-r' : '',
146 i === total && +i !== +current ? attached ? 'rounded-tr' : 'rounded-r' : '',
147 attached ? 'border-b-0' : '',
148 ]"
149 :style="{ width: length > 0 ? `${1 / length * 100}%` : '100%' }"
150 >
151 <div
152 absolute inset-0
153 :class="(i <= current && active) ? 'bg-primary op15' : ''"
154 />
155 <div
156 v-if="+i === +current && active"
157 absolute inset-y-0 right-0 w-0.5 bg-primary z-1
158 />
159 <div
160 :class="[
161 (+i === +current && active) ? 'text-primary font-bold op100' : 'op30',
162 i !== total ? 'border-r-2 border-main' : '',
163 ]"
164 w-full h-full text-xs flex items-center justify-center z-1
165 >
166 {{ i }}
167 </div>
168 </div>
169 </div>
170 </div>
171 </template>
172
172 lines Plain Text