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

Commit c7386fb

Browse files
committed
fix(client): add swipe event for mobile
1 parent 75962f5 commit c7386fb

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

packages/client/src/components/AppContainer.js

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import React, { useState } from 'react';
1+
import React, { useState, useEffect } from 'react';
22
import { MdMenu } from 'react-icons/md';
33
import { useCurrentIndex } from '../hooks/useCurrentIndex';
44
import { useMode } from '../hooks/useMode';
55
import { useSlidesProps } from '../hooks/useSlides';
66
import { useContentComponent } from '../hooks/useContentComponent';
77
import { useSidebarComponent } from '../hooks/useSidebarComponent';
88
import { useCommentsListComponent } from '../hooks/useCommentsListComponent';
9+
import { swipeEvent } from '../utils/swipeEvent';
910

1011
const slideWrapperClassName = '.swiper-container';
1112

@@ -19,15 +20,32 @@ export const AppContainer = ({ slides: originalSlides, hash }) => {
1920
const CommentsListComponent = useCommentsListComponent(mode);
2021

2122
const goTo = (num) => {
22-
document.querySelector(slideWrapperClassName)?.swiper?.slideTo(num);
23-
setCurrentIndex(num);
23+
let nextIndex = num;
24+
const { swiper } = document.querySelector(slideWrapperClassName);
25+
const { realIndex } = swiper;
26+
27+
if (num === '+') {
28+
nextIndex = Math.min(realIndex + 1, slides.length);
29+
} else if (num === '-') {
30+
nextIndex = Math.max(realIndex - 1, 0);
31+
}
32+
33+
swiper?.slideTo(nextIndex);
34+
setCurrentIndex(nextIndex);
2435
};
2536

2637
const runPresentationMode = (type) => {
2738
updateOpenSidebarStatus(false);
2839
setMode(type === 'start' ? 'host' : 'common');
2940
};
3041

42+
useEffect(() => {
43+
// for mobiles and tablets
44+
if (window.innerWidth <= 768) {
45+
swipeEvent(goTo);
46+
}
47+
}, []);
48+
3149
return (
3250
<>
3351
{SidebarComponent && (

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ export const Base = memo(
1010

1111
if (import.meta.webpackHot) {
1212
useEffect(() => {
13+
if (process.env.CHART) {
14+
mermaid?.reload();
15+
}
16+
1317
(async () => {
1418
const { Prism } = await import('../../setup/prism');
1519

16-
if (process.env.CHART) {
17-
mermaid?.reload();
18-
}
1920
Prism.highlightAll();
2021
})();
2122
}, [hash]);

packages/client/src/components/SlideCore.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const SlideCore = ({ slides, onChangeSlideIndex }) => (
4545
direction={process.env.UI.VERTICAL === 'true' ? 'vertical' : 'horizontal'}
4646
loop={/*TODO: respect to params to generate pdf */ process.env.LOOP}
4747
speed={350}
48-
allowTouchMove={/* TODO: only for mobile */ false}
48+
allowTouchMove={false}
4949
slidesPerView={1}
5050
keyboard={{ enabled: true }}
5151
hashNavigation={{
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// https://stackoverflow.com/questions/2264072/detect-a-finger-swipe-through-javascript-on-the-iphone-and-android
2+
3+
export function swipeEvent(goTo) {
4+
let xDown = null;
5+
let yDown = null;
6+
7+
document.addEventListener(
8+
'touchstart',
9+
(e) => {
10+
const { clientX, clientY } = e.touches[0];
11+
12+
xDown = clientX;
13+
yDown = clientY;
14+
},
15+
false
16+
);
17+
document.addEventListener(
18+
'touchmove',
19+
(e) => {
20+
if (!xDown || !yDown) {
21+
return;
22+
}
23+
24+
const firstTouch = e.touches[0];
25+
let xDiff = xDown - firstTouch.clientX;
26+
let yDiff = yDown - firstTouch.clientY;
27+
28+
if (Math.abs(xDiff) > Math.abs(yDiff)) {
29+
if (xDiff > 0) {
30+
// right
31+
goTo('+');
32+
} else {
33+
// left
34+
goTo('-');
35+
}
36+
}
37+
xDown = null;
38+
yDown = null;
39+
},
40+
false
41+
);
42+
}

0 commit comments

Comments
 (0)