Skip to content

Commit 4d0983b

Browse files
authored
Merge pull request #148 from FelixZY/fzy/mobile/1
Basic touchscreen support (#147) and rewrite of coordinate management
2 parents 4a7d312 + 5a56c3b commit 4d0983b

23 files changed

+708
-241
lines changed

package-lock.json

+22-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"@uiw/react-codemirror": "^4.21.25",
2020
"@vercel/analytics": "^1.2.2",
2121
"axios": "^1.6.2",
22+
"classnames": "^2.5.1",
2223
"dexie": "^3.2.4",
2324
"dexie-react-hooks": "^1.1.7",
2425
"file-saver": "^2.0.5",
@@ -35,7 +36,8 @@
3536
"react-hotkeys-hook": "^4.4.1",
3637
"react-i18next": "^14.1.1",
3738
"react-router-dom": "^6.21.0",
38-
"url": "^0.11.1"
39+
"url": "^0.11.1",
40+
"usehooks-ts": "^3.1.0"
3941
},
4042
"devDependencies": {
4143
"@types/react": "^18.2.43",

src/components/EditorCanvas/Area.jsx

+27-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from "react";
1+
import { useRef, useState } from "react";
22
import { Button, Popover, Input } from "@douyinfe/semi-ui";
33
import { IconEdit, IconDeleteStroked } from "@douyinfe/semi-icons";
44
import {
@@ -9,22 +9,33 @@ import {
99
State,
1010
} from "../../data/constants";
1111
import {
12+
useCanvas,
1213
useLayout,
1314
useSettings,
1415
useUndoRedo,
1516
useSelect,
1617
useAreas,
1718
useSaveState,
18-
useTransform,
1919
} from "../../hooks";
2020
import ColorPalette from "../ColorPicker";
2121
import { useTranslation } from "react-i18next";
22+
import { useHover } from "usehooks-ts";
2223

23-
export default function Area({ data, onMouseDown, setResize, setInitCoords }) {
24-
const [hovered, setHovered] = useState(false);
24+
export default function Area({
25+
data,
26+
onPointerDown,
27+
setResize,
28+
setInitCoords,
29+
}) {
30+
const ref = useRef(null);
31+
const isHovered = useHover(ref);
32+
const {
33+
pointer: {
34+
spaces: { diagram: pointer },
35+
},
36+
} = useCanvas();
2537
const { layout } = useLayout();
2638
const { settings } = useSettings();
27-
const { transform } = useTransform();
2839
const { setSaveState } = useSaveState();
2940
const { selectedElement, setSelectedElement } = useSelect();
3041

@@ -35,8 +46,8 @@ export default function Area({ data, onMouseDown, setResize, setInitCoords }) {
3546
y: data.y,
3647
width: data.width,
3748
height: data.height,
38-
mouseX: e.clientX / transform.zoom,
39-
mouseY: e.clientY / transform.zoom,
49+
pointerX: pointer.x,
50+
pointerY: pointer.y,
4051
});
4152
};
4253

@@ -84,21 +95,18 @@ export default function Area({ data, onMouseDown, setResize, setInitCoords }) {
8495
selectedElement.open;
8596

8697
return (
87-
<g
88-
onMouseEnter={() => setHovered(true)}
89-
onMouseLeave={() => setHovered(false)}
90-
>
98+
<g ref={ref}>
9199
<foreignObject
92100
key={data.id}
93101
x={data.x}
94102
y={data.y}
95103
width={data.width > 0 ? data.width : 0}
96104
height={data.height > 0 ? data.height : 0}
97-
onMouseDown={onMouseDown}
105+
onPointerDown={onPointerDown}
98106
>
99107
<div
100108
className={`border-2 ${
101-
hovered
109+
isHovered
102110
? "border-dashed border-blue-500"
103111
: selectedElement.element === ObjectType.AREA &&
104112
selectedElement.id === data.id
@@ -114,7 +122,7 @@ export default function Area({ data, onMouseDown, setResize, setInitCoords }) {
114122
<div className="text-color select-none overflow-hidden text-ellipsis">
115123
{data.name}
116124
</div>
117-
{(hovered || (areaIsSelected() && !layout.sidebar)) && (
125+
{(isHovered || (areaIsSelected() && !layout.sidebar)) && (
118126
<Popover
119127
visible={areaIsSelected() && !layout.sidebar}
120128
onClickOutSide={onClickOutSide}
@@ -139,7 +147,7 @@ export default function Area({ data, onMouseDown, setResize, setInitCoords }) {
139147
</div>
140148
</div>
141149
</foreignObject>
142-
{hovered && (
150+
{isHovered && (
143151
<>
144152
<circle
145153
cx={data.x}
@@ -149,7 +157,7 @@ export default function Area({ data, onMouseDown, setResize, setInitCoords }) {
149157
stroke="#5891db"
150158
strokeWidth={2}
151159
cursor="nwse-resize"
152-
onMouseDown={(e) => handleResize(e, "tl")}
160+
onPointerDown={(e) => e.isPrimary && handleResize(e, "tl")}
153161
/>
154162
<circle
155163
cx={data.x + data.width}
@@ -159,7 +167,7 @@ export default function Area({ data, onMouseDown, setResize, setInitCoords }) {
159167
stroke="#5891db"
160168
strokeWidth={2}
161169
cursor="nesw-resize"
162-
onMouseDown={(e) => handleResize(e, "tr")}
170+
onPointerDown={(e) => e.isPrimary && handleResize(e, "tr")}
163171
/>
164172
<circle
165173
cx={data.x}
@@ -169,7 +177,7 @@ export default function Area({ data, onMouseDown, setResize, setInitCoords }) {
169177
stroke="#5891db"
170178
strokeWidth={2}
171179
cursor="nesw-resize"
172-
onMouseDown={(e) => handleResize(e, "bl")}
180+
onPointerDown={(e) => e.isPrimary && handleResize(e, "bl")}
173181
/>
174182
<circle
175183
cx={data.x + data.width}
@@ -179,7 +187,7 @@ export default function Area({ data, onMouseDown, setResize, setInitCoords }) {
179187
stroke="#5891db"
180188
strokeWidth={2}
181189
cursor="nwse-resize"
182-
onMouseDown={(e) => handleResize(e, "br")}
190+
onPointerDown={(e) => e.isPrimary && handleResize(e, "br")}
183191
/>
184192
</>
185193
)}

0 commit comments

Comments
 (0)