feat: selection translation on mobile support
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import Paper from "@mui/material/Paper";
|
import Paper from "@mui/material/Paper";
|
||||||
import Box from "@mui/material/Box";
|
import Box from "@mui/material/Box";
|
||||||
|
import { isMobile } from "../../libs/mobile";
|
||||||
|
|
||||||
function Pointer({
|
function Pointer({
|
||||||
direction,
|
direction,
|
||||||
@@ -16,21 +17,23 @@ function Pointer({
|
|||||||
const [origin, setOrigin] = useState(null);
|
const [origin, setOrigin] = useState(null);
|
||||||
|
|
||||||
function handlePointerDown(e) {
|
function handlePointerDown(e) {
|
||||||
e.target.setPointerCapture(e.pointerId);
|
!isMobile && e.target.setPointerCapture(e.pointerId);
|
||||||
|
const { clientX, clientY } = isMobile ? e.targetTouches[0] : e;
|
||||||
setOrigin({
|
setOrigin({
|
||||||
x: position.x,
|
x: position.x,
|
||||||
y: position.y,
|
y: position.y,
|
||||||
w: size.w,
|
w: size.w,
|
||||||
h: size.h,
|
h: size.h,
|
||||||
clientX: e.clientX,
|
clientX,
|
||||||
clientY: e.clientY,
|
clientY,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function handlePointerMove(e) {
|
function handlePointerMove(e) {
|
||||||
|
const { clientX, clientY } = isMobile ? e.targetTouches[0] : e;
|
||||||
if (origin) {
|
if (origin) {
|
||||||
const dx = e.clientX - origin.clientX;
|
const dx = clientX - origin.clientX;
|
||||||
const dy = e.clientY - origin.clientY;
|
const dy = clientY - origin.clientY;
|
||||||
let x = position.x;
|
let x = position.x;
|
||||||
let y = position.y;
|
let y = position.y;
|
||||||
let w = size.w;
|
let w = size.w;
|
||||||
@@ -101,16 +104,24 @@ function Pointer({
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handlePointerUp(e) {
|
function handlePointerUp(e) {
|
||||||
|
e.stopPropagation();
|
||||||
setOrigin(null);
|
setOrigin(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const touchProps = isMobile
|
||||||
|
? {
|
||||||
|
onTouchStart: handlePointerDown,
|
||||||
|
onTouchMove: handlePointerMove,
|
||||||
|
onTouchEnd: handlePointerUp,
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
onPointerDown: handlePointerDown,
|
||||||
|
onPointerMove: handlePointerMove,
|
||||||
|
onPointerUp: handlePointerUp,
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div {...props} {...touchProps}>
|
||||||
{...props}
|
|
||||||
onPointerDown={handlePointerDown}
|
|
||||||
onPointerMove={handlePointerMove}
|
|
||||||
onPointerUp={handlePointerUp}
|
|
||||||
>
|
|
||||||
{children}
|
{children}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
@@ -162,6 +173,7 @@ export default function DraggableResizable({
|
|||||||
return (
|
return (
|
||||||
<Box
|
<Box
|
||||||
style={{
|
style={{
|
||||||
|
touchAction: "none",
|
||||||
position: "fixed",
|
position: "fixed",
|
||||||
left: position.x,
|
left: position.x,
|
||||||
top: position.y,
|
top: position.y,
|
||||||
|
|||||||
@@ -1,11 +1,24 @@
|
|||||||
|
import { limitNumber } from "../../libs/utils";
|
||||||
|
|
||||||
export default function TranBtn({ onClick, position, tranboxSetting }) {
|
export default function TranBtn({ onClick, position, tranboxSetting }) {
|
||||||
|
const left = limitNumber(
|
||||||
|
position.x + tranboxSetting.btnOffsetX,
|
||||||
|
0,
|
||||||
|
window.innerWidth - 20
|
||||||
|
);
|
||||||
|
const top = limitNumber(
|
||||||
|
position.y + tranboxSetting.btnOffsetY,
|
||||||
|
0,
|
||||||
|
window.innerHeight - 20
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
style={{
|
style={{
|
||||||
cursor: "pointer",
|
cursor: "pointer",
|
||||||
position: "absolute",
|
position: "absolute",
|
||||||
left: position.x + tranboxSetting.btnOffsetX,
|
left,
|
||||||
top: position.y + tranboxSetting.btnOffsetY,
|
top,
|
||||||
zIndex: 2147483647,
|
zIndex: 2147483647,
|
||||||
}}
|
}}
|
||||||
onClick={onClick}
|
onClick={onClick}
|
||||||
|
|||||||
@@ -2,20 +2,27 @@ import { useState, useEffect, useCallback } from "react";
|
|||||||
import TranBtn from "./TranBtn";
|
import TranBtn from "./TranBtn";
|
||||||
import TranBox from "./TranBox";
|
import TranBox from "./TranBox";
|
||||||
import { shortcutRegister } from "../../libs/shortcut";
|
import { shortcutRegister } from "../../libs/shortcut";
|
||||||
import { sleep } from "../../libs/utils";
|
import { sleep, limitNumber } from "../../libs/utils";
|
||||||
import { isGm } from "../../libs/client";
|
import { isGm } from "../../libs/client";
|
||||||
import { MSG_OPEN_TRANBOX, DEFAULT_TRANBOX_SHORTCUT } from "../../config";
|
import { MSG_OPEN_TRANBOX, DEFAULT_TRANBOX_SHORTCUT } from "../../config";
|
||||||
|
import { isMobile } from "../../libs/mobile";
|
||||||
|
|
||||||
export default function Slection({ contextMenus, tranboxSetting, transApis }) {
|
export default function Slection({ contextMenus, tranboxSetting, transApis }) {
|
||||||
|
const boxWidth = limitNumber(window.innerWidth, 300, 600);
|
||||||
|
const boxHeight = limitNumber(window.innerHeight, 200, 400);
|
||||||
|
|
||||||
const [showBox, setShowBox] = useState(false);
|
const [showBox, setShowBox] = useState(false);
|
||||||
const [showBtn, setShowBtn] = useState(false);
|
const [showBtn, setShowBtn] = useState(false);
|
||||||
const [selectedText, setSelText] = useState("");
|
const [selectedText, setSelText] = useState("");
|
||||||
const [text, setText] = useState("");
|
const [text, setText] = useState("");
|
||||||
const [position, setPosition] = useState({ x: 0, y: 0 });
|
const [position, setPosition] = useState({ x: 0, y: 0 });
|
||||||
const [boxSize, setBoxSize] = useState({ w: 600, h: 400 });
|
const [boxSize, setBoxSize] = useState({
|
||||||
|
w: boxWidth,
|
||||||
|
h: boxHeight,
|
||||||
|
});
|
||||||
const [boxPosition, setBoxPosition] = useState({
|
const [boxPosition, setBoxPosition] = useState({
|
||||||
x: (window.innerWidth - 600) / 2,
|
x: (window.innerWidth - boxWidth) / 2,
|
||||||
y: (window.innerHeight - 400) / 2,
|
y: (window.innerHeight - boxHeight) / 2,
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleClick = (e) => {
|
const handleClick = (e) => {
|
||||||
@@ -51,14 +58,18 @@ export default function Slection({ contextMenus, tranboxSetting, transApis }) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { pageX, pageY } = isMobile ? e.changedTouches[0] : e;
|
||||||
!tranboxSetting.hideTranBtn && setShowBtn(true);
|
!tranboxSetting.hideTranBtn && setShowBtn(true);
|
||||||
// setPosition({ x: e.clientX, y: e.clientY });
|
// setPosition({ x: e.clientX, y: e.clientY });
|
||||||
setPosition({ x: e.pageX, y: e.pageY });
|
setPosition({ x: pageX, y: pageY });
|
||||||
}
|
}
|
||||||
|
|
||||||
window.addEventListener("mouseup", handleMouseup);
|
window.addEventListener(isMobile ? "touchend" : "mouseup", handleMouseup);
|
||||||
return () => {
|
return () => {
|
||||||
window.removeEventListener("mouseup", handleMouseup);
|
window.removeEventListener(
|
||||||
|
isMobile ? "touchend" : "mouseup",
|
||||||
|
handleMouseup
|
||||||
|
);
|
||||||
};
|
};
|
||||||
}, [tranboxSetting.hideTranBtn]);
|
}, [tranboxSetting.hideTranBtn]);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user