| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- import {
- asTRBL,
- getOrientation,
- roundPoint
- } from '../layout/LayoutUtil';
- import {
- center,
- delta
- } from './PositionUtil';
- /**
- * Calculates the absolute point relative to the new element's position
- *
- * @param {point} point [absolute]
- * @param {bounds} oldBounds
- * @param {bounds} newBounds
- *
- * @return {point} point [absolute]
- */
- export function getNewAttachPoint(point, oldBounds, newBounds) {
- var oldCenter = center(oldBounds),
- newCenter = center(newBounds),
- oldDelta = delta(point, oldCenter);
- var newDelta = {
- x: oldDelta.x * (newBounds.width / oldBounds.width),
- y: oldDelta.y * (newBounds.height / oldBounds.height)
- };
- return roundPoint({
- x: newCenter.x + newDelta.x,
- y: newCenter.y + newDelta.y
- });
- }
- /**
- * Calculates the shape's delta relative to a new position
- * of a certain element's bounds
- *
- * @param {djs.model.Shape} point [absolute]
- * @param {bounds} oldBounds
- * @param {bounds} newBounds
- *
- * @return {delta} delta
- */
- export function getNewAttachShapeDelta(shape, oldBounds, newBounds) {
- var shapeCenter = center(shape),
- oldCenter = center(oldBounds),
- newCenter = center(newBounds),
- shapeDelta = delta(shape, shapeCenter),
- oldCenterDelta = delta(shapeCenter, oldCenter),
- stickyPositionDelta = getStickyPositionDelta(shapeCenter, oldBounds, newBounds);
- if (stickyPositionDelta) {
- return stickyPositionDelta;
- }
- var newCenterDelta = {
- x: oldCenterDelta.x * (newBounds.width / oldBounds.width),
- y: oldCenterDelta.y * (newBounds.height / oldBounds.height)
- };
- var newShapeCenter = {
- x: newCenter.x + newCenterDelta.x,
- y: newCenter.y + newCenterDelta.y
- };
- return roundPoint({
- x: newShapeCenter.x + shapeDelta.x - shape.x,
- y: newShapeCenter.y + shapeDelta.y - shape.y
- });
- }
- function getStickyPositionDelta(oldShapeCenter, oldBounds, newBounds) {
- var oldTRBL = asTRBL(oldBounds),
- newTRBL = asTRBL(newBounds);
- if (isMoved(oldTRBL, newTRBL)) {
- return null;
- }
- var oldOrientation = getOrientation(oldBounds, oldShapeCenter),
- stickyPositionDelta,
- newShapeCenter,
- newOrientation;
- if (oldOrientation === 'top') {
- stickyPositionDelta = {
- x: 0,
- y: newTRBL.bottom - oldTRBL.bottom
- };
- } else if (oldOrientation === 'bottom') {
- stickyPositionDelta = {
- x: 0,
- y: newTRBL.top - oldTRBL.top
- };
- } else if (oldOrientation === 'right') {
- stickyPositionDelta = {
- x: newTRBL.left - oldTRBL.left,
- y: 0
- };
- } else if (oldOrientation === 'left') {
- stickyPositionDelta = {
- x: newTRBL.right - oldTRBL.right,
- y: 0
- };
- } else {
- // fallback to proportional movement for corner-placed attachments
- return null;
- }
- newShapeCenter = {
- x: oldShapeCenter.x + stickyPositionDelta.x,
- y: oldShapeCenter.y + stickyPositionDelta.y
- };
- newOrientation = getOrientation(newBounds, newShapeCenter);
- if (newOrientation !== oldOrientation) {
- // fallback to proportional movement if orientation would otherwise change
- return null;
- }
- return stickyPositionDelta;
- }
- function isMoved(oldTRBL, newTRBL) {
- return isHorizontallyMoved(oldTRBL, newTRBL) || isVerticallyMoved(oldTRBL, newTRBL);
- }
- function isHorizontallyMoved(oldTRBL, newTRBL) {
- return oldTRBL.right !== newTRBL.right && oldTRBL.left !== newTRBL.left;
- }
- function isVerticallyMoved(oldTRBL, newTRBL) {
- return oldTRBL.top !== newTRBL.top && oldTRBL.bottom !== newTRBL.bottom;
- }
|