From 914baf0790eb55104020dddc77898807a9260590 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Sun, 25 Feb 2024 19:26:42 +0100 Subject: [PATCH] refactor: allow set `current` in `ClicksContext` --- packages/client/composables/useClicks.ts | 29 ++++++++++++------- packages/client/context.ts | 2 +- packages/client/internals/NoteDisplay.vue | 13 +++++---- packages/client/internals/NoteEditor.vue | 8 +++-- packages/client/internals/NoteStatic.vue | 5 ++-- .../client/internals/OverviewClicksSlider.vue | 9 +++--- packages/client/internals/PrintSlide.vue | 10 +++++-- packages/client/internals/SlidesOverview.vue | 2 +- packages/client/pages/overview.vue | 13 ++++----- packages/client/pages/presenter.vue | 4 +-- packages/slidev/node/cli.ts | 4 ++- packages/types/src/types.ts | 2 +- 12 files changed, 59 insertions(+), 42 deletions(-) diff --git a/packages/client/composables/useClicks.ts b/packages/client/composables/useClicks.ts index b16baf3c26..0396666688 100644 --- a/packages/client/composables/useClicks.ts +++ b/packages/client/composables/useClicks.ts @@ -1,16 +1,13 @@ import { sum } from '@antfu/utils' import type { ClicksContext } from '@slidev/types' import type { Ref } from 'vue' -import { ref, shallowReactive } from 'vue' +import { computed, ref, shallowReactive } from 'vue' import type { RouteRecordRaw } from 'vue-router' import { currentRoute, isPrintMode, isPrintWithClicks, queryClicks, routeForceRefresh } from '../logic/nav' import { normalizeAtProp } from '../logic/utils' import { CLICKS_MAX } from '../constants' -/** - * @internal - */ -export function useClicksContextBase(getCurrent: () => number, clicksOverrides?: number): ClicksContext { +function useClicksContextBase(current: Ref, clicksOverrides?: number): ClicksContext { const relativeOffsets: ClicksContext['relativeOffsets'] = new Map() const map: ClicksContext['map'] = shallowReactive(new Map()) @@ -19,7 +16,10 @@ export function useClicksContextBase(getCurrent: () => number, clicksOverrides?: return isPrintMode.value && !isPrintWithClicks.value }, get current() { - return getCurrent() + return current.value + }, + set current(value) { + current.value = value }, relativeOffsets, map, @@ -67,8 +67,8 @@ export function usePrimaryClicks(route: RouteRecordRaw | undefined): ClicksConte if (route?.meta?.__clicksContext) return route.meta.__clicksContext const thisPath = +(route?.path ?? CLICKS_MAX) - const context = useClicksContextBase( - () => { + const current = computed({ + get() { const currentPath = +(currentRoute.value?.path ?? CLICKS_MAX) if (currentPath === thisPath) return queryClicks.value @@ -77,6 +77,14 @@ export function usePrimaryClicks(route: RouteRecordRaw | undefined): ClicksConte else return 0 }, + set(v) { + const currentPath = +(currentRoute.value?.path ?? CLICKS_MAX) + if (currentPath === thisPath) + queryClicks.value = v + }, + }) + const context = useClicksContextBase( + current, route?.meta?.clicks, ) if (route?.meta) @@ -84,7 +92,6 @@ export function usePrimaryClicks(route: RouteRecordRaw | undefined): ClicksConte return context } -export function useFixedClicks(route?: RouteRecordRaw | undefined, currentInit = 0): [Ref, ClicksContext] { - const current = ref(currentInit) - return [current, useClicksContextBase(() => current.value, route?.meta?.clicks)] +export function useFixedClicks(route?: RouteRecordRaw | undefined, currentInit = 0): ClicksContext { + return useClicksContextBase(ref(currentInit), route?.meta?.clicks) } diff --git a/packages/client/context.ts b/packages/client/context.ts index d032fcbbcb..d51794a946 100644 --- a/packages/client/context.ts +++ b/packages/client/context.ts @@ -12,7 +12,7 @@ import { injectionSlidevContext, } from './constants' -const clicksContextFallback = shallowRef(useFixedClicks()[1]) +const clicksContextFallback = shallowRef(useFixedClicks()) /** * Get the current slide context, should be called inside the setup function of a component inside slide diff --git a/packages/client/internals/NoteDisplay.vue b/packages/client/internals/NoteDisplay.vue index b62b47a9d4..cc3f6848d9 100644 --- a/packages/client/internals/NoteDisplay.vue +++ b/packages/client/internals/NoteDisplay.vue @@ -1,5 +1,6 @@ diff --git a/packages/client/internals/SlidesOverview.vue b/packages/client/internals/SlidesOverview.vue index e4e251a191..cbac74f8d8 100644 --- a/packages/client/internals/SlidesOverview.vue +++ b/packages/client/internals/SlidesOverview.vue @@ -140,7 +140,7 @@ watchEffect(() => { -import type { Ref } from 'vue' import { computed, nextTick, onMounted, reactive, ref } from 'vue' import { useHead } from '@unhead/vue' import type { RouteRecordRaw } from 'vue-router' @@ -30,8 +29,8 @@ const wordCounts = computed(() => rawRoutes.map(route => wordCount(route.meta?.s const totalWords = computed(() => wordCounts.value.reduce((a, b) => a + b, 0)) const totalClicks = computed(() => rawRoutes.map(route => getSlideClicks(route)).reduce((a, b) => a + b, 0)) -const clicksContextMap = new WeakMap, ClicksContext]>() -function getClickContext(route: RouteRecordRaw) { +const clicksContextMap = new WeakMap() +function getClicksContext(route: RouteRecordRaw) { // We create a local clicks context to calculate the total clicks of the slide if (!clicksContextMap.has(route)) clicksContextMap.set(route, useFixedClicks(route, CLICKS_MAX)) @@ -39,7 +38,7 @@ function getClickContext(route: RouteRecordRaw) { } function getSlideClicks(route: RouteRecordRaw) { - return route.meta?.clicks || getClickContext(route)?.[1]?.total + return route.meta?.clicks || getClicksContext(route)?.total } function wordCount(str: string) { @@ -152,7 +151,7 @@ onMounted(() => { { @@ -182,7 +181,7 @@ onMounted(() => { class="max-w-250 w-250 text-lg rounded p3" :auto-height="true" :editing="edittingNote === idx" - :clicks="getClickContext(route)[0].value" + :clicks="getClicksContext(route).current" @dblclick="edittingNote !== idx ? edittingNote = idx : null" @update:editing="edittingNote = null" /> diff --git a/packages/client/pages/presenter.vue b/packages/client/pages/presenter.vue index 63b7361a43..7d388ad78c 100644 --- a/packages/client/pages/presenter.vue +++ b/packages/client/pages/presenter.vue @@ -49,7 +49,7 @@ const nextFrameClicksCtx = computed(() => { return nextFrame.value && clicksCtxMap[+nextFrame.value[0].path - 1] }) watch([currentRoute, queryClicks], () => { - nextFrameClicksCtx.value && (nextFrameClicksCtx.value[0].value = nextFrame.value![1]) + nextFrameClicksCtx.value && (nextFrameClicksCtx.value.current = nextFrame.value![1]) }, { immediate: true }) const Editor = shallowRef() @@ -123,7 +123,7 @@ onMounted(() => { ${cyan(`http://localhost:${bold(port)}/`)}`) if (query) console.log(`${dim(' private slide show ')} > ${cyan(`http://localhost:${bold(port)}/${query}`)}`) console.log(`${dim(' presenter mode ')} > ${blue(`http://localhost:${bold(port)}${presenterPath}`)}`) + console.log(`${dim(' slides overview ')} > ${blue(`http://localhost:${bold(port)}${overviewPath}`)}`) if (options.inspect) - console.log(`${dim(' inspector')} > ${yellow(`http://localhost:${bold(port)}/__inspect/`)}`) + console.log(`${dim(' vite inspector')} > ${yellow(`http://localhost:${bold(port)}/__inspect/`)}`) let lastRemoteUrl = '' diff --git a/packages/types/src/types.ts b/packages/types/src/types.ts index e689f246b3..9247d4d0b0 100644 --- a/packages/types/src/types.ts +++ b/packages/types/src/types.ts @@ -143,8 +143,8 @@ export type ResolvedClicksInfo = Required export type ClicksMap = Map export interface ClicksContext { + current: number readonly disabled: boolean - readonly current: number readonly relativeOffsets: ClicksRelativeEls readonly map: ClicksMap resolve: (at: string | number, size?: number) => {