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

38.46% Statements 5/13
0% Branches 0/2
50% Functions 1/2
38.46% Lines 5/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

    1×

    74×

    74×

    1×

    1×import inherits from 'inherits';

    import CommandInterceptor from '../../../command/CommandInterceptor';

    import { getBBox } from '../../../util/Elements';
    import { getMid } from '../../../layout/LayoutUtil';

    import { SPACING } from '../GridUtil';

    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;

        if (!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);