Skip to content

Commit 43e08d1

Browse files
committed
fix: fix request with bounds #809
1 parent e1b3ae7 commit 43e08d1

File tree

5 files changed

+76
-15
lines changed

5 files changed

+76
-15
lines changed

packages/react-moveable/src/ables/Draggable.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,11 @@ export default {
174174
distY = r * Math.sin(dragRotateRad);
175175
}
176176

177-
if (!isPinch && !parentEvent && !parentFlag && (!throttleDragRotate || distX || distY)) {
177+
if (!isPinch && !parentEvent && !parentFlag) {
178178
const [verticalInfo, horizontalInfo] = checkSnapBoundsDrag(
179-
moveable, distX, distY, throttleDragRotate, isRequest || deltaOffset, datas,
179+
moveable, distX, distY,
180+
throttleDragRotate,
181+
isRequest || deltaOffset, datas,
180182
);
181183
isVerticalSnap = verticalInfo.isSnap;
182184
isVerticalBound = verticalInfo.isBound;

packages/react-moveable/src/types.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3041,6 +3041,34 @@ export interface MoveableInterface {
30413041
isMoveableElement(target: Element): boolean;
30423042
updateRect(type?: "Start" | "" | "End", isTarget?: boolean, isSetState?: boolean): void;
30433043
updateTarget(): void;
3044+
/**
3045+
* Request able through a method rather than an event.
3046+
* At the moment of execution, requestStart is executed,
3047+
* and then request and requestEnd can be executed through Requester.
3048+
* @see {@link https://daybrush.com/moveable/release/latest/doc/Moveable.Draggable.html#request Draggable Requester}
3049+
* @see {@link https://daybrush.com/moveable/release/latest/doc/Moveable.Resizable.html#request Resizable Requester}
3050+
* @see {@link https://daybrush.com/moveable/release/latest/doc/Moveable.Scalable.html#request Scalable Requester}
3051+
* @see {@link https://daybrush.com/moveable/release/latest/doc/Moveable.Rotatable.html#request Rotatable Requester}
3052+
* @see {@link https://daybrush.com/moveable/release/latest/doc/Moveable.OriginDraggable.html#request OriginDraggable Requester}
3053+
* @param - ableName
3054+
* @param - request to be able params.
3055+
* @param - If isInstant is true, request and requestEnd are executed immediately.
3056+
* @return - Able Requester. If there is no request in able, nothing will work.
3057+
* @example
3058+
* import Moveable from "moveable";
3059+
*
3060+
* const moveable = new Moveable(document.body);
3061+
*
3062+
* // Instantly Request (requestStart - request - requestEnd)
3063+
* moveable.request("draggable", { deltaX: 10, deltaY: 10 }, true);
3064+
*
3065+
* // Start move
3066+
* const requester = moveable.request("draggable");
3067+
* requester.request({ deltaX: 10, deltaY: 10 });
3068+
* requester.request({ deltaX: 10, deltaY: 10 });
3069+
* requester.request({ deltaX: 10, deltaY: 10 });
3070+
* requester.requestEnd();
3071+
*/
30443072
request<RequestParam extends {} = AbleRequestParam>(
30453073
ableName: string, params?: RequestParam, isInstant?: boolean): Requester<RequestParam>;
30463074
destroy(): void;

packages/react-moveable/stories/7-Request/apps/DraggableApp.tsx

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import * as React from "react";
22
import Moveable from "@/react-moveable";
33

44
export default function App(props: Record<string, any>) {
5-
const [translate, setTranslate] = React.useState([0, 0]);
6-
const targetRef = React.useRef<HTMLDivElement>(null);
75
const xInputRef = React.useRef<HTMLInputElement>(null);
86
const yInputRef = React.useRef<HTMLInputElement>(null);
97
const moveableRef = React.useRef<Moveable>(null);
@@ -43,27 +41,19 @@ export default function App(props: Record<string, any>) {
4341
<div className="container" style={{
4442
transform: `scale(${props.containerScale})`,
4543
}}>
46-
<div className="target" ref={targetRef}>Target</div>
44+
<div className="target">Target</div>
4745
<Moveable
4846
ref={moveableRef}
49-
target={targetRef}
47+
target={".target"}
5048
draggable={true}
5149
throttleDrag={props.throttleDrag}
5250
edgeDraggable={props.edgeDraggable}
5351
startDragRotate={props.startDragRotate}
5452
throttleDragRotate={props.throttleDragRotate}
55-
onDragStart={e => {
56-
e.set(translate);
57-
}}
5853
onDrag={e => {
59-
e.target.style.transform = `translate(${e.beforeTranslate[0]}px, ${e.beforeTranslate[1]}px)`;
54+
e.target.style.transform = e.transform;
6055
}}
6156
onDragEnd={e => {
62-
const lastEvent = e.lastEvent;
63-
if (lastEvent) {
64-
setTranslate(lastEvent.beforeTranslate);
65-
}
66-
6757
requestAnimationFrame(() => {
6858
const rect = e.moveable.getRect();
6959

packages/react-moveable/stories/99-Tests/Deafult.stories.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,9 @@ group.add("Test snap for scaled target", {
104104
app: require("./ReactZoomedSnapApp").default,
105105
path: require.resolve("./ReactZoomedSnapApp"),
106106
});
107+
108+
109+
group.add("Test request with bounds", {
110+
app: require("./ReactRequestBoundsApp").default,
111+
path: require.resolve("./ReactRequestBoundsApp"),
112+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as React from "react";
2+
import Moveable from "@/react-moveable";
3+
4+
export default function App() {
5+
const moveableRef = React.useRef<Moveable>(null);
6+
7+
return (
8+
<div className="container">
9+
<button onClick={() => {
10+
moveableRef.current!.request("draggable", {
11+
isInstant: true,
12+
deltaX: 0,
13+
deltaY: 0,
14+
});
15+
}}>Request</button>
16+
<div className="target">Target</div>
17+
18+
<Moveable
19+
ref={moveableRef}
20+
target={".target"}
21+
draggable={true}
22+
rotatable={true}
23+
resizable={true}
24+
snappable={true}
25+
bounds={{
26+
left: 200,
27+
top: 0,
28+
}}
29+
onRender={e => {
30+
e.target.style.cssText += e.cssText;
31+
}}
32+
/>
33+
</div>
34+
);
35+
}

0 commit comments

Comments
 (0)