Skip to content

Commit cc27dcd

Browse files
committed
fix: clicks missing when going backward
1 parent 9466212 commit cc27dcd

File tree

5 files changed

+47
-14
lines changed

5 files changed

+47
-14
lines changed

packages/client/composables/useClicks.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function useClicksContextBase(current: Ref<number>, clicksOverrides?: number): C
2323
},
2424
relativeOffsets,
2525
map,
26+
onMounted() {},
2627
resolve(at, size = 1) {
2728
const [isRelative, value] = normalizeAtProp(at)
2829
if (isRelative) {
@@ -57,8 +58,7 @@ function useClicksContextBase(current: Ref<number>, clicksOverrides?: number): C
5758
get total() {
5859
// eslint-disable-next-line no-unused-expressions
5960
routeForceRefresh.value
60-
return clicksOverrides
61-
?? Math.max(0, ...[...map.values()].map(v => v.max || 0))
61+
return clicksOverrides ?? Math.max(0, ...[...map.values()].map(v => v.max || 0))
6262
},
6363
}
6464
}
@@ -68,6 +68,7 @@ const queryClicksRaw = useRouteQuery('clicks', '0')
6868
export function usePrimaryClicks(route: SlideRoute): ClicksContext {
6969
if (route?.meta?.__clicksContext)
7070
return route.meta.__clicksContext
71+
7172
const thisNo = route.no
7273
const current = computed({
7374
get() {
@@ -92,6 +93,13 @@ export function usePrimaryClicks(route: SlideRoute): ClicksContext {
9293
current,
9394
route?.meta?.clicks,
9495
)
96+
97+
// On slide mounted, make sure the query is not greater than the total
98+
context.onMounted = () => {
99+
if (queryClicksRaw.value)
100+
queryClicksRaw.value = Math.min(queryClicksRaw.value, context.total)
101+
}
102+
95103
if (route?.meta)
96104
route.meta.__clicksContext = context
97105
return context

packages/client/composables/useNav.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import type { ComputedRef, Ref, TransitionGroupProps } from 'vue'
33
import { computed, ref, watch } from 'vue'
44
import type { Router } from 'vue-router'
55
import { getCurrentTransition } from '../logic/transition'
6-
import { getSlidePath } from '../logic/slides'
6+
import { getSlide, getSlidePath } from '../logic/slides'
7+
import { CLICKS_MAX } from '../constants'
78
import { useTocTree } from './useTocTree'
89
import { skipTransition } from './hmr'
910
import { slides } from '#slidev/slides'
@@ -121,12 +122,12 @@ export function useNavBase(
121122
async function prevSlide(lastClicks = true) {
122123
clicksDirection.value = -1
123124
const next = Math.max(1, currentSlideNo.value - 1)
124-
await go(next)
125-
if (lastClicks && clicksTotal.value) {
126-
router?.replace({
127-
query: { ...router.currentRoute.value.query, clicks: clicksTotal.value },
128-
})
129-
}
125+
await go(
126+
next,
127+
lastClicks
128+
? getSlide(next)?.meta.__clicksContext?.total ?? CLICKS_MAX
129+
: undefined,
130+
)
130131
}
131132

132133
function goFirst() {
@@ -141,7 +142,10 @@ export function useNavBase(
141142
skipTransition.value = false
142143
await router?.push({
143144
path: getSlidePath(page),
144-
query: { ...router.currentRoute.value.query, clicks },
145+
query: {
146+
...router.currentRoute.value.query,
147+
clicks: clicks || undefined,
148+
},
145149
})
146150
}
147151

packages/client/internals/SlideWrapper.vue

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { computed, defineAsyncComponent, ref, toRef } from 'vue'
2+
import { computed, defineAsyncComponent, defineComponent, h, onMounted, ref, toRef } from 'vue'
33
import type { PropType } from 'vue'
44
import { provideLocal } from '@vueuse/core'
55
import type { ClicksContext, RenderContext, SlideRoute } from '@slidev/types'
@@ -20,6 +20,7 @@ const props = defineProps({
2020
default: false,
2121
},
2222
is: {
23+
type: Function as PropType<() => any>,
2324
required: true,
2425
},
2526
route: {
@@ -47,14 +48,28 @@ const style = computed(() => {
4748
})
4849
4950
const SlideComponent = defineAsyncComponent({
50-
loader: (props.is as any),
51+
loader: async () => {
52+
const component = await props.is()
53+
return defineComponent({
54+
setup(_, { attrs }) {
55+
onMounted(() => {
56+
props.clicksContext.onMounted()
57+
})
58+
return () => h(component.default, attrs)
59+
},
60+
})
61+
},
5162
delay: 300,
5263
loadingComponent: SlideLoading,
5364
})
5465
</script>
5566

5667
<template>
57-
<component :is="SlideComponent" :style="style" :class="{ 'disable-view-transition': !['slide', 'presenter'].includes(props.renderContext) }" />
68+
<component
69+
:is="SlideComponent"
70+
:style="style"
71+
:class="{ 'disable-view-transition': !['slide', 'presenter'].includes(props.renderContext) }"
72+
/>
5873
</template>
5974

6075
<style scoped>

packages/client/logic/route.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ export function useRouteQuery<T extends string | string[]>(
1919
},
2020
set(v) {
2121
nextTick(() => {
22-
router[unref(mode) as 'replace' | 'push']({ query: { ...router.currentRoute.value.query, [name]: v } })
22+
router[unref(mode) as 'replace' | 'push']({
23+
query: {
24+
...router.currentRoute.value.query,
25+
[name]: v === defaultValue ? undefined : v,
26+
},
27+
})
2328
})
2429
},
2530
})

packages/types/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ export interface ClicksContext {
161161
}
162162
register: (el: ClicksElement, info: ClicksInfo) => void
163163
unregister: (el: ClicksElement) => void
164+
onMounted: () => void
164165
readonly currentOffset: number
165166
readonly total: number
166167
}

0 commit comments

Comments
 (0)