all files / lib/features/keyboard/ DiagramKeyBindings.js

54.55% Statements 30/55
0% Branches 0/21
11.11% Functions 1/9
54.55% Lines 30/55
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                              20× 20× 20× 20×   20× 20×   20× 20×   20×   20× 20× 20×   20× 20× 20× 20×                                                                                                                                                            
import {
  isCmd,
  isShift,
  MoveCanvasFactory
} from './helpers';
 
var zKeys = [ 'Z', 'z' ];
 
var copyKeys = [ 'C', 'c' ];
var pasteKeys = [ 'V', 'v' ];
 
var undoKeys = [ 'Z', 'z' ];
var redoKeys = zKeys.concat('Y', 'y');
 
var removeSelectionKeys = [ 'Delete' ];
 
var zoomDefaultKeys = [ '0' ];
var zoomInKeys = [ '+', 'Add' ];
var zoomOutKeys = [ '-', 'Subtract' ];
 
var moveCanvasLeftKeys = [ 'ArrowLeft', 'Left' ];
var moveCanvasUpKeys = [ 'ArrowUp', 'Up' ];
var moveCanvasRightKeys = [ 'ArrowRight', 'Right' ];
var moveCanvasDownKeys = [ 'ArrowDown', 'Down' ];
 
 
export default function DiagramKeyBindings(keyboard, config, editorActions) {
 
  var moveCanvasLeft = MoveCanvasFactory('left', config, editorActions);
  var moveCanvasUp = MoveCanvasFactory('up', config, editorActions);
  var moveCanvasRight = MoveCanvasFactory('right', config, editorActions);
  var moveCanvasDown = MoveCanvasFactory('down', config, editorActions);
 
  keyboard.addListener(copyKeys, copy);
  keyboard.addListener(pasteKeys, paste);
 
  keyboard.addListener(undoKeys, undo);
  keyboard.addListener(redoKeys, redo);
 
  keyboard.addListener(removeSelectionKeys, removeSelection);
 
  keyboard.addListener(zoomDefaultKeys, zoomDefault);
  keyboard.addListener(zoomInKeys, zoomIn);
  keyboard.addListener(zoomOutKeys, zoomOut);
 
  keyboard.addListener(moveCanvasLeftKeys, moveCanvasLeft);
  keyboard.addListener(moveCanvasUpKeys, moveCanvasUp);
  keyboard.addListener(moveCanvasRightKeys, moveCanvasRight);
  keyboard.addListener(moveCanvasDownKeys, moveCanvasDown);
 
  function copy(context) {
    if (isCmd(context.event)) {
      editorActions.trigger('copy');
 
      return true;
    }
  }
 
  function paste(context) {
 
    if (isCmd(context.event)) {
      editorActions.trigger('paste');
 
      return true;
    }
  }
 
  function redo(context) {
    var key = context.key;
 
    if (isCmd(context.event) && (!zKeys.indexOf(key) > -1 || isShift(context.event))) {
      editorActions.trigger('redo');
 
      return true;
    }
  }
 
  function removeSelection(context) {
 
    if (isCmd(context.event)) {
      editorActions.trigger('removeSelection');
 
      return true;
    }
  }
 
  function undo(context) {
 
    if (isCmd(context.event) && !isShift(context.event)) {
      editorActions.trigger('undo');
 
      return true;
    }
  }
 
  function zoomDefault(context) {
 
    if (isCmd(context.event)) {
      editorActions.trigger('zoom', { value: 1 });
 
      return true;
    }
  }
 
  function zoomIn(context) {
 
    if (isCmd(context.event)) {
      editorActions.trigger('stepZoom', { value: 1 });
 
      return true;
    }
  }
 
  function zoomOut(context) {
 
    if (isCmd(context.event)) {
      editorActions.trigger('stepZoom', { value: -1 });
 
      return true;
    }
  }
}
 
DiagramKeyBindings.$inject = [
  'keyboard',
  'config.keyboard',
  'editorActions'
];