Skip to content

Commit

Permalink
feat: add RenderWhen component (#616)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonai committed Jun 1, 2022
1 parent fb8054f commit 2a62962
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 3 deletions.
29 changes: 29 additions & 0 deletions packages/client/builtin/RenderWhen.vue
@@ -0,0 +1,29 @@
<script setup lang="ts">
import { computed, inject } from 'vue'
import type { RenderContext } from '@slidev/types'
import { injectionSlideContext } from '../constants'
type Context = 'main' | RenderContext
const props = defineProps<{
context: Context | Context[]
}>()
const { context } = props
const currentContext = inject(injectionSlideContext)
const shouldRender = computed(() => context instanceof Array ? context.some(contextMatch) : contextMatch(context))
function contextMatch(context: Context) {
if (context === currentContext)
return true
if (context === 'main' && (currentContext === 'slide' || currentContext === 'presenter'))
return true
return false
}
</script>

<template>
<slot v-if="shouldRender" />
</template>
2 changes: 2 additions & 0 deletions packages/client/constants.ts
@@ -1,5 +1,6 @@
import type { ComputedRef, InjectionKey, Ref } from 'vue'
import type { RouteRecordRaw } from 'vue-router'
import type { RenderContext } from '@slidev/types'
import type { SlidevContext } from './modules/context'

export const injectionClicks: InjectionKey<Ref<number>> = Symbol('v-click-clicks')
Expand All @@ -9,6 +10,7 @@ export const injectionClicksDisabled: InjectionKey<Ref<boolean>> = Symbol('v-cli
export const injectionSlideScale: InjectionKey<ComputedRef<number>> = Symbol('slidev-slide-scale')
export const injectionSlidevContext: InjectionKey<SlidevContext> = Symbol('slidev-slidev-context')
export const injectionRoute: InjectionKey<RouteRecordRaw> = Symbol('slidev-route')
export const injectionSlideContext: InjectionKey<RenderContext> = Symbol('slidev-slide-context')

export const CLASS_VCLICK_TARGET = 'slidev-vclick-target'
export const CLASS_VCLICK_HIDDEN = 'slidev-vclick-hidden'
Expand Down
2 changes: 1 addition & 1 deletion packages/client/internals/Play.vue
Expand Up @@ -49,7 +49,7 @@ if (__SLIDEV_FEATURE_DRAWINGS__)
@pointerdown="onClick"
>
<template #>
<SlidesShow />
<SlidesShow context="slidesshow" />
</template>
<template #controls>
<div
Expand Down
3 changes: 2 additions & 1 deletion packages/client/internals/Presenter.vue
Expand Up @@ -103,7 +103,7 @@ onMounted(() => {
class="h-full w-full"
>
<template #>
<SlidesShow />
<SlidesShow context="presenter" />
</template>
</SlideContainer>
</div>
Expand All @@ -120,6 +120,7 @@ onMounted(() => {
:clicks-disabled="false"
:class="getSlideClass(nextSlide.route)"
:route="nextSlide.route"
context="previewNext"
/>
</SlideContainer>
</div>
Expand Down
7 changes: 6 additions & 1 deletion packages/client/internals/SlideWrapper.ts
@@ -1,6 +1,6 @@
import { useVModel } from '@vueuse/core'
import { defineComponent, h, provide } from 'vue'
import { injectionClicks, injectionClicksDisabled, injectionClicksElements, injectionOrderMap, injectionRoute } from '../constants'
import { injectionClicks, injectionClicksDisabled, injectionClicksElements, injectionOrderMap, injectionRoute, injectionSlideContext } from '../constants'

export default defineComponent({
props: {
Expand All @@ -20,6 +20,10 @@ export default defineComponent({
type: Boolean,
default: false,
},
context: {
type: String,
default: 'main',
},
is: {
type: Object,
default: undefined,
Expand All @@ -38,6 +42,7 @@ export default defineComponent({
clicksElements.value.length = 0

provide(injectionRoute, props.route)
provide(injectionSlideContext, props.context)
provide(injectionClicks, clicks)
provide(injectionClicksDisabled, clicksDisabled)
provide(injectionClicksElements, clicksElements)
Expand Down
1 change: 1 addition & 0 deletions packages/client/internals/SlidesOverview.vue
Expand Up @@ -85,6 +85,7 @@ watchEffect(() => {
:clicks-disabled="true"
:class="getSlideClass(route)"
:route="route"
context="overview"
/>
<DrawingPreview :page="+route.path" />
</SlideContainer>
Expand Down
3 changes: 3 additions & 0 deletions packages/client/internals/SlidesShow.vue
Expand Up @@ -9,6 +9,8 @@ import GlobalTop from '/@slidev/global-components/top'
import GlobalBottom from '/@slidev/global-components/bottom'
import PresenterMouse from './PresenterMouse.vue'
defineProps<{ context: 'slide' | 'presenter' }>()
// preload next route
watch(currentRoute, () => {
if (currentRoute.value?.meta && currentRoute.value.meta.preload !== false)
Expand Down Expand Up @@ -37,6 +39,7 @@ if (__SLIDEV_FEATURE_DRAWINGS__ || __SLIDEV_FEATURE_DRAWINGS_PERSIST__)
:clicks-disabled="false"
:class="getSlideClass(route)"
:route="route"
:context="context"
/>
</template>
Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/types.ts
Expand Up @@ -55,3 +55,5 @@ export interface SlidevMarkdown {
entries?: string[]
themeMeta?: SlidevThemeMeta
}

export type RenderContext = 'slide' | 'overview' | 'presenter' | 'previewNext'

0 comments on commit 2a62962

Please sign in to comment.