# all files / Github/diagram-js/lib/features/keyboard-move-selection/ KeyboardMoveSelection.js

40.63% Statements 13/32
20% Branches 2/10
14.29% Functions 1/7
40.63% Lines 13/32

    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
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133

    1×

    1×

    1×
    1×
    1×
    1×

    1×

    1×

    53×

    53×

    53×

    53×

    1×

     import {
      assign
    } from 'min-dash';

    var DEFAULT_CONFIG = {
      moveSpeed: 1,
      moveSpeedAccelerated: 10
    };

    var HIGHER_PRIORITY = 1500;

    var LEFT = 'left';
    var UP = 'up';
    var RIGHT = 'right';
    var DOWN = 'down';

    var KEY_TO_DIRECTION = {
      ArrowLeft: LEFT,
      Left: LEFT,
      ArrowUp: UP,
      Up: UP,
      ArrowRight: RIGHT,
      Right: RIGHT,
      ArrowDown: DOWN,
      Down: DOWN
    };

    var DIRECTIONS_DELTA = {
      left: function(speed) {
        return {
          x: -speed,
          y: 0
        };
      },
      up: function(speed) {
        return {
          x: 0,
          y: -speed
        };
      },
      right: function(speed) {
        return {
          x: speed,
          y: 0
        };
      },
      down: function(speed) {
        return {
          x: 0,
          y: speed
        };
      }
    };

    /**
     * Enables to move selection with keyboard arrows.
     * Use with Shift for modified speed (default=1, with Shift=10).
     * Pressed Cmd/Ctrl turns the feature off.
     *
     * @param {Object} config
     * @param {Number} [config.moveSpeed=1]
     * @param {Number} [config.moveSpeedAccelerated=10]
     * @param {Keyboard} keyboard
     * @param {Modeling} modeling
     * @param {Selection} selection
     */
    export default function KeyboardMoveSelection(
        config, keyboard,
        modeling, selection
    ) {

      var self = this;

      this._config = assign({}, DEFAULT_CONFIG, config || {});

      keyboard.addListener(HIGHER_PRIORITY, function(event) {

        var keyEvent = event.keyEvent;

        var direction = KEY_TO_DIRECTION[keyEvent.key];

        if (!direction) {
          return;
        }

        if (keyboard.isCmd(keyEvent)) {
          return;
        }

        var accelerated = keyboard.isShift(keyEvent);

        self.moveSelection(direction, accelerated);

        return true;
      });

      /**
       * Move selected elements in the given direction,
       * optionally specifying accelerated movement.
       *
       * @param {String} direction
       * @param {Boolean} [accelerated=false]
       */
      this.moveSelection = function(direction, accelerated) {

        var selectedElements = selection.get();

        if (!selectedElements.length) {
          return;
        }

        var speed = this._config[
          accelerated ?
            'moveSpeedAccelerated' :
            'moveSpeed'
        ];

        var delta = DIRECTIONS_DELTA[direction](speed);

        modeling.moveElements(selectedElements, delta);
      };

    }

    KeyboardMoveSelection.$inject = [
      'config.keyboardMoveSelection',
      'keyboard',
      'modeling',
      'selection'
    ];