# all files / diagram-js/lib/features/keyboard/ KeyboardBindings.js

59.18% Statements 29/49
54.29% Branches 19/35
83.33% Functions 10/12
59.18% Lines 29/49

    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
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164

    1×

    71×

    71×

    67×

    67×

    1×

    1×

    536×
    505×

    67×

    12×

    12×

    67×

    12×

    12×

    67×

    12×

    12×

    67×

    12×

    12×

    67×

    67×

    67×

    12×

    12×

    67×

    12×

    12×

     import {
      isCmd,
      isKey,
      isShift
    } from './KeyboardUtil';

    var LOW_PRIORITY = 500;

    /**
     * Adds default keyboard bindings.
     *
     * This does not pull in any features will bind only actions that
     * have previously been registered against the editorActions component.
     *
     * @param {EventBus} eventBus
     * @param {Keyboard} keyboard
     */
    export default function KeyboardBindings(eventBus, keyboard) {

      var self = this;

      eventBus.on('editorActions.init', LOW_PRIORITY, function(event) {

        var editorActions = event.editorActions;

        self.registerBindings(keyboard, editorActions);
      });
    }

    KeyboardBindings.$inject = [
      'eventBus',
      'keyboard'
    ];

    /**
     * Register available keyboard bindings.
     *
     * @param {Keyboard} keyboard
     * @param {EditorActions} editorActions
     */
    KeyboardBindings.prototype.registerBindings = function(keyboard, editorActions) {

      /**
       * Add keyboard binding if respective editor action
       * is registered.
       *
       * @param {String} action name
       * @param {Function} fn that implements the key binding
       */
      function addListener(action, fn) {

        if (editorActions.isRegistered(action)) {
          keyboard.addListener(fn);
        }
      }

      // undo
      // (CTRL|CMD) + Z
      addListener('undo', function(context) {

        var event = context.keyEvent;

        Iif (isCmd(event) && !isShift(event) && isKey(['z', 'Z'], event)) {
          editorActions.trigger('undo');

          return true;
        }
      });

      // redo
      // CTRL + Y
      // CMD + SHIFT + Z
      addListener('redo', function(context) {

        var event = context.keyEvent;

        Iif (isCmd(event) && (isKey(['y', 'Y'], event) || (isKey(['z', 'Z'], event) && isShift(event)))) {
          editorActions.trigger('redo');

          return true;
        }
      });

      // copy
      // CTRL/CMD + C
      addListener('copy', function(context) {

        var event = context.keyEvent;

        Iif (isCmd(event) && isKey(['c', 'C'], event)) {
          editorActions.trigger('copy');

          return true;
        }
      });

      // paste
      // CTRL/CMD + V
      addListener('paste', function(context) {

        var event = context.keyEvent;

        Iif (isCmd(event) && isKey(['v', 'V'], event)) {
          editorActions.trigger('paste');

          return true;
        }
      });

      // zoom in one step
      // CTRL/CMD + +
      addListener('stepZoom', function(context) {

        var event = context.keyEvent;

        if (isKey([ '+', 'Add' ], event) && isCmd(event)) {
          editorActions.trigger('stepZoom', { value: 1 });

          return true;
        }
      });

      // zoom out one step
      // CTRL + -
      addListener('stepZoom', function(context) {

        var event = context.keyEvent;

        if (isKey([ '-', 'Subtract' ], event) && isCmd(event)) {
          editorActions.trigger('stepZoom', { value: -1 });

          return true;
        }
      });

      // zoom to the default level
      // CTRL + 0
      addListener('zoom', function(context) {

        var event = context.keyEvent;

        Iif (isKey('0', event) && isCmd(event)) {
          editorActions.trigger('zoom', { value: 1 });

          return true;
        }
      });

      // delete selected element
      // DEL
      addListener('removeSelection', function(context) {

        var event = context.keyEvent;

        Iif (isKey([ 'Delete', 'Del' ], event)) {
          editorActions.trigger('removeSelection');

          return true;
        }
      });
    };