all files / lib/features/grid-snapping/behavior/ PasteBehavior.js

92.31% Statements 12/13
50% Branches 1/2
100% Functions 2/2
92.31% Lines 12/13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38                        35×   35×                          
import inherits from 'inherits';
 
import CommandInterceptor from '../../../command/CommandInterceptor';
 
import { getBBox } from '../../../util/Elements';
import { getMid } from '../../../layout/LayoutUtil';
 
import { SPACING } from '../GridSnapping';
 
var HIGH_PRIORITY = 2000;
 
 
export default function PasteBehavior(eventBus, gridSnapping) {
  CommandInterceptor.call(this, eventBus);
 
  this.preExecute('elements.paste', HIGH_PRIORITY, function(event) {
    var context = event.context,
        position = context.position,
        tree = context.tree;
 
    Iif (!tree[0]) {
      return;
    }
 
    var mid = getMid(getBBox(tree[0]));
 
    // snap paste position but maintain each element's grid offset
    position.x = gridSnapping.snapValue(position.x) + (mid.x % SPACING);
    position.y = gridSnapping.snapValue(position.y) + (mid.y % SPACING);
  });
}
 
PasteBehavior.$inject = [
  'eventBus',
  'gridSnapping'
];
 
inherits(PasteBehavior, CommandInterceptor);