返回 slidev
presenter.vue
根目录 / packages / client / pages / presenter.vue
1 <script setup lang="ts">
2 import { useHead } from '@unhead/vue'
3 import { useEventListener, useLocalStorage, useMediaQuery, useWindowFocus } from '@vueuse/core'
4 import { computed, onMounted, ref, shallowRef, watch, watchEffect } from 'vue'
5 import { createClicksContextBase } from '../composables/useClicks'
6 import { useDrawings } from '../composables/useDrawings'
7 import { useMousePosInSlide } from '../composables/useMousePosInSlide'
8 import { useNav } from '../composables/useNav'
9 import { useSwipeControls } from '../composables/useSwipeControls'
10 import { useWakeLock } from '../composables/useWakeLock'
11 import { slidesTitle } from '../env'
12 import ClicksSlider from '../internals/ClicksSlider.vue'
13 import ContextMenu from '../internals/ContextMenu.vue'
14 import CurrentProgressBar from '../internals/CurrentProgressBar.vue'
15 import DrawingControls from '../internals/DrawingControls.vue'
16 import Goto from '../internals/Goto.vue'
17 import IconButton from '../internals/IconButton.vue'
18 import LaserPointer from '../internals/LaserPointer.vue'
19 import NavControls from '../internals/NavControls.vue'
20 import NoteEditable from '../internals/NoteEditable.vue'
21 import NoteStatic from '../internals/NoteStatic.vue'
22 import QuickOverview from '../internals/QuickOverview.vue'
23 import ScreenCaptureMirror from '../internals/ScreenCaptureMirror.vue'
24 import SegmentControl from '../internals/SegmentControl.vue'
25 import SlideContainer from '../internals/SlideContainer.vue'
26 import SlidesShow from '../internals/SlidesShow.vue'
27 import SlideWrapper from '../internals/SlideWrapper.vue'
28 import TimerBar from '../internals/TimerBar.vue'
29 import TimerInlined from '../internals/TimerInlined.vue'
30 import { onContextMenu } from '../logic/contextMenu'
31 import { registerShortcuts } from '../logic/shortcuts'
32 import { cursorStyle, decreasePresenterFontSize, increasePresenterFontSize, presenterLayout, presenterNotesFontSize, showEditor, showPresenterCursor } from '../state'
33 import { sharedState } from '../state/shared'
34
35 const inFocus = useWindowFocus()
36 const main = ref<HTMLDivElement>()
37 const gridContainer = ref<HTMLDivElement>()
38 const noteSection = ref<HTMLDivElement>()
39 const bottomSection = ref<HTMLDivElement>()
40
41 registerShortcuts()
42 useSwipeControls(main)
43 if (__SLIDEV_FEATURE_WAKE_LOCK__)
44 useWakeLock()
45
46 const {
47 clicksContext,
48 currentSlideNo,
49 currentSlideRoute,
50 hasNext,
51 nextRoute,
52 slides,
53 getPrimaryClicks,
54 } = useNav()
55 const { isDrawing } = useDrawings()
56
57 useHead({ title: `Presenter - ${slidesTitle}` })
58
59 const notesEditing = ref(false)
60
61 const clicksCtxMap = computed(() => slides.value.map((route) => {
62 const clicks = ref(0)
63 return {
64 context: createClicksContextBase(clicks, route?.meta.slide?.frontmatter.clicksStart ?? 0, route?.meta.clicks),
65 clicks,
66 }
67 }))
68 const nextFrame = computed(() => {
69 if (clicksContext.value.current < clicksContext.value.total)
70 return [currentSlideRoute.value!, clicksContext.value.current + 1] as const
71 else if (hasNext.value)
72 return [nextRoute.value, 0] as const
73 else
74 return null
75 })
76
77 const nextFrameClicksCtx = computed(() => {
78 return nextFrame.value && clicksCtxMap.value[nextFrame.value[0].no - 1]
79 })
80
81 watch(
82 nextFrame,
83 () => {
84 if (nextFrameClicksCtx.value && nextFrame.value)
85 nextFrameClicksCtx.value.clicks.value = nextFrame.value[1]
86 },
87 { immediate: true },
88 )
89
90 const mainSlideMode = useLocalStorage<'slides' | 'mirror'>('slidev-presenter-main-slide-mode', 'slides')
91
92 // Resize state (persisted)
93 const notesWidth = useLocalStorage('slidev-presenter-notes-width', 360)
94 const notesRowSize = useLocalStorage('slidev-presenter-notes-row-size', 280)
95 const bottomSectionHeight = ref(0)
96 const isResizingNotes = ref(false)
97 const isResizingNotesRow = ref(false)
98 const resizeStartX = ref(0)
99 const resizeStartWidth = ref(360)
100 const resizeStartY = ref(0)
101 const resizeStartRowSize = ref(280)
102
103 const RESIZER_LIMITS = {
104 minNotesWidth: 240,
105 minNotesRowSize: 160,
106 maxNotesWidthRatio: 0.7,
107 maxNotesRowHeightRatio: 0.75,
108 }
109
110 const isLayout1Wide = useMediaQuery('(min-aspect-ratio: 1/1)')
111 const isLayout1Stacked = useMediaQuery('(max-aspect-ratio: 3/5)')
112 const isNotesOnRight = computed(() => presenterLayout.value === 1 && isLayout1Wide.value)
113 const isNotesResizable = computed(() => !(presenterLayout.value === 1 && isLayout1Stacked.value))
114 const isNotesRowResizable = computed(() =>
115 (presenterLayout.value === 1 && !isLayout1Stacked.value) || presenterLayout.value === 2 || presenterLayout.value === 3,
116 )
117 const isNotesOnBottom = computed(() => presenterLayout.value === 1 && !isLayout1Stacked.value)
118
119 function clampNotesWidth(width: number) {
120 if (!Number.isFinite(width))
121 return RESIZER_LIMITS.minNotesWidth
122 return Math.max(RESIZER_LIMITS.minNotesWidth, Math.round(width))
123 }
124
125 function updateNotesWidthFromPointer(clientX: number) {
126 const container = gridContainer.value
127 if (!container)
128 return
129
130 const rect = container.getBoundingClientRect()
131 const deltaX = clientX - resizeStartX.value
132 const proposedWidth = isNotesOnRight.value
133 ? resizeStartWidth.value - deltaX
134 : resizeStartWidth.value + deltaX
135 const nextWidth = clampNotesWidth(proposedWidth)
136 const maxByViewport = Math.round(rect.width * RESIZER_LIMITS.maxNotesWidthRatio)
137 notesWidth.value = Math.min(nextWidth, Math.max(RESIZER_LIMITS.minNotesWidth, maxByViewport))
138 }
139
140 function onNotesResizeStart(e: PointerEvent) {
141 if (!isNotesResizable.value)
142 return
143 if (e.button !== 0)
144 return
145 e.preventDefault()
146 resizeStartX.value = e.clientX
147 resizeStartWidth.value = notesWidth.value
148 isResizingNotes.value = true
149 }
150
151 function clampNotesRowSize(size: number) {
152 if (!Number.isFinite(size))
153 return RESIZER_LIMITS.minNotesRowSize
154 return Math.max(RESIZER_LIMITS.minNotesRowSize, Math.round(size))
155 }
156
157 function updateNotesRowSizeFromPointer(clientY: number) {
158 const container = gridContainer.value
159 if (!container)
160 return
161
162 const rect = container.getBoundingClientRect()
163 const deltaY = clientY - resizeStartY.value
164 const proposed = isNotesOnBottom.value
165 ? resizeStartRowSize.value - deltaY
166 : resizeStartRowSize.value + deltaY
167 const maxByViewport = Math.round(rect.height * RESIZER_LIMITS.maxNotesRowHeightRatio)
168 notesRowSize.value = Math.min(clampNotesRowSize(proposed), Math.max(RESIZER_LIMITS.minNotesRowSize, maxByViewport))
169 }
170
171 function onNotesRowResizeStart(e: PointerEvent) {
172 if (!isNotesRowResizable.value)
173 return
174 if (e.button !== 0)
175 return
176 e.preventDefault()
177
178 // In layout 2, notesRowSize controls the top section (main slide) height
179 // In other layouts, it represents the notes area height
180 const currentHeight = presenterLayout.value === 2
181 ? main.value?.getBoundingClientRect().height
182 : noteSection.value?.getBoundingClientRect().height
183 resizeStartY.value = e.clientY
184 resizeStartRowSize.value = clampNotesRowSize(currentHeight ?? notesRowSize.value)
185 isResizingNotesRow.value = true
186 }
187
188 function updateBottomSectionHeight() {
189 const element = bottomSection.value
190 if (!element)
191 return
192 bottomSectionHeight.value = Math.round(element.getBoundingClientRect().height)
193 }
194
195 function stopResizing() {
196 isResizingNotes.value = false
197 isResizingNotesRow.value = false
198 }
199
200 function syncResizerLayoutState() {
201 updateBottomSectionHeight()
202 normalizeResizerState()
203 }
204
205 useEventListener(window, 'pointermove', (e) => {
206 if (isResizingNotes.value)
207 updateNotesWidthFromPointer(e.clientX)
208 if (isResizingNotesRow.value)
209 updateNotesRowSizeFromPointer(e.clientY)
210 })
211
212 useEventListener(window, 'pointerup', stopResizing)
213 useEventListener(window, 'pointercancel', stopResizing)
214
215 onMounted(() => {
216 syncResizerLayoutState()
217 })
218
219 useEventListener(window, 'resize', () => {
220 syncResizerLayoutState()
221 })
222
223 function normalizeResizerState() {
224 notesWidth.value = clampNotesWidth(notesWidth.value)
225 notesRowSize.value = clampNotesRowSize(notesRowSize.value)
226
227 const container = gridContainer.value
228 if (!container)
229 return
230
231 const rect = container.getBoundingClientRect()
232 const maxWidth = Math.round(rect.width * RESIZER_LIMITS.maxNotesWidthRatio)
233 const maxRowSize = Math.round(rect.height * RESIZER_LIMITS.maxNotesRowHeightRatio)
234
235 notesWidth.value = Math.min(notesWidth.value, Math.max(RESIZER_LIMITS.minNotesWidth, maxWidth))
236 notesRowSize.value = Math.min(notesRowSize.value, Math.max(RESIZER_LIMITS.minNotesRowSize, maxRowSize))
237 }
238
239 const SideEditor = shallowRef<any>()
240 if (__DEV__ && __SLIDEV_FEATURE_EDITOR__)
241 import('../internals/SideEditor.vue').then(v => SideEditor.value = v.default)
242
243 // sync presenter cursor
244 onMounted(() => {
245 const mouse = useMousePosInSlide()
246 const focus = useWindowFocus()
247
248 watchEffect(() => {
249 if (!mouse.value || !focus.value || isDrawing.value || !showPresenterCursor.value) {
250 sharedState.cursor = undefined
251 }
252 else {
253 sharedState.cursor = {
254 ...mouse.value,
255 style: cursorStyle.value,
256 }
257 }
258 })
259 })
260 </script>
261
262 <template>
263 <div class="bg-main h-full slidev-presenter grid grid-rows-[max-content_1fr] of-hidden">
264 <div>
265 <CurrentProgressBar />
266 <TimerBar />
267 </div>
268 <div
269 ref="gridContainer"
270 class="grid-container"
271 :class="`layout${presenterLayout}`"
272 :style="{
273 '--slidev-presenter-notes-width': `${notesWidth}px`,
274 '--slidev-presenter-notes-row-size': `${notesRowSize}px`,
275 '--slidev-presenter-bottom-height': `${bottomSectionHeight}px`,
276 }"
277 >
278 <!-- Unified vertical resizer for wide layout -->
279 <div
280 v-if="isNotesResizable && isNotesOnRight"
281 class="notes-vertical-resizer"
282 role="separator"
283 aria-orientation="vertical"
284 title="Resize notes panel"
285 @pointerdown="onNotesResizeStart"
286 />
287 <!-- Unified vertical resizer for layout 3 -->
288 <div
289 v-if="isNotesResizable && presenterLayout === 3"
290 class="notes-vertical-resizer-left"
291 role="separator"
292 aria-orientation="vertical"
293 title="Resize notes panel"
294 @pointerdown="onNotesResizeStart"
295 />
296 <div ref="main" class="relative grid-section main flex flex-col">
297 <div flex="~ gap-4 items-center" border="b main" p1>
298 <span op50 px2>Current</span>
299 <div flex-auto />
300 <SegmentControl
301 v-model="mainSlideMode"
302 :options="[
303 { label: 'Slides', value: 'slides' },
304 { label: 'Screen Mirror', value: 'mirror' },
305 ]"
306 />
307 </div>
308 <template v-if="mainSlideMode === 'mirror'">
309 <ScreenCaptureMirror />
310 </template>
311
312 <!-- We use v-show here to still infer the clicks context -->
313 <SlideContainer
314 v-show="mainSlideMode === 'slides'"
315 key="main"
316 class="p-2 lg:p-4 flex-auto"
317 is-main
318 @contextmenu="onContextMenu"
319 >
320 <SlidesShow render-context="presenter" />
321 <LaserPointer />
322 </SlideContainer>
323
324 <ClicksSlider
325 :key="currentSlideRoute?.no"
326 :clicks-context="getPrimaryClicks(currentSlideRoute)"
327 class="w-full pb2 px4 flex-none"
328 />
329 </div>
330 <div class="relative grid-section next flex flex-col p-2 lg:p-4">
331 <div
332 v-if="isNotesRowResizable && presenterLayout === 2"
333 class="notes-row-resizer top-[-6px]"
334 role="separator"
335 aria-orientation="horizontal"
336 title="Resize notes panel height"
337 @pointerdown="onNotesRowResizeStart"
338 />
339 <SlideContainer v-if="nextFrame && nextFrameClicksCtx" key="next">
340 <SlideWrapper
341 :key="nextFrame[0].no"
342 :clicks-context="nextFrameClicksCtx.context"
343 :route="nextFrame[0]"
344 render-context="previewNext"
345 />
346 </SlideContainer>
347 <div v-else class="h-full flex justify-center items-center">
348 <div class="text-gray-500">
349 End of the presentation
350 </div>
351 </div>
352 <div class="absolute left-0 top-0 bg-main border-b border-r border-main px2 py1 op50 text-sm">
353 Next
354 </div>
355 </div>
356 <div ref="noteSection" class="relative grid-section note overflow-hidden">
357 <div
358 v-if="isNotesResizable && !isNotesOnRight && presenterLayout !== 3"
359 class="notes-resizer right-[-6px]"
360 role="separator"
361 aria-orientation="vertical"
362 title="Resize notes panel"
363 @pointerdown="onNotesResizeStart"
364 />
365 <div
366 v-if="isNotesRowResizable && presenterLayout !== 2"
367 class="notes-row-resizer"
368 :class="isNotesOnBottom ? 'top-[-6px]' : 'bottom-[-6px]'"
369 role="separator"
370 aria-orientation="horizontal"
371 title="Resize notes panel height"
372 @pointerdown="onNotesRowResizeStart"
373 />
374
375 <SideEditor v-if="SideEditor && showEditor" class="h-full" />
376
377 <div v-else class="h-full grid grid-rows-[1fr_min-content]">
378 <NoteEditable
379 v-if="__DEV__"
380 :key="`edit-${currentSlideNo}`"
381 v-model:editing="notesEditing"
382 :no="currentSlideNo"
383 class="w-full max-w-full h-full overflow-auto p-2 lg:p-4"
384 :clicks-context="clicksContext"
385 :style="{ fontSize: `${presenterNotesFontSize}em` }"
386 />
387 <NoteStatic
388 v-else
389 :key="`static-${currentSlideNo}`"
390 :no="currentSlideNo"
391 class="w-full max-w-full h-full overflow-auto p-2 lg:p-4"
392 :style="{ fontSize: `${presenterNotesFontSize}em` }"
393 :clicks-context="clicksContext"
394 auto-scroll
395 />
396 <div border-t border-main />
397 <div class="py-1 px-2 text-sm transition" :class="inFocus ? '' : 'op25'">
398 <IconButton title="Increase font size" @click="increasePresenterFontSize">
399 <div class="i-carbon:zoom-in" />
400 </IconButton>
401 <IconButton title="Decrease font size" @click="decreasePresenterFontSize">
402 <div class="i-carbon:zoom-out" />
403 </IconButton>
404 <IconButton
405 v-if="__DEV__"
406 title="Edit Notes"
407 @click="notesEditing = !notesEditing"
408 >
409 <div class="i-carbon:edit" />
410 </IconButton>
411 </div>
412 </div>
413 </div>
414 <div ref="bottomSection" class="grid-section bottom flex">
415 <NavControls :persist="true" class="transition" :class="inFocus ? '' : 'op25'" />
416 <div flex-auto />
417 <TimerInlined />
418 </div>
419 <DrawingControls v-if="__SLIDEV_FEATURE_DRAWINGS__" />
420 </div>
421 </div>
422 <Goto />
423 <QuickOverview />
424 <ContextMenu />
425 </template>
426
427 <style scoped>
428 .slidev-presenter {
429 --slidev-controls-foreground: current;
430 }
431
432 .grid-container {
433 --slidev-presenter-notes-width: 360px;
434 --slidev-presenter-notes-row-size: 280px;
435 --uno: bg-gray/20 flex-1 of-hidden;
436 position: relative;
437 display: grid;
438 gap: 1px 1px;
439 }
440
441 .grid-container.layout1 {
442 grid-template-columns: var(--slidev-presenter-notes-width) minmax(0, 1fr);
443 grid-template-rows: minmax(0, 2fr) minmax(0, var(--slidev-presenter-notes-row-size)) min-content;
444 grid-template-areas:
445 'main main'
446 'note next'
447 'bottom bottom';
448 }
449
450 .grid-container.layout2 {
451 grid-template-columns: var(--slidev-presenter-notes-width) minmax(0, 1fr);
452 grid-template-rows: minmax(0, var(--slidev-presenter-notes-row-size)) minmax(0, 1fr) min-content;
453 grid-template-areas:
454 'note main'
455 'note next'
456 'bottom bottom';
457 }
458
459 @media (max-aspect-ratio: 3/5) {
460 .grid-container.layout1 {
461 grid-template-columns: 1fr;
462 grid-template-rows: 2fr 1fr 1fr min-content;
463 grid-template-areas:
464 'main'
465 'note'
466 'next'
467 'bottom';
468 }
469 }
470
471 @media (min-aspect-ratio: 1/1) {
472 .grid-container.layout1 {
473 grid-template-columns: minmax(0, 1fr) minmax(0, 1.1fr) var(--slidev-presenter-notes-width);
474 grid-template-rows: minmax(0, 1fr) minmax(0, var(--slidev-presenter-notes-row-size)) min-content;
475 grid-template-areas:
476 'main main next'
477 'main main note'
478 'bottom bottom bottom';
479 }
480 }
481
482 .grid-container.layout3 {
483 grid-template-columns: var(--slidev-presenter-notes-width) minmax(0, 1fr);
484 grid-template-rows: minmax(0, var(--slidev-presenter-notes-row-size)) minmax(0, 1fr) min-content;
485 grid-template-areas:
486 'note next'
487 'main next'
488 'bottom bottom';
489 }
490
491 .grid-section {
492 --uno: bg-main;
493 }
494 .grid-section.top {
495 grid-area: top;
496 }
497 .grid-section.main {
498 grid-area: main;
499 }
500 .grid-section.next {
501 grid-area: next;
502 }
503 .grid-section.note {
504 grid-area: note;
505 }
506 .grid-section.bottom {
507 grid-area: bottom;
508 }
509
510 .notes-resizer {
511 position: absolute;
512 top: 0;
513 width: 12px;
514 height: 100%;
515 cursor: col-resize;
516 z-index: 10;
517 touch-action: none;
518 }
519
520 .notes-resizer::before {
521 content: '';
522 position: absolute;
523 left: 50%;
524 top: 0;
525 width: 1px;
526 height: 100%;
527 background-color: currentColor;
528 opacity: 0.2;
529 transform: translateX(-50%);
530 }
531
532 .notes-row-resizer {
533 position: absolute;
534 left: 0;
535 width: 100%;
536 height: 12px;
537 cursor: row-resize;
538 z-index: 10;
539 touch-action: none;
540 }
541
542 .notes-row-resizer::before {
543 content: '';
544 position: absolute;
545 left: 0;
546 top: 50%;
547 width: 100%;
548 height: 1px;
549 background-color: currentColor;
550 opacity: 0.2;
551 transform: translateY(-50%);
552 }
553
554 .notes-vertical-resizer {
555 position: absolute;
556 right: var(--slidev-presenter-notes-width);
557 top: 0;
558 bottom: var(--slidev-presenter-bottom-height, 0px);
559 width: 12px;
560 cursor: col-resize;
561 z-index: 10;
562 touch-action: none;
563 transform: translateX(50%);
564 }
565
566 .notes-vertical-resizer::before {
567 content: '';
568 position: absolute;
569 left: 50%;
570 top: 0;
571 width: 1px;
572 height: 100%;
573 transform: translateX(-50%);
574 }
575
576 .notes-vertical-resizer-left {
577 position: absolute;
578 left: var(--slidev-presenter-notes-width);
579 top: 0;
580 bottom: var(--slidev-presenter-bottom-height, 0px);
581 width: 12px;
582 cursor: col-resize;
583 z-index: 10;
584 touch-action: none;
585 transform: translateX(-50%);
586 }
587
588 .notes-vertical-resizer-left::before {
589 content: '';
590 position: absolute;
591 left: 50%;
592 top: 0;
593 width: 1px;
594 height: 100%;
595 transform: translateX(-50%);
596 }
597 </style>
598
598 lines Plain Text