| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- import SnapContext from './SnapContext';
- import {
- bottomRight,
- getChildren,
- isSnapped,
- setSnapped,
- topLeft,
- } from './SnapUtil';
- import { isCmd } from '../keyboard/KeyboardUtil';
- import {
- asTRBL,
- getMid
- } from '../../layout/LayoutUtil';
- import { forEach } from 'min-dash';
- var HIGHER_PRIORITY = 1250;
- /**
- * Snap during resize.
- *
- * @param {EventBus} eventBus
- * @param {Snapping} snapping
- */
- export default function ResizeSnapping(eventBus, snapping) {
- var self = this;
- eventBus.on([ 'resize.start' ], function(event) {
- self.initSnap(event);
- });
- eventBus.on([
- 'resize.move',
- 'resize.end',
- ], HIGHER_PRIORITY, function(event) {
- var context = event.context,
- shape = context.shape,
- parent = shape.parent,
- direction = context.direction,
- snapContext = context.snapContext;
- if (event.originalEvent && isCmd(event.originalEvent)) {
- return;
- }
- if (isSnapped(event)) {
- return;
- }
- var snapPoints = snapContext.pointsForTarget(parent);
- if (!snapPoints.initialized) {
- snapPoints = self.addSnapTargetPoints(snapPoints, shape, parent, direction);
- snapPoints.initialized = true;
- }
- if (isHorizontal(direction)) {
- setSnapped(event, 'x', event.x);
- }
- if (isVertical(direction)) {
- setSnapped(event, 'y', event.y);
- }
- snapping.snap(event, snapPoints);
- });
- eventBus.on([ 'resize.cleanup' ], function() {
- snapping.hide();
- });
- }
- ResizeSnapping.prototype.initSnap = function(event) {
- var context = event.context,
- shape = context.shape,
- direction = context.direction,
- snapContext = context.snapContext;
- if (!snapContext) {
- snapContext = context.snapContext = new SnapContext();
- }
- var snapOrigin = getSnapOrigin(shape, direction);
- snapContext.setSnapOrigin('corner', {
- x: snapOrigin.x - event.x,
- y: snapOrigin.y - event.y
- });
- return snapContext;
- };
- ResizeSnapping.prototype.addSnapTargetPoints = function(snapPoints, shape, target, direction) {
- var snapTargets = this.getSnapTargets(shape, target);
- forEach(snapTargets, function(snapTarget) {
- snapPoints.add('corner', bottomRight(snapTarget));
- snapPoints.add('corner', topLeft(snapTarget));
- });
- snapPoints.add('corner', getSnapOrigin(shape, direction));
- return snapPoints;
- };
- ResizeSnapping.$inject = [
- 'eventBus',
- 'snapping'
- ];
- ResizeSnapping.prototype.getSnapTargets = function(shape, target) {
- return getChildren(target).filter(function(child) {
- return !isAttached(child, shape)
- && !isConnection(child)
- && !isHidden(child)
- && !isLabel(child);
- });
- };
- // helpers //////////
- function getSnapOrigin(shape, direction) {
- var mid = getMid(shape),
- trbl = asTRBL(shape);
- var snapOrigin = {
- x: mid.x,
- y: mid.y
- };
- if (direction.indexOf('n') !== -1) {
- snapOrigin.y = trbl.top;
- } else if (direction.indexOf('s') !== -1) {
- snapOrigin.y = trbl.bottom;
- }
- if (direction.indexOf('e') !== -1) {
- snapOrigin.x = trbl.right;
- } else if (direction.indexOf('w') !== -1) {
- snapOrigin.x = trbl.left;
- }
- return snapOrigin;
- }
- function isAttached(element, host) {
- return element.host === host;
- }
- function isConnection(element) {
- return !!element.waypoints;
- }
- function isHidden(element) {
- return !!element.hidden;
- }
- function isLabel(element) {
- return !!element.labelTarget;
- }
- function isHorizontal(direction) {
- return direction === 'n' || direction === 's';
- }
- function isVertical(direction) {
- return direction === 'e' || direction === 'w';
- }
|