From a467aa30cee5f4acd9b4ca6f54eebaaacd91e612 Mon Sep 17 00:00:00 2001 From: _Kerman Date: Sat, 11 May 2024 14:26:41 +0800 Subject: [PATCH 1/2] fix: Slidev client features enablement --- .../client/composables/useDragElements.ts | 4 ++- packages/client/composables/useFeatures.ts | 35 +++++++++++++++++++ packages/client/composables/useNav.ts | 4 --- packages/client/env.ts | 2 -- packages/client/internals/Controls.vue | 6 ++-- packages/client/internals/NavControls.vue | 11 +++--- packages/client/internals/PrintSlideClick.vue | 15 +++----- packages/client/internals/QuickOverview.vue | 4 ++- packages/client/internals/SlidesShow.vue | 9 ++--- packages/client/logic/contextMenu.ts | 8 +++-- packages/client/pages/play.vue | 4 ++- packages/client/pages/presenter.vue | 11 ++++-- packages/client/setup/context-menu.ts | 12 +++---- packages/client/setup/routes.ts | 3 +- packages/client/state/drawings.ts | 3 +- packages/slidev/node/vite/extendConfig.ts | 6 +--- shim.d.ts | 16 +++------ 17 files changed, 92 insertions(+), 61 deletions(-) create mode 100644 packages/client/composables/useFeatures.ts diff --git a/packages/client/composables/useDragElements.ts b/packages/client/composables/useDragElements.ts index 242925d972..87adc807a7 100644 --- a/packages/client/composables/useDragElements.ts +++ b/packages/client/composables/useDragElements.ts @@ -9,6 +9,7 @@ import { activeDragElement } from '../state' import { directiveInject } from '../utils' import { useSlideBounds } from './useSlideBounds' import { useDynamicSlideInfo } from './useSlideInfo' +import { useFeatures } from './useFeatures' export type DragElementDataSource = 'frontmatter' | 'prop' | 'directive' /** @@ -21,7 +22,8 @@ export type DragElementsUpdater = (id: string, posStr: string, type: DragElement const map: Record = {} export function useDragElementsUpdater(no: number) { - if (!(__DEV__ && __SLIDEV_FEATURE_EDITOR__)) + const features = useFeatures() + if (!features.editor) return () => {} if (map[no]) diff --git a/packages/client/composables/useFeatures.ts b/packages/client/composables/useFeatures.ts new file mode 100644 index 0000000000..359b3ff2a0 --- /dev/null +++ b/packages/client/composables/useFeatures.ts @@ -0,0 +1,35 @@ +import { createSharedComposable } from '@vueuse/core' +import { computed, reactive } from 'vue' +import { configs } from '../env' +import { useRouteQuery } from '../logic/route' +import { useNav } from './useNav' + +function satisfiesMode(mode: boolean | string) { + return mode === true || mode === __MODE__ +} + +export const staticFeatures = { + record: satisfiesMode(configs.record), + presenter: satisfiesMode(configs.presenter), + contextMenu: configs.contextMenu === true || configs.contextMenu === undefined || configs.contextMenu === __MODE__, +} + +export const useFeatures = createSharedComposable(() => { + const { isPresenter, isEmbedded } = useNav() + + const password = useRouteQuery('password') + const trusted = computed(() => !configs.remote || password.value === configs.remote) + + const drawings = computed(() => satisfiesMode(configs.drawings.enabled) && !isEmbedded.value) + const allowToDraw = computed(() => drawings.value && trusted.value && (!configs.drawings.presenterOnly || isPresenter.value) && !isEmbedded.value) + const editor = computed(() => configs.editor && trusted.value && __MODE__ === 'dev') + const enterPresenter = computed(() => staticFeatures.presenter && !isPresenter.value && trusted.value) + + return reactive({ + ...staticFeatures, + drawings, + allowToDraw, + editor, + enterPresenter, + }) +}) diff --git a/packages/client/composables/useNav.ts b/packages/client/composables/useNav.ts index 460cd9265b..fae31b9af1 100644 --- a/packages/client/composables/useNav.ts +++ b/packages/client/composables/useNav.ts @@ -10,7 +10,6 @@ import { getCurrentTransition } from '../logic/transition' import { getSlide, getSlidePath } from '../logic/slides' import { CLICKS_MAX } from '../constants' import { skipTransition } from '../logic/hmr' -import { configs } from '../env' import { useRouteQuery } from '../logic/route' import { useTocTree } from './useTocTree' import { createClicksContextBase } from './useClicks' @@ -76,7 +75,6 @@ export interface SlidevContextNavState { isPlaying: ComputedRef isPresenter: ComputedRef isNotesViewer: ComputedRef - isPresenterAvailable: ComputedRef hasPrimarySlide: ComputedRef currentSlideNo: ComputedRef currentSlideRoute: ComputedRef @@ -281,7 +279,6 @@ const useNavState = createSharedComposable((): SlidevContextNavState => { const isPlaying = computed(() => currentRoute.value.name === 'play') const isPresenter = computed(() => currentRoute.value.name === 'presenter') const isNotesViewer = computed(() => currentRoute.value.name === 'notes') - const isPresenterAvailable = computed(() => !isPresenter.value && (!configs.remote || query.value.get('password') === configs.remote)) const hasPrimarySlide = logicOr(isPlaying, isPresenter) const currentSlideNo = computed(() => hasPrimarySlide.value ? getSlide(currentRoute.value.params.no as string)?.no ?? 1 : 1) @@ -351,7 +348,6 @@ const useNavState = createSharedComposable((): SlidevContextNavState => { isPlaying, isPresenter, isNotesViewer, - isPresenterAvailable, hasPrimarySlide, currentSlideNo, currentSlideRoute, diff --git a/packages/client/env.ts b/packages/client/env.ts index 59dad897df..7099f5cc44 100644 --- a/packages/client/env.ts +++ b/packages/client/env.ts @@ -4,8 +4,6 @@ import configs from '#slidev/configs' export { configs } -export const mode = __DEV__ ? 'dev' : 'build' - export const slideAspect = ref(configs.aspectRatio ?? (16 / 9)) export const slideWidth = ref(configs.canvasWidth ?? 980) diff --git a/packages/client/internals/Controls.vue b/packages/client/internals/Controls.vue index dc840ae011..e155002310 100644 --- a/packages/client/internals/Controls.vue +++ b/packages/client/internals/Controls.vue @@ -3,20 +3,22 @@ import { shallowRef } from 'vue' import { showInfoDialog, showRecordingDialog } from '../state' import { configs } from '../env' import { useNav } from '../composables/useNav' +import { useFeatures } from '../composables/useFeatures' import QuickOverview from './QuickOverview.vue' import InfoDialog from './InfoDialog.vue' import Goto from './Goto.vue' import ContextMenu from './ContextMenu.vue' const { isEmbedded } = useNav() -const drawingEnabled = __SLIDEV_FEATURE_DRAWINGS__ && !configs.drawings.presenterOnly && !isEmbedded.value +const features = useFeatures() +const drawingEnabled = features.drawings && !configs.drawings.presenterOnly && !isEmbedded.value const DrawingControls = shallowRef() if (drawingEnabled) import('../internals/DrawingControls.vue').then(v => DrawingControls.value = v.default) const WebCamera = shallowRef() const RecordingDialog = shallowRef() -if (__SLIDEV_FEATURE_RECORD__) { +if (features.record) { import('./WebCamera.vue').then(v => WebCamera.value = v.default) import('./RecordingDialog.vue').then(v => RecordingDialog.value = v.default) } diff --git a/packages/client/internals/NavControls.vue b/packages/client/internals/NavControls.vue index 72444736fb..69afd5d66a 100644 --- a/packages/client/internals/NavControls.vue +++ b/packages/client/internals/NavControls.vue @@ -6,6 +6,7 @@ import { activeElement, breakpoints, fullscreen, presenterLayout, showEditor, sh import { configs } from '../env' import { useNav } from '../composables/useNav' import { useDrawings } from '../composables/useDrawings' +import { useFeatures } from '../composables/useFeatures' import Settings from './Settings.vue' import MenuButton from './MenuButton.vue' import VerticalDivider from './VerticalDivider.vue' @@ -25,7 +26,6 @@ const { hasPrev, isEmbedded, isPresenter, - isPresenterAvailable, next, prev, total, @@ -36,6 +36,7 @@ const { brush, drawingEnabled, } = useDrawings() +const features = useFeatures() const md = breakpoints.smaller('md') const { isFullscreen, toggle: toggleFullscreen } = fullscreen @@ -51,7 +52,7 @@ const barStyle = computed(() => props.persist : 'rounded-md bg-main shadow dark:border dark:border-main') const RecordingControls = shallowRef() -if (__SLIDEV_FEATURE_RECORD__) +if (features.record) import('./RecordingControls.vue').then(v => RecordingControls.value = v.default) @@ -102,7 +103,7 @@ if (__SLIDEV_FEATURE_RECORD__) -