BpmnKeyboardBindings.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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: any) {
  14. //@ts-ignore
  15. injector.invoke(KeyboardBindings, this);
  16. }
  17. inherits(BpmnKeyboardBindings, KeyboardBindings);
  18. BpmnKeyboardBindings.$inject = ['injector'];
  19. /**
  20. * Register available keyboard bindings.
  21. *
  22. * @param {Keyboard} keyboard
  23. * @param {EditorActions} editorActions
  24. */
  25. BpmnKeyboardBindings.prototype.registerBindings = function (keyboard: any, editorActions: any) {
  26. // inherit default bindings
  27. KeyboardBindings.prototype.registerBindings.call(this, keyboard, editorActions);
  28. /**
  29. * Add keyboard binding if respective editor action
  30. * is registered.
  31. *
  32. * @param {string} action name
  33. * @param {Function} fn that implements the key binding
  34. */
  35. function addListener(action: string, fn: Function) {
  36. if (editorActions.isRegistered(action)) {
  37. keyboard.addListener(fn);
  38. }
  39. }
  40. addListener('find', function (context: any) {
  41. var event = context.keyEvent;
  42. if (keyboard.isKey(['f', 'F'], event) && keyboard.isCmd(event)) {
  43. editorActions.trigger('find');
  44. return true;
  45. }
  46. });
  47. // activate space tool
  48. // S
  49. addListener('spaceTool', function (context: any) {
  50. var event = context.keyEvent;
  51. if (keyboard.hasModifier(event)) {
  52. return;
  53. }
  54. if (keyboard.isKey(['s', 'S'], event)) {
  55. editorActions.trigger('spaceTool');
  56. return true;
  57. }
  58. });
  59. // activate lasso tool
  60. // L
  61. addListener('lassoTool', function (context: any) {
  62. var event = context.keyEvent;
  63. if (keyboard.hasModifier(event)) {
  64. return;
  65. }
  66. if (keyboard.isKey(['l', 'L'], event)) {
  67. editorActions.trigger('lassoTool');
  68. return true;
  69. }
  70. });
  71. // activate hand tool
  72. // H
  73. addListener('handTool', function (context: any) {
  74. var event = context.keyEvent;
  75. if (keyboard.hasModifier(event)) {
  76. return;
  77. }
  78. if (keyboard.isKey(['h', 'H'], event)) {
  79. editorActions.trigger('handTool');
  80. return true;
  81. }
  82. });
  83. // activate global connect tool
  84. // C
  85. addListener('globalConnectTool', function (context: any) {
  86. var event = context.keyEvent;
  87. if (keyboard.hasModifier(event)) {
  88. return;
  89. }
  90. if (keyboard.isKey(['c', 'C'], event)) {
  91. editorActions.trigger('globalConnectTool');
  92. return true;
  93. }
  94. });
  95. // activate direct editing
  96. // E
  97. addListener('directEditing', function (context: any) {
  98. var event = context.keyEvent;
  99. if (keyboard.hasModifier(event)) {
  100. return;
  101. }
  102. if (keyboard.isKey(['e', 'E'], event)) {
  103. editorActions.trigger('directEditing');
  104. return true;
  105. }
  106. });
  107. // activate replace element
  108. // R
  109. addListener('replaceElement', function (context: any) {
  110. var event = context.keyEvent;
  111. if (keyboard.hasModifier(event)) {
  112. return;
  113. }
  114. if (keyboard.isKey(['r', 'R'], event)) {
  115. editorActions.trigger('replaceElement', event);
  116. return true;
  117. }
  118. });
  119. };