fix mobile bug

This commit is contained in:
Gabe Yuan
2023-08-07 14:07:50 +08:00
parent c61b9e3910
commit 32f338137c
2 changed files with 25 additions and 8 deletions

View File

@@ -17,3 +17,4 @@ export const client = process.env.REACT_APP_CLIENT;
export const isExt = CLIENT_EXTS.includes(client); export const isExt = CLIENT_EXTS.includes(client);
export const isGm = client === CLIENT_USERSCRIPT; export const isGm = client === CLIENT_USERSCRIPT;
export const isWeb = client === CLIENT_WEB; export const isWeb = client === CLIENT_WEB;
export const isMobile = "ontouchstart" in document.documentElement;

View File

@@ -1,5 +1,6 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { limitNumber } from "../../libs/utils"; import { limitNumber } from "../../libs/utils";
import { isMobile } from "../../libs/browser";
export default function Draggable(props) { export default function Draggable(props) {
const [origin, setOrigin] = useState(null); const [origin, setOrigin] = useState(null);
@@ -9,21 +10,23 @@ export default function Draggable(props) {
}); });
const handlePointerDown = (e) => { const handlePointerDown = (e) => {
e.target.setPointerCapture(e.pointerId); !isMobile && e.target.setPointerCapture(e.pointerId);
props?.onStart(); props?.onStart();
const { clientX, clientY } = isMobile ? e.targetTouches[0] : e;
setOrigin({ setOrigin({
x: position.x, x: position.x,
y: position.y, y: position.y,
px: e.clientX, px: clientX,
py: e.clientY, py: clientY,
}); });
}; };
const handlePointerMove = (e) => { const handlePointerMove = (e) => {
props?.onMove(); props?.onMove();
const { clientX, clientY } = isMobile ? e.targetTouches[0] : e;
if (origin) { if (origin) {
const dx = e.clientX - origin.px; const dx = clientX - origin.px;
const dy = e.clientY - origin.py; const dy = clientY - origin.py;
let x = origin.x + dx; let x = origin.x + dx;
let y = origin.y + dy; let y = origin.y + dy;
const { w, h } = props.windowSize; const { w, h } = props.windowSize;
@@ -41,6 +44,18 @@ export default function Draggable(props) {
e.stopPropagation(); e.stopPropagation();
}; };
const touchProps = isMobile
? {
onTouchStart: handlePointerDown,
onTouchMove: handlePointerMove,
onTouchEnd: handlePointerUp,
}
: {
onPointerDown: handlePointerDown,
onPointerMove: handlePointerMove,
onPointerUp: handlePointerUp,
};
useEffect(() => { useEffect(() => {
const { w, h } = props.windowSize; const { w, h } = props.windowSize;
setPosition(({ x, y }) => ({ setPosition(({ x, y }) => ({
@@ -60,9 +75,10 @@ export default function Draggable(props) {
onClick={handleClick} onClick={handleClick}
> >
<div <div
onPointerDown={handlePointerDown} style={{
onPointerMove={handlePointerMove} touchAction: "none",
onPointerUp={handlePointerUp} }}
{...touchProps}
> >
{props.handler} {props.handler}
</div> </div>