Skip to content

Commit

Permalink
fix: fix dragTarget in control box #1054
Browse files Browse the repository at this point in the history
  • Loading branch information
daybrush committed Dec 2, 2023
1 parent 4d12e48 commit f4e3232
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/react-moveable/src/MoveableGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import MoveableManager from "./MoveableManager";
import { GroupableProps, GroupRect, MoveableManagerInterface, MoveableTargetGroupsType, RectInfo } from "./types";
import ChildrenDiffer from "@egjs/children-differ";
import { getAbleGesto, getTargetAbleGesto } from "./gesto/getAbleGesto";
import { getControlAbleGesto, getTargetAbleGesto } from "./gesto/getAbleGesto";
import Groupable from "./ables/Groupable";
import { MIN_NUM, MAX_NUM, TINY_NUM } from "./consts";
import {
Expand Down Expand Up @@ -462,7 +462,7 @@ class MoveableGroup extends MoveableManager<GroupableProps> {
this.targetGesto = getTargetAbleGesto(this, this._dragTarget!, "Group");
}
if (!this.controlGesto) {
this.controlGesto = getAbleGesto(this, this.controlBox, "controlAbles", "GroupControl");
this.controlGesto = getControlAbleGesto(this, "GroupControl");
}
}
const isContainerChanged = !equals(state.container, props.container);
Expand Down
8 changes: 5 additions & 3 deletions packages/react-moveable/src/MoveableManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ import {
GroupableProps,
MoveableRefType,
} from "./types";
import { triggerAble, getTargetAbleGesto, getAbleGesto, checkMoveableTarget } from "./gesto/getAbleGesto";
import {
triggerAble, getTargetAbleGesto,
checkMoveableTarget, getControlAbleGesto,
} from "./gesto/getAbleGesto";
import { createOriginMatrix, multiplies, plus } from "@scena/matrix";
import {
addClass, cancelAnimationFrame, find,
Expand Down Expand Up @@ -1045,7 +1048,6 @@ export default class MoveableManager<T = {}>
this._updateMutationObserver(prevProps);
}
protected _updateEvents() {
const controlBoxElement = this.controlBox;
const hasTargetAble = this.targetAbles.length;
const hasControlAble = this.controlAbles.length;
const target = this._dragTarget;
Expand All @@ -1064,7 +1066,7 @@ export default class MoveableManager<T = {}>
this.targetGesto = getTargetAbleGesto(this, target!, "");
}
if (!this.controlGesto && hasControlAble) {
this.controlGesto = getAbleGesto(this, controlBoxElement, "controlAbles", "Control");
this.controlGesto = getControlAbleGesto(this, "Control");
}
}
protected _updateTargets() {
Expand Down
35 changes: 31 additions & 4 deletions packages/react-moveable/src/gesto/getAbleGesto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,13 @@ export function triggerAble(
return true;
}

export function checkMoveableTarget(moveable: MoveableManagerInterface) {
export function checkMoveableTarget(moveable: MoveableManagerInterface, isControl?: boolean) {
return (e: { inputEvent: Event }, target: EventTarget | null = e.inputEvent.target) => {
const eventTarget = target as Element;
const areaElement = moveable.areaElement;
const dragTargetElement = (moveable as any)._dragTarget;

if (!dragTargetElement || moveable.controlGesto?.isFlag()) {
if (!dragTargetElement || (!isControl && moveable.controlGesto?.isFlag())) {
return false;
}

Expand Down Expand Up @@ -241,12 +241,39 @@ export function getTargetAbleGesto(
if (!dragArea && dragTarget && target && moveableTarget !== target && props.dragTargetSelf) {
targets.push(target);
}
const checkTarget = checkMoveableTarget(moveable);

return getAbleGesto(moveable, targets, "targetAbles", eventAffix, {
dragStart: checkMoveableTarget(moveable),
pinchStart: checkMoveableTarget(moveable),
dragStart: checkTarget,
pinchStart: checkTarget,
});
}

export function getControlAbleGesto(
moveable: MoveableManagerInterface,
eventAffix: string,
) {
const controlBox = moveable.controlBox;
const targets: Array<HTMLElement | SVGElement> = [];

targets.push(controlBox);

const checkTarget = checkMoveableTarget(moveable, true);
const checkControlTarget = (e: any, target: EventTarget | null = e.inputEvent.target) => {
if (target === controlBox) {
return true;
}
const result = checkTarget(e, target);

return !result;
};

return getAbleGesto(moveable, targets, "controlAbles", eventAffix, {
dragStart: checkControlTarget,
pinchStart: checkControlTarget,
});
}

export function getAbleGesto(
moveable: MoveableManagerInterface,
target: HTMLElement | SVGElement | Array<HTMLElement | SVGElement>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ export const CustomAbleMouseEnterLeave = add("Mouse Enter & Leave", {
app: require("./ReactMouseEnterLeaveApp").default,
path: require.resolve("./ReactMouseEnterLeaveApp"),
});


export const CustomAbleDragTarget = add("DragTarget", {
app: require("./ReactDragTargetAbleApp").default,
path: require.resolve("./ReactDragTargetAbleApp"),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import React from "react";
// import "./App.css";
import Moveable, { MoveableManagerInterface, Renderer } from "@/react-moveable";

const DimensionViewable = {
name: "dimensionViewable",
props: [],
events: [],
render(moveable: MoveableManagerInterface<any, any>, React: Renderer) {
const rect = moveable.getRect();

// Add key (required)
// Add class prefix moveable-(required)
return (
<div
key={"dimension-viewer"}
className={"moveable-dimension"}
style={{
position: "absolute",
left: `${rect.width / 2}px`,
top: `${rect.height + 20}px`,
background: "#4af",
borderRadius: "2px",
padding: "2px 4px",
color: "white",
fontSize: "13px",
whiteSpace: "nowrap",
fontWeight: "bold",
willChange: "transform",
transform: `translate(-50%, 0px)`,
}}
>
Target
</div>
);
},
} as const;

function App() {
const [dragTarget, setDragTarget] = React.useState<HTMLElement>();

React.useEffect(() => {
setDragTarget(document.querySelector<HTMLElement>(".moveable-dimension")!);
}, []);
return (
<div className="container">
<div className="target target1"></div>
<div className="target target2"></div>
<Moveable
ables={[DimensionViewable]}
dragTarget={dragTarget}
resizable={true}
props={{
dimensionViewable: true,
}}
target={[".target1"]}
draggable={true}
onDrag={(e) => {
e.target.style.transform = e.transform;
}}
onResize={(e) => {
e.target.style.width = `${e.width}px`;
e.target.style.height = `${e.height}px`;
e.target.style.transform = e.drag.transform;
}}
onRotate={(e) => {
e.target.style.transform = e.drag.transform;
}}
elementGuidelines={[".target1", ".target2"]}
snappable={true}
/>
</div>
);
}

export default App;

0 comments on commit f4e3232

Please sign in to comment.