YuSaZh commited on
Commit
694a400
·
1 Parent(s): fd72c24

Improve iOS PWA lightbox interactions

Browse files
web/src/app/layout.tsx CHANGED
@@ -6,6 +6,11 @@ import { TopNav } from "@/components/top-nav";
6
  export const metadata: Metadata = {
7
  title: "ChatGPT 号池管理",
8
  description: "ChatGPT account pool management dashboard",
 
 
 
 
 
9
  };
10
 
11
  export const viewport: Viewport = {
@@ -13,6 +18,7 @@ export const viewport: Viewport = {
13
  initialScale: 1,
14
  maximumScale: 1,
15
  userScalable: false,
 
16
  themeColor: "#f0ebe3",
17
  };
18
 
 
6
  export const metadata: Metadata = {
7
  title: "ChatGPT 号池管理",
8
  description: "ChatGPT account pool management dashboard",
9
+ appleWebApp: {
10
+ capable: true,
11
+ statusBarStyle: "black-translucent",
12
+ title: "chatgpt2api",
13
+ },
14
  };
15
 
16
  export const viewport: Viewport = {
 
18
  initialScale: 1,
19
  maximumScale: 1,
20
  userScalable: false,
21
+ viewportFit: "cover",
22
  themeColor: "#f0ebe3",
23
  };
24
 
web/src/components/image-lightbox.tsx CHANGED
@@ -42,6 +42,8 @@ type TouchGesture =
42
  | {
43
  type: "pinch";
44
  startDistance: number;
 
 
45
  startTransform: ImageTransform;
46
  };
47
 
@@ -58,6 +60,15 @@ function getTouchDistance(touches: TouchList) {
58
  return Math.hypot(first.clientX - second.clientX, first.clientY - second.clientY);
59
  }
60
 
 
 
 
 
 
 
 
 
 
61
  function normalizeTransform(transform: ImageTransform) {
62
  if (transform.scale <= minScale) {
63
  return { scale: minScale, x: 0, y: 0 };
@@ -81,15 +92,53 @@ export function ImageLightbox({
81
  }: ImageLightboxProps) {
82
  const gestureRef = useRef<TouchGesture | null>(null);
83
  const lastTapRef = useRef(0);
 
 
84
  const [transform, setTransform] = useState<ImageTransform>({ scale: 1, x: 0, y: 0 });
 
85
  const current = images[currentIndex];
86
  const hasPrev = currentIndex > 0;
87
  const hasNext = currentIndex < images.length - 1;
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  const resetTransform = useCallback(() => {
 
90
  setTransform({ scale: 1, x: 0, y: 0 });
 
91
  gestureRef.current = null;
92
- }, []);
93
 
94
  const goPrev = useCallback(() => {
95
  if (hasPrev) onIndexChange(currentIndex - 1);
@@ -103,6 +152,15 @@ export function ImageLightbox({
103
  resetTransform();
104
  }, [current?.id, open, resetTransform]);
105
 
 
 
 
 
 
 
 
 
 
106
  useEffect(() => {
107
  if (!open) return;
108
 
@@ -138,9 +196,19 @@ export function ImageLightbox({
138
  (event: React.TouchEvent<HTMLDivElement>) => {
139
  if (event.touches.length === 2) {
140
  event.preventDefault();
 
 
 
 
 
 
 
 
141
  gestureRef.current = {
142
  type: "pinch",
143
- startDistance: getTouchDistance(event.touches),
 
 
144
  startTransform: transform,
145
  };
146
  return;
@@ -152,58 +220,81 @@ export function ImageLightbox({
152
  }
153
 
154
  const touch = event.touches[0];
155
- gestureRef.current =
156
- transform.scale > minScale
157
- ? {
158
- type: "pan",
159
- startX: touch.clientX,
160
- startY: touch.clientY,
161
- startTransform: transform,
162
- }
163
- : {
164
- type: "swipe",
165
- startX: touch.clientX,
166
- startY: touch.clientY,
167
- };
 
 
 
168
  },
169
- [transform],
170
  );
171
 
172
- const handleTouchMove = useCallback((event: React.TouchEvent<HTMLDivElement>) => {
173
- const gesture = gestureRef.current;
174
- if (!gesture) return;
175
-
176
- if (gesture.type === "pinch" && event.touches.length === 2) {
177
- event.preventDefault();
178
- const nextScale = clamp(
179
- (getTouchDistance(event.touches) / gesture.startDistance) * gesture.startTransform.scale,
180
- minScale,
181
- maxScale,
182
- );
183
- setTransform(normalizeTransform({ ...gesture.startTransform, scale: nextScale }));
184
- return;
185
- }
186
 
187
- if (gesture.type === "pan" && event.touches.length === 1) {
188
- event.preventDefault();
189
- const touch = event.touches[0];
190
- setTransform(
191
- normalizeTransform({
192
- scale: gesture.startTransform.scale,
193
- x: gesture.startTransform.x + touch.clientX - gesture.startX,
194
- y: gesture.startTransform.y + touch.clientY - gesture.startY,
195
- }),
196
- );
197
- return;
198
- }
 
 
 
 
 
 
 
 
 
 
 
 
199
 
200
- if (event.touches.length !== 1) {
201
- gestureRef.current = null;
202
- }
203
- }, []);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
 
205
  const handleTouchEnd = useCallback(
206
  (event: React.TouchEvent<HTMLDivElement>) => {
 
 
 
207
  const gesture = gestureRef.current;
208
  gestureRef.current = null;
209
  if (!gesture) return;
@@ -235,9 +326,15 @@ export function ImageLightbox({
235
  goNext();
236
  }
237
  },
238
- [goPrev, goNext, toggleZoom],
239
  );
240
 
 
 
 
 
 
 
241
  if (!current) return null;
242
 
243
  return (
@@ -294,12 +391,14 @@ export function ImageLightbox({
294
  onTouchStart={handleTouchStart}
295
  onTouchMove={handleTouchMove}
296
  onTouchEnd={handleTouchEnd}
 
297
  >
298
  <img
299
  src={current.src}
300
  alt=""
301
  className={cn(
302
- "max-h-[90vh] max-w-[90vw] rounded-lg object-contain transition-transform duration-100",
 
303
  transform.scale > minScale ? "cursor-grab active:cursor-grabbing" : "cursor-zoom-in",
304
  )}
305
  style={{
 
42
  | {
43
  type: "pinch";
44
  startDistance: number;
45
+ startCenterX: number;
46
+ startCenterY: number;
47
  startTransform: ImageTransform;
48
  };
49
 
 
60
  return Math.hypot(first.clientX - second.clientX, first.clientY - second.clientY);
61
  }
62
 
63
+ function getTouchCenter(touches: TouchList) {
64
+ const first = touches[0];
65
+ const second = touches[1];
66
+ return {
67
+ x: (first.clientX + second.clientX) / 2,
68
+ y: (first.clientY + second.clientY) / 2,
69
+ };
70
+ }
71
+
72
  function normalizeTransform(transform: ImageTransform) {
73
  if (transform.scale <= minScale) {
74
  return { scale: minScale, x: 0, y: 0 };
 
92
  }: ImageLightboxProps) {
93
  const gestureRef = useRef<TouchGesture | null>(null);
94
  const lastTapRef = useRef(0);
95
+ const pendingTransformRef = useRef<ImageTransform | null>(null);
96
+ const rafRef = useRef<number | null>(null);
97
  const [transform, setTransform] = useState<ImageTransform>({ scale: 1, x: 0, y: 0 });
98
+ const [isGesturing, setIsGesturing] = useState(false);
99
  const current = images[currentIndex];
100
  const hasPrev = currentIndex > 0;
101
  const hasNext = currentIndex < images.length - 1;
102
 
103
+ const cancelScheduledTransform = useCallback(() => {
104
+ if (rafRef.current != null) {
105
+ cancelAnimationFrame(rafRef.current);
106
+ rafRef.current = null;
107
+ }
108
+ pendingTransformRef.current = null;
109
+ }, []);
110
+
111
+ const scheduleTransform = useCallback((next: ImageTransform) => {
112
+ pendingTransformRef.current = next;
113
+ if (rafRef.current != null) return;
114
+ rafRef.current = requestAnimationFrame(() => {
115
+ rafRef.current = null;
116
+ const pending = pendingTransformRef.current;
117
+ pendingTransformRef.current = null;
118
+ if (pending) {
119
+ setTransform(pending);
120
+ }
121
+ });
122
+ }, []);
123
+
124
+ const flushScheduledTransform = useCallback(() => {
125
+ if (rafRef.current != null) {
126
+ cancelAnimationFrame(rafRef.current);
127
+ rafRef.current = null;
128
+ }
129
+ const pending = pendingTransformRef.current;
130
+ pendingTransformRef.current = null;
131
+ if (pending) {
132
+ setTransform(pending);
133
+ }
134
+ }, []);
135
+
136
  const resetTransform = useCallback(() => {
137
+ cancelScheduledTransform();
138
  setTransform({ scale: 1, x: 0, y: 0 });
139
+ setIsGesturing(false);
140
  gestureRef.current = null;
141
+ }, [cancelScheduledTransform]);
142
 
143
  const goPrev = useCallback(() => {
144
  if (hasPrev) onIndexChange(currentIndex - 1);
 
152
  resetTransform();
153
  }, [current?.id, open, resetTransform]);
154
 
155
+ useEffect(() => {
156
+ return () => {
157
+ if (rafRef.current != null) {
158
+ cancelAnimationFrame(rafRef.current);
159
+ rafRef.current = null;
160
+ }
161
+ };
162
+ }, []);
163
+
164
  useEffect(() => {
165
  if (!open) return;
166
 
 
196
  (event: React.TouchEvent<HTMLDivElement>) => {
197
  if (event.touches.length === 2) {
198
  event.preventDefault();
199
+ const startDistance = getTouchDistance(event.touches);
200
+ if (startDistance < 1) {
201
+ gestureRef.current = null;
202
+ return;
203
+ }
204
+ const center = getTouchCenter(event.touches);
205
+ cancelScheduledTransform();
206
+ setIsGesturing(true);
207
  gestureRef.current = {
208
  type: "pinch",
209
+ startDistance,
210
+ startCenterX: center.x,
211
+ startCenterY: center.y,
212
  startTransform: transform,
213
  };
214
  return;
 
220
  }
221
 
222
  const touch = event.touches[0];
223
+ if (transform.scale > minScale) {
224
+ cancelScheduledTransform();
225
+ setIsGesturing(true);
226
+ gestureRef.current = {
227
+ type: "pan",
228
+ startX: touch.clientX,
229
+ startY: touch.clientY,
230
+ startTransform: transform,
231
+ };
232
+ } else {
233
+ gestureRef.current = {
234
+ type: "swipe",
235
+ startX: touch.clientX,
236
+ startY: touch.clientY,
237
+ };
238
+ }
239
  },
240
+ [transform, cancelScheduledTransform],
241
  );
242
 
243
+ const handleTouchMove = useCallback(
244
+ (event: React.TouchEvent<HTMLDivElement>) => {
245
+ const gesture = gestureRef.current;
246
+ if (!gesture) return;
 
 
 
 
 
 
 
 
 
 
247
 
248
+ if (gesture.type === "pinch" && event.touches.length === 2) {
249
+ event.preventDefault();
250
+ const targetScale = clamp(
251
+ (getTouchDistance(event.touches) / gesture.startDistance) * gesture.startTransform.scale,
252
+ minScale,
253
+ maxScale,
254
+ );
255
+ const effectiveRatio = targetScale / gesture.startTransform.scale;
256
+ const center = getTouchCenter(event.touches);
257
+ const viewportCenterX = window.innerWidth / 2;
258
+ const viewportCenterY = window.innerHeight / 2;
259
+ const nextX =
260
+ center.x -
261
+ viewportCenterX -
262
+ (gesture.startCenterX - viewportCenterX - gesture.startTransform.x) * effectiveRatio;
263
+ const nextY =
264
+ center.y -
265
+ viewportCenterY -
266
+ (gesture.startCenterY - viewportCenterY - gesture.startTransform.y) * effectiveRatio;
267
+ scheduleTransform(
268
+ normalizeTransform({ scale: targetScale, x: nextX, y: nextY }),
269
+ );
270
+ return;
271
+ }
272
 
273
+ if (gesture.type === "pan" && event.touches.length === 1) {
274
+ event.preventDefault();
275
+ const touch = event.touches[0];
276
+ scheduleTransform(
277
+ normalizeTransform({
278
+ scale: gesture.startTransform.scale,
279
+ x: gesture.startTransform.x + touch.clientX - gesture.startX,
280
+ y: gesture.startTransform.y + touch.clientY - gesture.startY,
281
+ }),
282
+ );
283
+ return;
284
+ }
285
+
286
+ if (event.touches.length !== 1) {
287
+ gestureRef.current = null;
288
+ }
289
+ },
290
+ [scheduleTransform],
291
+ );
292
 
293
  const handleTouchEnd = useCallback(
294
  (event: React.TouchEvent<HTMLDivElement>) => {
295
+ flushScheduledTransform();
296
+ setIsGesturing(false);
297
+
298
  const gesture = gestureRef.current;
299
  gestureRef.current = null;
300
  if (!gesture) return;
 
326
  goNext();
327
  }
328
  },
329
+ [goPrev, goNext, toggleZoom, flushScheduledTransform],
330
  );
331
 
332
+ const handleTouchCancel = useCallback(() => {
333
+ cancelScheduledTransform();
334
+ setIsGesturing(false);
335
+ gestureRef.current = null;
336
+ }, [cancelScheduledTransform]);
337
+
338
  if (!current) return null;
339
 
340
  return (
 
391
  onTouchStart={handleTouchStart}
392
  onTouchMove={handleTouchMove}
393
  onTouchEnd={handleTouchEnd}
394
+ onTouchCancel={handleTouchCancel}
395
  >
396
  <img
397
  src={current.src}
398
  alt=""
399
  className={cn(
400
+ "max-h-[90vh] max-w-[90vw] rounded-lg object-contain will-change-transform",
401
+ isGesturing ? "" : "transition-transform duration-150 ease-out",
402
  transform.scale > minScale ? "cursor-grab active:cursor-grabbing" : "cursor-zoom-in",
403
  )}
404
  style={{