From cc27dcd4f397c535cdcd30f0ea3ef2a8bc1849ce Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Fri, 8 Mar 2024 00:09:49 +0100 Subject: [PATCH] fix: clicks missing when going backward --- packages/client/composables/useClicks.ts | 12 ++++++++++-- packages/client/composables/useNav.ts | 20 ++++++++++++-------- packages/client/internals/SlideWrapper.vue | 21 ++++++++++++++++++--- packages/client/logic/route.ts | 7 ++++++- packages/types/src/types.ts | 1 + 5 files changed, 47 insertions(+), 14 deletions(-) diff --git a/packages/client/composables/useClicks.ts b/packages/client/composables/useClicks.ts index 5543071089..817af39363 100644 --- a/packages/client/composables/useClicks.ts +++ b/packages/client/composables/useClicks.ts @@ -23,6 +23,7 @@ function useClicksContextBase(current: Ref, clicksOverrides?: number): C }, relativeOffsets, map, + onMounted() {}, resolve(at, size = 1) { const [isRelative, value] = normalizeAtProp(at) if (isRelative) { @@ -57,8 +58,7 @@ function useClicksContextBase(current: Ref, clicksOverrides?: number): C get total() { // eslint-disable-next-line no-unused-expressions routeForceRefresh.value - return clicksOverrides - ?? Math.max(0, ...[...map.values()].map(v => v.max || 0)) + return clicksOverrides ?? Math.max(0, ...[...map.values()].map(v => v.max || 0)) }, } } @@ -68,6 +68,7 @@ const queryClicksRaw = useRouteQuery('clicks', '0') export function usePrimaryClicks(route: SlideRoute): ClicksContext { if (route?.meta?.__clicksContext) return route.meta.__clicksContext + const thisNo = route.no const current = computed({ get() { @@ -92,6 +93,13 @@ export function usePrimaryClicks(route: SlideRoute): ClicksContext { current, route?.meta?.clicks, ) + + // On slide mounted, make sure the query is not greater than the total + context.onMounted = () => { + if (queryClicksRaw.value) + queryClicksRaw.value = Math.min(queryClicksRaw.value, context.total) + } + if (route?.meta) route.meta.__clicksContext = context return context diff --git a/packages/client/composables/useNav.ts b/packages/client/composables/useNav.ts index 8273bb1d14..6174445d6f 100644 --- a/packages/client/composables/useNav.ts +++ b/packages/client/composables/useNav.ts @@ -3,7 +3,8 @@ import type { ComputedRef, Ref, TransitionGroupProps } from 'vue' import { computed, ref, watch } from 'vue' import type { Router } from 'vue-router' import { getCurrentTransition } from '../logic/transition' -import { getSlidePath } from '../logic/slides' +import { getSlide, getSlidePath } from '../logic/slides' +import { CLICKS_MAX } from '../constants' import { useTocTree } from './useTocTree' import { skipTransition } from './hmr' import { slides } from '#slidev/slides' @@ -121,12 +122,12 @@ export function useNavBase( async function prevSlide(lastClicks = true) { clicksDirection.value = -1 const next = Math.max(1, currentSlideNo.value - 1) - await go(next) - if (lastClicks && clicksTotal.value) { - router?.replace({ - query: { ...router.currentRoute.value.query, clicks: clicksTotal.value }, - }) - } + await go( + next, + lastClicks + ? getSlide(next)?.meta.__clicksContext?.total ?? CLICKS_MAX + : undefined, + ) } function goFirst() { @@ -141,7 +142,10 @@ export function useNavBase( skipTransition.value = false await router?.push({ path: getSlidePath(page), - query: { ...router.currentRoute.value.query, clicks }, + query: { + ...router.currentRoute.value.query, + clicks: clicks || undefined, + }, }) } diff --git a/packages/client/internals/SlideWrapper.vue b/packages/client/internals/SlideWrapper.vue index a6a999ffc9..b4f6a59c29 100644 --- a/packages/client/internals/SlideWrapper.vue +++ b/packages/client/internals/SlideWrapper.vue @@ -1,5 +1,5 @@