Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit dd775a4

Browse files
committed
feat(client): apply fragments
1 parent 5508ef2 commit dd775a4

File tree

8 files changed

+41
-44
lines changed

8 files changed

+41
-44
lines changed

packages/client/src/components/ContentView/Base.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { memo, useEffect, useRef } from 'react';
1+
import React, { memo, useEffect } from 'react';
22
import { useSlides, updateCurrentIndex } from '../../context/slides';
33
import { SlideCore } from '../SlideCore';
44

@@ -7,7 +7,6 @@ export const Base = memo(() => {
77
state: { currentIndex },
88
dispatch,
99
} = useSlides();
10-
const currentIndexRef = useRef(currentIndex);
1110

1211
useEffect(() => {
1312
// TODO: swiper should be gone to context
@@ -26,8 +25,10 @@ export const Base = memo(() => {
2625
useEffect(() => {
2726
if (window.innerWidth <= 768) {
2827
(async () => {
29-
// const { swipeEvent } = await import('../../utils/swipeEvent');
30-
// swipeEvent();
28+
const { swipeEvent } = await import('../../utils/swipeEvent');
29+
swipeEvent((operation) => {
30+
dispatch(updateCurrentIndex(operation));
31+
});
3132
})();
3233
}
3334

@@ -38,9 +39,5 @@ export const Base = memo(() => {
3839
};
3940
}, []);
4041

41-
useEffect(() => {
42-
currentIndexRef.current = currentIndex;
43-
}, [currentIndex]);
44-
4542
return <SlideCore />;
4643
});

packages/client/src/components/ContentView/Host.js

+18-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { memo, useState, useEffect } from 'react';
1+
import React, { memo, useState, useEffect, useRef } from 'react';
22
import Modal from 'react-modal';
33
import {
44
FaTimes,
@@ -10,8 +10,8 @@ import {
1010
FaMicrophoneAltSlash,
1111
} from 'react-icons/fa';
1212
import { MdZoomOutMap } from 'react-icons/md';
13-
import { useSlides, setMode } from '../../context/slides';
14-
import { PresenterProvider, usePresenter, updateCurrentIndex } from '../../context/presenter';
13+
import { useSlides, setMode, updateCurrentIndex, resetState } from '../../context/slides';
14+
import { PresenterProvider, usePresenter } from '../../context/presenter';
1515
import { Controller as PresentationController } from '../../presentationMode/Controller'; // common and host
1616
import { Canvas, emitCanvasEvent } from '../Canvas';
1717
import { Timer } from '../Timer';
@@ -33,20 +33,17 @@ const Iframe = ({ slideUrl, slideIndex }) => (
3333
let webrtc = null;
3434
let slideUrl = null;
3535
let presentationController = null;
36-
// let presentationApiId = null;
3736
let recordedTimeline = [];
3837
let recordedStartedTime = 0;
3938
let audioUrl = null;
4039

4140
const Host = () => {
4241
const {
43-
state: { slides },
42+
state: { slides, currentIndex },
4443
dispatch: dispatchSlides,
4544
} = useSlides();
46-
const {
47-
state: { currentIndex },
48-
dispatch: dispatchPresenter,
49-
} = usePresenter();
45+
const { dispatch: dispatchPresenter } = usePresenter();
46+
const currentIndexRef = useRef(currentIndex);
5047

5148
if (!presentationController) {
5249
const { origin, pathname } = new URL(window.location);
@@ -182,6 +179,8 @@ const Host = () => {
182179
};
183180

184181
useEffect(() => {
182+
dispatchSlides(resetState());
183+
185184
(async () => {
186185
try {
187186
if (!presentationController) {
@@ -194,17 +193,15 @@ const Host = () => {
194193
})();
195194

196195
const keyboardListener = ({ key }) => {
197-
const slideLength = slides.length;
198-
199196
if (key === 'ArrowLeft') {
200-
dispatchPresenter(updateCurrentIndex({ index: '-', slideLength }));
197+
dispatchSlides(updateCurrentIndex('-'));
198+
presentationController.changePage('-');
201199
} else if (key === 'ArrowRight') {
202-
dispatchPresenter(updateCurrentIndex({ index: '+', slideLength }));
203-
204-
// TODO: fix here
205-
// presentationController.changePage(3);
200+
dispatchSlides(updateCurrentIndex('+'));
201+
presentationController.changePage('+');
206202
}
207203
};
204+
208205
document.addEventListener('keydown', keyboardListener, false);
209206

210207
return () => {
@@ -218,6 +215,11 @@ const Host = () => {
218215
};
219216
}, []);
220217

218+
useEffect(() => {
219+
currentIndexRef.current = currentIndex;
220+
presentationController.changePage(currentIndex);
221+
}, [currentIndex]);
222+
221223
// prohibit below actions
222224
// usedAudio && status === 'start'
223225
// modal, reset

packages/client/src/components/ContentView/View.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ const View = memo(() => {
2626
window.onload = () => {
2727
presentationReceiver = new PresentationReceiver();
2828

29-
presentationReceiver.onChangePage((pageNum) => {
30-
dispatch(updateCurrentIndex(pageNum));
29+
presentationReceiver.onChangePage((operation) => {
30+
dispatch(updateCurrentIndex(operation));
3131
// stop capturing
3232
if (webrtc && currentVideoTag) {
3333
stopCapturing(currentVideoTag);

packages/client/src/components/SlideCore.js

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ export const SlideCore = () => {
5858
}
5959

6060
useEffect(() => {
61+
Prism.highlightAll();
62+
6163
if (slides.some(({ fusumaProps }) => !!fusumaProps.hasExecutableCode)) {
6264
createVMEnv();
6365
}

packages/client/src/context/presenter.js

-14
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,11 @@ const PresenterContext = createContext(initialState);
99

1010
const reducer = (state, action) => {
1111
switch (action.type) {
12-
case 'UPDATE_CURRENT_INDEX':
13-
return {
14-
...state,
15-
currentIndex: getSlideIndex(
16-
action.payload.index,
17-
action.payload.slideLength - 1,
18-
state.currentIndex
19-
),
20-
};
2112
default:
2213
return state;
2314
}
2415
};
2516

26-
export const updateCurrentIndex = (payload) => ({
27-
type: 'UPDATE_CURRENT_INDEX',
28-
payload,
29-
});
30-
3117
export const usePresenter = () => useContext(PresenterContext);
3218

3319
export const PresenterProvider = ({ children }) => {

packages/client/src/context/slides.js

+10
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ const reducer = (state, action) => {
2828
...state,
2929
}),
3030
};
31+
case 'RESET_STATE':
32+
return {
33+
...state,
34+
currentIndex: 0,
35+
currentFragmentSteps: 0,
36+
};
3137
case 'UPDATE_CURRENT_FRAGMENT_STEPS':
3238
return {
3339
...state,
@@ -59,6 +65,10 @@ export const updateCurrentFragmentSteps = (payload) => ({
5965
payload,
6066
});
6167

68+
export const resetState = () => ({
69+
type: 'RESET_STATE',
70+
});
71+
6272
export const useSlides = () => useContext(SlidesContext);
6373

6474
export const SlidesProvider = ({ children }) => {

packages/client/src/utils/getSlideIndex.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export function getSlideIndex({ next, slides, currentIndex, timeline, currentFragmentSteps }) {
22
let nextIndex = next;
3-
let nextCurrentFragmentSteps;
3+
let nextCurrentFragmentSteps = 0;
44

55
if (next === '+') {
66
nextIndex = Math.min(currentIndex + 1, slides.length - 1);

packages/client/src/utils/swipeEvent.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// https://stackoverflow.com/questions/2264072/detect-a-finger-swipe-through-javascript-on-the-iphone-and-android
22

3-
export function swipeEvent(goTo) {
3+
export function swipeEvent(cb) {
44
let xDown = null;
55
let yDown = null;
66

@@ -28,10 +28,10 @@ export function swipeEvent(goTo) {
2828
if (Math.abs(xDiff) > Math.abs(yDiff)) {
2929
if (xDiff > 0) {
3030
// right
31-
goTo('+');
31+
cb('+');
3232
} else {
3333
// left
34-
goTo('-');
34+
cb('-');
3535
}
3636
}
3737
xDown = null;

0 commit comments

Comments
 (0)