Skip to content
This repository was archived by the owner on Mar 13, 2023. It is now read-only.

Commit 70fe21f

Browse files
MrDockalSTRML
authored andcommitted
feat(DraggableCore): apply scale property while dragging an element (react-grid-layout#438)
1 parent 1931791 commit 70fe21f

4 files changed

Lines changed: 87 additions & 4 deletions

File tree

lib/DraggableCore.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export type DraggableCoreProps = {
6767
onDrag: DraggableEventHandler,
6868
onStop: DraggableEventHandler,
6969
onMouseDown: (e: MouseEvent) => void,
70+
scale: number,
7071
};
7172

7273
//
@@ -185,6 +186,11 @@ export default class DraggableCore extends React.Component<DraggableCoreProps, D
185186
*/
186187
onMouseDown: PropTypes.func,
187188

189+
/**
190+
* `scale`, if set, applies scaling while dragging an element
191+
*/
192+
scale: PropTypes.number,
193+
188194
/**
189195
* These properties should be defined on the child, not here.
190196
*/
@@ -206,6 +212,7 @@ export default class DraggableCore extends React.Component<DraggableCoreProps, D
206212
onDrag: function(){},
207213
onStop: function(){},
208214
onMouseDown: function(){},
215+
scale: 1,
209216
};
210217

211218
state = {

lib/utils/domFns.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ export function innerWidth(node: HTMLElement): number {
9999
}
100100

101101
// Get from offsetParent
102-
export function offsetXYFromParent(evt: {clientX: number, clientY: number}, offsetParent: HTMLElement): ControlPosition {
102+
export function offsetXYFromParent(evt: {clientX: number, clientY: number}, offsetParent: HTMLElement, scale: number): ControlPosition {
103103
const isBody = offsetParent === offsetParent.ownerDocument.body;
104104
const offsetParentRect = isBody ? {left: 0, top: 0} : offsetParent.getBoundingClientRect();
105105

106-
const x = evt.clientX + offsetParent.scrollLeft - offsetParentRect.left;
107-
const y = evt.clientY + offsetParent.scrollTop - offsetParentRect.top;
106+
const x = (evt.clientX + offsetParent.scrollLeft - offsetParentRect.left) / scale;
107+
const y = (evt.clientY + offsetParent.scrollTop - offsetParentRect.top) / scale;
108108

109109
return {x, y};
110110
}

lib/utils/positionFns.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export function getControlPosition(e: MouseTouchEvent, touchIdentifier: ?number,
7373
const node = findDOMNode(draggableCore);
7474
// User can provide an offsetParent if desired.
7575
const offsetParent = draggableCore.props.offsetParent || node.offsetParent || node.ownerDocument.body;
76-
return offsetXYFromParent(touchObj || e, offsetParent);
76+
return offsetXYFromParent(touchObj || e, offsetParent, draggableCore.props.scale);
7777
}
7878

7979
// Create an data object exposed by <DraggableCore>'s events

specs/draggable.spec.jsx

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,44 @@ describe('react-draggable', function () {
683683

684684
simulateMovementFromTo(drag, 200, 200, 300, 300);
685685
});
686+
687+
it('should call back with correct position when parent element is 2x scaled', function() {
688+
function onDrag(event, data) {
689+
// visually it will look like 100, because parent is 2x scaled
690+
assert(data.x === 50);
691+
assert(data.y === 50);
692+
assert(data.deltaX === 50);
693+
assert(data.deltaY === 50);
694+
assert(data.node === ReactDOM.findDOMNode(drag));
695+
}
696+
drag = TestUtils.renderIntoDocument(
697+
<Draggable onDrag={onDrag} scale={2}>
698+
<div />
699+
</Draggable>
700+
);
701+
702+
// (element, fromX, fromY, toX, toY)
703+
simulateMovementFromTo(drag, 0, 0, 100, 100);
704+
});
705+
706+
it('should call back with correct position when parent element is 0.5x scaled', function() {
707+
function onDrag(event, data) {
708+
// visually it will look like 100, because parent is 0.5x scaled
709+
assert(data.x === 200);
710+
assert(data.y === 200);
711+
assert(data.deltaX === 200);
712+
assert(data.deltaY === 200);
713+
assert(data.node === ReactDOM.findDOMNode(drag));
714+
}
715+
drag = TestUtils.renderIntoDocument(
716+
<Draggable onDrag={onDrag} scale={0.5}>
717+
<div />
718+
</Draggable>
719+
);
720+
721+
// (element, fromX, fromY, toX, toY)
722+
simulateMovementFromTo(drag, 0, 0, 100, 100);
723+
});
686724
});
687725

688726
describe('DraggableCore callbacks', function () {
@@ -703,6 +741,44 @@ describe('react-draggable', function () {
703741
// (element, fromX, fromY, toX, toY)
704742
simulateMovementFromTo(drag, 0, 0, 100, 100);
705743
});
744+
745+
it('should call back with correct position when parent element is 2x scaled', function() {
746+
function onDrag(event, data) {
747+
// visually it will look like 100, because parent is 2x scaled
748+
assert(data.x === 50);
749+
assert(data.y === 50);
750+
assert(data.deltaX === 50);
751+
assert(data.deltaY === 50);
752+
assert(data.node === ReactDOM.findDOMNode(drag));
753+
}
754+
drag = TestUtils.renderIntoDocument(
755+
<DraggableCore onDrag={onDrag} scale={2}>
756+
<div />
757+
</DraggableCore>
758+
);
759+
760+
// (element, fromX, fromY, toX, toY)
761+
simulateMovementFromTo(drag, 0, 0, 100, 100);
762+
});
763+
764+
it('should call back with correct position when parent element is 0.5x scaled', function() {
765+
function onDrag(event, data) {
766+
// visually it will look like 100, because parent is 0.5x scaled
767+
assert(data.x === 200);
768+
assert(data.y === 200);
769+
assert(data.deltaX === 200);
770+
assert(data.deltaY === 200);
771+
assert(data.node === ReactDOM.findDOMNode(drag));
772+
}
773+
drag = TestUtils.renderIntoDocument(
774+
<DraggableCore onDrag={onDrag} scale={0.5}>
775+
<div />
776+
</DraggableCore>
777+
);
778+
779+
// (element, fromX, fromY, toX, toY)
780+
simulateMovementFromTo(drag, 0, 0, 100, 100);
781+
});
706782
});
707783

708784

0 commit comments

Comments
 (0)