BpmnKeyboardBindings.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import inherits from 'inherits-browser';
  2. import KeyboardBindings from 'diagram-js/lib/features/keyboard/KeyboardBindings';
  3. /**
  4. * @typedef {import('didi').Injector} Injector
  5. * @typedef {import('diagram-js/lib/features/editor-actions/EditorActions').default} EditorActions
  6. * @typedef {import('diagram-js/lib/features/keyboard/Keyboard').default} Keyboard
  7. */
  8. /**
  9. * BPMN 2.0 specific keyboard bindings.
  10. *
  11. * @param {Injector} injector
  12. */
  13. export default function BpmnKeyboardBindings(injector) {
  14. injector.invoke(KeyboardBindings, this);
  15. }
  16. inherits(BpmnKeyboardBindings, KeyboardBindings);
  17. BpmnKeyboardBindings.$inject = [
  18. 'injector'
  19. ];
  20. /**
  21. * Register available keyboard bindings.
  22. *
  23. * @param {Keyboard} keyboard
  24. * @param {EditorActions} editorActions
  25. */
  26. BpmnKeyboardBindings.prototype.registerBindings = function(keyboard, editorActions) {
  27. // inherit default bindings
  28. KeyboardBindings.prototype.registerBindings.call(this, keyboard, editorActions);
  29. /**
  30. * Add keyboard binding if respective editor action
  31. * is registered.
  32. *
  33. * @param {string} action name
  34. * @param {Function} fn that implements the key binding
  35. */
  36. function addListener(action, fn) {
  37. if (editorActions.isRegistered(action)) {
  38. keyboard.addListener(fn);
  39. }
  40. }
  41. // select all elements
  42. // CTRL + A
  43. addListener('selectElements', function(context) {
  44. var event = context.keyEvent;
  45. if (keyboard.isKey([ 'a', 'A' ], event) && keyboard.isCmd(event)) {
  46. editorActions.trigger('selectElements');
  47. return true;
  48. }
  49. });
  50. // search labels
  51. // CTRL + F
  52. addListener('find', function(context) {
  53. var event = context.keyEvent;
  54. if (keyboard.isKey([ 'f', 'F' ], event) && keyboard.isCmd(event)) {
  55. editorActions.trigger('find');
  56. return true;
  57. }
  58. });
  59. // activate space tool
  60. // S
  61. addListener('spaceTool', function(context) {
  62. var event = context.keyEvent;
  63. if (keyboard.hasModifier(event)) {
  64. return;
  65. }
  66. if (keyboard.isKey([ 's', 'S' ], event)) {
  67. editorActions.trigger('spaceTool');
  68. return true;
  69. }
  70. });
  71. // activate lasso tool
  72. // L
  73. addListener('lassoTool', function(context) {
  74. var event = context.keyEvent;
  75. if (keyboard.hasModifier(event)) {
  76. return;
  77. }
  78. if (keyboard.isKey([ 'l', 'L' ], event)) {
  79. editorActions.trigger('lassoTool');
  80. return true;
  81. }
  82. });
  83. // activate hand tool
  84. // H
  85. addListener('handTool', function(context) {
  86. var event = context.keyEvent;
  87. if (keyboard.hasModifier(event)) {
  88. return;
  89. }
  90. if (keyboard.isKey([ 'h', 'H' ], event)) {
  91. editorActions.trigger('handTool');
  92. return true;
  93. }
  94. });
  95. // activate global connect tool
  96. // C
  97. addListener('globalConnectTool', function(context) {
  98. var event = context.keyEvent;
  99. if (keyboard.hasModifier(event)) {
  100. return;
  101. }
  102. if (keyboard.isKey([ 'c', 'C' ], event)) {
  103. editorActions.trigger('globalConnectTool');
  104. return true;
  105. }
  106. });
  107. // activate direct editing
  108. // E
  109. addListener('directEditing', function(context) {
  110. var event = context.keyEvent;
  111. if (keyboard.hasModifier(event)) {
  112. return;
  113. }
  114. if (keyboard.isKey([ 'e', 'E' ], event)) {
  115. editorActions.trigger('directEditing');
  116. return true;
  117. }
  118. });
  119. // activate replace element
  120. // R
  121. addListener('replaceElement', function(context) {
  122. var event = context.keyEvent;
  123. if (keyboard.hasModifier(event)) {
  124. return;
  125. }
  126. if (keyboard.isKey([ 'r', 'R' ], event)) {
  127. editorActions.trigger('replaceElement', event);
  128. return true;
  129. }
  130. });
  131. };