Skip to content

Commit

Permalink
fix: fix drag start edge #745
Browse files Browse the repository at this point in the history
  • Loading branch information
daybrush committed Sep 25, 2022
1 parent 7cfadae commit 105bb1c
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 16 deletions.
13 changes: 10 additions & 3 deletions packages/react-moveable/src/react-moveable/MoveableManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
MoveableDefaultOptions,
GroupableProps,
} from "./types";
import { triggerAble, getTargetAbleGesto, getAbleGesto } from "./gesto/getAbleGesto";
import { triggerAble, getTargetAbleGesto, getAbleGesto, checkMoveableTarget } from "./gesto/getAbleGesto";
import { plus } from "@scena/matrix";
import { cancelAnimationFrame, find, getKeys, IObject, requestAnimationFrame } from "@daybrush/utils";
import { renderLine } from "./renderDirections";
Expand Down Expand Up @@ -239,9 +239,16 @@ export default class MoveableManager<T = {}>
*/
public dragStart(e: MouseEvent | TouchEvent) {
const targetGesto = this.targetGesto;
const controlGesto = this.controlGesto;

if (targetGesto && !targetGesto.isFlag()) {
targetGesto.triggerDragStart(e);
if (targetGesto && checkMoveableTarget(this)({ inputEvent: e })) {
if (!targetGesto.isFlag()) {
targetGesto.triggerDragStart(e);
}
} else if (controlGesto && this.isMoveableElement(e.target as Element)) {
if (!controlGesto.isFlag()) {
controlGesto.triggerDragStart(e);
}
}
return this;
}
Expand Down
31 changes: 18 additions & 13 deletions packages/react-moveable/src/react-moveable/gesto/getAbleGesto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,22 @@ export function triggerAble(
return true;
}

export function checkMoveableTarget(moveable: MoveableManagerInterface) {
const dragTarget = moveable.props.dragTarget;

return (e: { inputEvent: Event }) => {
const eventTarget = e.inputEvent.target as Element;
const areaElement = moveable.areaElement;

return dragTarget && (eventTarget === dragTarget || dragTarget.contains(eventTarget))
|| eventTarget === areaElement
|| (!moveable.isMoveableElement(eventTarget) && !moveable.controlBox.getElement().contains(eventTarget))
|| hasClass(eventTarget, "moveable-area")
|| hasClass(eventTarget, "moveable-padding")
|| hasClass(eventTarget, "moveable-edgeDraggable");
};
}

export function getTargetAbleGesto(
moveable: MoveableManagerInterface,
moveableTarget: HTMLElement | SVGElement,
Expand All @@ -174,20 +190,9 @@ export function getTargetAbleGesto(
targets.push(moveableTarget);
}

const startFunc = (e: any) => {
const eventTarget = e.inputEvent.target;
const areaElement = moveable.areaElement;

return dragTarget && (eventTarget === dragTarget || dragTarget.contains(eventTarget))
|| eventTarget === areaElement
|| (!moveable.isMoveableElement(eventTarget) && !moveable.controlBox.getElement().contains(eventTarget))
|| hasClass(eventTarget, "moveable-area")
|| hasClass(eventTarget, "moveable-padding");
};

return getAbleGesto(moveable, targets, "targetAbles", eventAffix, {
dragStart: startFunc,
pinchStart: startFunc,
dragStart: checkMoveableTarget(moveable),
pinchStart: checkMoveableTarget(moveable),
});
}
export function getAbleGesto(
Expand Down
4 changes: 4 additions & 0 deletions packages/react-moveable/stories/99-Tests/Deafult.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,7 @@ group.add("Test Drag Target", {
app: require("./ReactDragTargetApp").default,
text: require("!!raw-loader!./ReactDragTargetApp").default,
});
group.add("Test Drag Start Group Manually", {
app: require("./ReactDragStartGroupApp").default,
text: require("!!raw-loader!./ReactDragStartGroupApp").default,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import * as React from "react";
import { useState } from "react";
import Moveable from "@/react-moveable";

export default function App() {
const [groupInstances, setGroupInstances] = useState<any[]>([]);
const t1 = React.useRef<any>(null);
const t2 = React.useRef<any>(null);
const groupMoveable = React.useRef<any>(null);

function initGroupElements() {
const arr: any[] = [];
arr.push(document.getElementsByClassName("target1")[0]);
arr.push(document.getElementsByClassName("target2")[0]);
setGroupInstances(arr);
}

function handleDragStart(e: any) {
initGroupElements();
groupMoveable.current!.dragStart(e.inputEvent);
}

return (
<div className="root">
<div className="container">
<div className={`target target1`}>Target 1</div>
<div className={`target target2`}>Target 2</div>
<Moveable
ref={t1}
target={`.target1` as string}
draggable={true}
edgeDraggable={true}
origin={false}
onDragStart={handleDragStart}
/>
<Moveable
ref={t2}
target={`.target2` as string}
draggable={true}
origin={false}
edgeDraggable={true}
onDragStart={handleDragStart}
/>
<Moveable
ref={groupMoveable}
target={groupInstances}
hideDefaultLines={false}
draggable={true}
onDragGroup={(e) => {
e.events.forEach(
(ev) => (ev.target.style.transform = ev.transform)
);
t1.current.updateRect();
t2.current.updateRect();
}}
onDragGroupEnd={() => {
setGroupInstances([]);
}}
/>
</div>
</div>
);
}

0 comments on commit 105bb1c

Please sign in to comment.