1
- /**
2
- * Host for Presentation mode
3
- */
4
-
5
1
import React , { memo , useState , useEffect } from 'react' ;
6
2
import Modal from 'react-modal' ;
7
3
import {
8
4
FaTimes ,
9
5
FaHistory ,
10
6
FaCaretDown ,
11
- FaCaretRight ,
7
+ // FaCaretRight,
12
8
FaCaretUp ,
13
9
FaMicrophoneAlt ,
14
10
FaMicrophoneAltSlash ,
15
11
} from 'react-icons/fa' ;
16
12
import { MdZoomOutMap } from 'react-icons/md' ;
13
+ import { useSlides , setMode } from '../../context/slides' ;
14
+ import { PresenterProvider , usePresenter , updateCurrentIndex } from '../../context/presenter' ;
17
15
import { Controller as PresentationController } from '../../presentationMode/Controller' ; // common and host
18
16
import { Canvas , emitCanvasEvent } from '../Canvas' ;
19
17
import { Timer } from '../Timer' ;
@@ -40,11 +38,21 @@ let recordedTimeline = [];
40
38
let recordedStartedTime = 0 ;
41
39
let audioUrl = null ;
42
40
43
- const Host = memo ( ( { slides, currentIndex, terminate, onChangeSlideIndex } ) => {
41
+ const Host = ( ) => {
42
+ const {
43
+ state : { slides } ,
44
+ dispatch : dispatchSlides ,
45
+ } = useSlides ( ) ;
46
+ const {
47
+ state : { currentIndex } ,
48
+ dispatch : dispatchPresenter ,
49
+ } = usePresenter ( ) ;
50
+
44
51
if ( ! presentationController ) {
45
52
const { origin, pathname } = new URL ( window . location ) ;
46
53
47
54
slideUrl = `${ origin } ${ pathname } ?sidebar=false&reference=false#slide-` ;
55
+
48
56
presentationController = new PresentationController ( ) ;
49
57
}
50
58
@@ -54,39 +62,38 @@ const Host = memo(({ slides, currentIndex, terminate, onChangeSlideIndex }) => {
54
62
const [ isEmptyRecordedTimeline , updateEmptyRecordedTimelineStatus ] = useState ( true ) ;
55
63
const [ isOpenZoomSlide , updateOpenZoomSlideStatus ] = useState ( false ) ;
56
64
57
- const _terminate = ( ) => {
65
+ const terminate = ( ) => {
58
66
try {
59
- terminate ( ) ;
60
-
61
67
if ( presentationController ) {
62
68
presentationController . terminate ( ) ;
63
69
presentationController = null ;
64
70
}
65
71
} catch ( e ) {
66
72
console . error ( e ) ;
73
+ } finally {
74
+ dispatchSlides ( setMode ( 'common' ) ) ;
67
75
}
68
76
} ;
69
77
70
- const changeCurrentSlide = ( num ) => {
71
- if ( status === 'start' ) {
72
- const time = new Date ( ) . getTime ( ) - recordedStartedTime ;
73
- const prevItem = recordedTimeline . slice ( - 1 ) [ 0 ] ;
74
-
75
- recordedTimeline . push ( {
76
- slideNum : num + 1 ,
77
- time,
78
- timeStr : `${ formatTime ( time ) } (+${ formatTime ( time - prevItem . time ) } )` ,
79
- event : 'changed' ,
80
- title : `Moved to the ${ num + 1 } slide from the ${ num } slide.` ,
81
- Slide : slides [ num ] . slide ,
82
- color : '#3498db' ,
83
- Icon : < FaCaretRight size = "22" /> ,
84
- } ) ;
85
- }
86
-
87
- onChangeSlideIndex ( num ) ;
88
- presentationController . changePage ( num ) ;
89
- } ;
78
+ // const changeCurrentSlide = (num) => {
79
+ // if (status === 'start') {
80
+ // const time = new Date().getTime() - recordedStartedTime;
81
+ // const prevItem = recordedTimeline.slice(-1)[0];
82
+
83
+ // recordedTimeline.push({
84
+ // slideNum: num + 1,
85
+ // time,
86
+ // timeStr: `${formatTime(time)} (+${formatTime(time - prevItem.time)})`,
87
+ // event: 'changed',
88
+ // title: `Moved to the ${num + 1} slide from the ${num} slide.`,
89
+ // Slide: slides[num].slide,
90
+ // color: '#3498db',
91
+ // Icon: <FaCaretRight size="22" />,
92
+ // });
93
+ // }
94
+
95
+ // presentationController.changePage(num);
96
+ // };
90
97
91
98
const start = ( ) => {
92
99
if ( recordedTimeline . length === 0 ) {
@@ -143,14 +150,6 @@ const Host = memo(({ slides, currentIndex, terminate, onChangeSlideIndex }) => {
143
150
updateEmptyRecordedTimelineStatus ( true ) ;
144
151
} ;
145
152
146
- const openTimeline = ( ) => {
147
- updateOpenTimelineStatus ( true ) ;
148
- } ;
149
-
150
- const closeTimeline = ( ) => {
151
- updateOpenTimelineStatus ( false ) ;
152
- } ;
153
-
154
153
const setupRecording = ( ) => {
155
154
if ( ! webrtc ) {
156
155
try {
@@ -183,7 +182,7 @@ const Host = memo(({ slides, currentIndex, terminate, onChangeSlideIndex }) => {
183
182
} ;
184
183
185
184
useEffect ( ( ) => {
186
- async function openView ( ) {
185
+ ( async ( ) => {
187
186
try {
188
187
if ( ! presentationController ) {
189
188
throw new Error ( 'Not found PresenterController.' ) ;
@@ -192,30 +191,32 @@ const Host = memo(({ slides, currentIndex, terminate, onChangeSlideIndex }) => {
192
191
} catch ( e ) {
193
192
console . error ( e ) ;
194
193
}
195
- }
194
+ } ) ( ) ;
196
195
197
- openView ( ) ;
196
+ const keyboardListener = ( { key } ) => {
197
+ const slideLength = slides . length ;
198
198
199
- return ( ) => {
200
- document . onkeyup = null ;
199
+ if ( key === 'ArrowLeft' ) {
200
+ dispatchPresenter ( updateCurrentIndex ( { index : '-' , slideLength } ) ) ;
201
+ } else if ( key === 'ArrowRight' ) {
202
+ dispatchPresenter ( updateCurrentIndex ( { index : '+' , slideLength } ) ) ;
203
+
204
+ // TODO: fix here
205
+ // presentationController.changePage(3);
206
+ }
207
+ } ;
208
+ document . addEventListener ( 'keydown' , keyboardListener , false ) ;
201
209
210
+ return ( ) => {
202
211
if ( presentationController ) {
203
- _terminate ( ) ;
212
+ terminate ( ) ;
204
213
}
205
214
206
215
disposeRecording ( ) ;
207
- } ;
208
- } , [ ] ) ;
209
216
210
- useEffect ( ( ) => {
211
- document . onkeyup = ( e ) => {
212
- if ( e . key === 'ArrowLeft' ) {
213
- changeCurrentSlide ( Math . max ( 0 , currentIndex - 1 ) ) ;
214
- } else if ( e . key === 'ArrowRight' ) {
215
- changeCurrentSlide ( Math . min ( slides . length - 1 , currentIndex + 1 ) ) ;
216
- }
217
+ document . removeEventListener ( 'keydown' , keyboardListener ) ;
217
218
} ;
218
- } ) ;
219
+ } , [ ] ) ;
219
220
220
221
// prohibit below actions
221
222
// usedAudio && status === 'start'
@@ -226,7 +227,7 @@ const Host = memo(({ slides, currentIndex, terminate, onChangeSlideIndex }) => {
226
227
// mic
227
228
return (
228
229
< div className = "host-container" >
229
- < Modal isOpen = { isOpenTimeline } onRequestClose = { closeTimeline } >
230
+ < Modal isOpen = { isOpenTimeline } onRequestClose = { ( ) => updateOpenTimelineStatus ( false ) } >
230
231
< Timeline items = { recordedTimeline } url = { audioUrl } />
231
232
</ Modal >
232
233
< div className = "host-left-box" >
@@ -260,7 +261,7 @@ const Host = memo(({ slides, currentIndex, terminate, onChangeSlideIndex }) => {
260
261
</ div >
261
262
</ div >
262
263
< div className = "host-bottom-box" >
263
- < FaTimes onClick = { _terminate } className = "terminate-button" />
264
+ < FaTimes onClick = { terminate } className = "terminate-button" />
264
265
< div className = "host-bottom-box-info" >
265
266
< Timer
266
267
start = { start }
@@ -275,7 +276,7 @@ const Host = memo(({ slides, currentIndex, terminate, onChangeSlideIndex }) => {
275
276
{ `${ currentIndex + 1 } ` . padStart ( 2 , '0' ) } / { `${ slides . length } ` . padStart ( 2 , '0' ) }
276
277
</ span >
277
278
< FaHistory
278
- onClick = { openTimeline }
279
+ onClick = { ( ) => updateOpenTimelineStatus ( true ) }
279
280
size = { 18 }
280
281
className = {
281
282
( status === 'start' && usedAudio ) || isEmptyRecordedTimeline ? 'disabled' : undefined
@@ -299,6 +300,12 @@ const Host = memo(({ slides, currentIndex, terminate, onChangeSlideIndex }) => {
299
300
</ div >
300
301
</ div >
301
302
) ;
302
- } ) ;
303
+ } ;
304
+
305
+ const Presenter = memo ( ( ) => (
306
+ < PresenterProvider >
307
+ < Host />
308
+ </ PresenterProvider >
309
+ ) ) ;
303
310
304
- export default Host ;
311
+ export default Presenter ;
0 commit comments