Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

feat: support swipe to switch img on touchscreen (#970)

Wujiao233b8ab43aa2505
diff --git a/web/src/components/PreviewImageDialog.tsx b/web/src/components/PreviewImageDialog.tsx
index e60f87961..e045dd063 100644
--- a/web/src/components/PreviewImageDialog.tsx
+++ b/web/src/components/PreviewImageDialog.tsx
@@ -30,6 +30,8 @@ const defaultState: State = {
const PreviewImageDialog: React.FC<Props> = ({ destroy, imgUrls, initialIndex }: Props) => {
const [currentIndex, setCurrentIndex] = useState(initialIndex);
const [state, setState] = useState<State>(defaultState);
+ let startX = -1;
+ let endX = -1;
const handleCloseBtnClick = () => {
destroy();
@@ -42,21 +44,63 @@ const PreviewImageDialog: React.FC<Props> = ({ destroy, imgUrls, initialIndex }:
a.click();
};
+ const handleTouchStart = (event: React.TouchEvent) => {
+ if (event.touches.length > 1) {
+ // two or more fingers, ignore
+ return;
+ }
+ startX = event.touches[0].clientX;
+ };
+
+ const handleTouchMove = (event: React.TouchEvent) => {
+ if (event.touches.length > 1) {
+ // two or more fingers, ignore
+ return;
+ }
+ endX = event.touches[0].clientX;
+ };
+
+ const handleTouchEnd = (event: React.TouchEvent) => {
+ if (event.touches.length > 1) {
+ // two or more fingers, ignore
+ return;
+ }
+ if (startX > -1 && endX > -1) {
+ const distance = startX - endX;
+ if (distance > 50) {
+ showNextImg();
+ } else if (distance < -50) {
+ showPrevImg();
+ }
+ }
+
+ endX = -1;
+ startX = -1;
+ };
+
+ const showPrevImg = () => {
+ if (currentIndex > 0) {
+ setState(defaultState);
+ setCurrentIndex(currentIndex - 1);
+ } else {
+ destroy();
+ }
+ };
+
+ const showNextImg = () => {
+ if (currentIndex < imgUrls.length - 1) {
+ setState(defaultState);
+ setCurrentIndex(currentIndex + 1);
+ } else {
+ destroy();
+ }
+ };
+
const handleImgContainerClick = (event: React.MouseEvent) => {
if (event.clientX < window.innerWidth / 2) {
- if (currentIndex > 0) {
- setState(defaultState);
- setCurrentIndex(currentIndex - 1);
- } else {
- destroy();
- }
+ showPrevImg();
} else {
- if (currentIndex < imgUrls.length - 1) {
- setState(defaultState);
- setCurrentIndex(currentIndex + 1);
- } else {
- destroy();
- }
+ showNextImg();
}
};
@@ -111,6 +155,9 @@ const PreviewImageDialog: React.FC<Props> = ({ destroy, imgUrls, initialIndex }:
<div className="img-container" onClick={handleImgContainerClick}>
<img
onClick={(e) => e.stopPropagation()}
+ onTouchStart={handleTouchStart}
+ onTouchMove={handleTouchMove}
+ onTouchEnd={handleTouchEnd}
src={imgUrls[currentIndex]}
onWheel={handleImgContainerScroll}
style={getImageComputedStyle()}