BpmnEditorActions.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import inherits from 'inherits-browser';
  2. import EditorActions from 'diagram-js/lib/features/editor-actions/EditorActions';
  3. import { filter } from 'min-dash';
  4. import { is } from '../../util/ModelUtil';
  5. import {
  6. getBBox
  7. } from 'diagram-js/lib/util/Elements';
  8. /**
  9. * @typedef {import('didi').Injector} Injector
  10. */
  11. /**
  12. * Registers and executes BPMN specific editor actions.
  13. *
  14. * @param {Injector} injector
  15. */
  16. export default function BpmnEditorActions(injector) {
  17. injector.invoke(EditorActions, this);
  18. }
  19. inherits(BpmnEditorActions, EditorActions);
  20. BpmnEditorActions.$inject = [
  21. 'injector'
  22. ];
  23. /**
  24. * Register default actions.
  25. *
  26. * @param {Injector} injector
  27. */
  28. BpmnEditorActions.prototype._registerDefaultActions = function(injector) {
  29. // (0) invoke super method
  30. EditorActions.prototype._registerDefaultActions.call(this, injector);
  31. // (1) retrieve optional components to integrate with
  32. var canvas = injector.get('canvas', false);
  33. var elementRegistry = injector.get('elementRegistry', false);
  34. var selection = injector.get('selection', false);
  35. var spaceTool = injector.get('spaceTool', false);
  36. var lassoTool = injector.get('lassoTool', false);
  37. var handTool = injector.get('handTool', false);
  38. var globalConnect = injector.get('globalConnect', false);
  39. var distributeElements = injector.get('distributeElements', false);
  40. var alignElements = injector.get('alignElements', false);
  41. var directEditing = injector.get('directEditing', false);
  42. var searchPad = injector.get('searchPad', false);
  43. var modeling = injector.get('modeling', false);
  44. var contextPad = injector.get('contextPad', false);
  45. // (2) check components and register actions
  46. if (canvas && elementRegistry && selection) {
  47. this._registerAction('selectElements', function() {
  48. // select all elements except for the invisible
  49. // root element
  50. var rootElement = canvas.getRootElement();
  51. var elements = elementRegistry.filter(function(element) {
  52. return element !== rootElement;
  53. });
  54. selection.select(elements);
  55. return elements;
  56. });
  57. }
  58. if (spaceTool) {
  59. this._registerAction('spaceTool', function() {
  60. spaceTool.toggle();
  61. });
  62. }
  63. if (lassoTool) {
  64. this._registerAction('lassoTool', function() {
  65. lassoTool.toggle();
  66. });
  67. }
  68. if (handTool) {
  69. this._registerAction('handTool', function() {
  70. handTool.toggle();
  71. });
  72. }
  73. if (globalConnect) {
  74. this._registerAction('globalConnectTool', function() {
  75. globalConnect.toggle();
  76. });
  77. }
  78. if (selection && distributeElements) {
  79. this._registerAction('distributeElements', function(opts) {
  80. var currentSelection = selection.get(),
  81. type = opts.type;
  82. if (currentSelection.length) {
  83. distributeElements.trigger(currentSelection, type);
  84. }
  85. });
  86. }
  87. if (selection && alignElements) {
  88. this._registerAction('alignElements', function(opts) {
  89. var currentSelection = selection.get(),
  90. aligneableElements = [],
  91. type = opts.type;
  92. if (currentSelection.length) {
  93. aligneableElements = filter(currentSelection, function(element) {
  94. return !is(element, 'bpmn:Lane');
  95. });
  96. alignElements.trigger(aligneableElements, type);
  97. }
  98. });
  99. }
  100. if (selection && modeling) {
  101. this._registerAction('setColor', function(opts) {
  102. var currentSelection = selection.get();
  103. if (currentSelection.length) {
  104. modeling.setColor(currentSelection, opts);
  105. }
  106. });
  107. }
  108. if (selection && directEditing) {
  109. this._registerAction('directEditing', function() {
  110. var currentSelection = selection.get();
  111. if (currentSelection.length) {
  112. directEditing.activate(currentSelection[0]);
  113. }
  114. });
  115. }
  116. if (searchPad) {
  117. this._registerAction('find', function() {
  118. searchPad.toggle();
  119. });
  120. }
  121. if (canvas && modeling) {
  122. this._registerAction('moveToOrigin', function() {
  123. var rootElement = canvas.getRootElement(),
  124. boundingBox,
  125. elements;
  126. if (is(rootElement, 'bpmn:Collaboration')) {
  127. elements = elementRegistry.filter(function(element) {
  128. return is(element.parent, 'bpmn:Collaboration');
  129. });
  130. } else {
  131. elements = elementRegistry.filter(function(element) {
  132. return element !== rootElement && !is(element.parent, 'bpmn:SubProcess');
  133. });
  134. }
  135. boundingBox = getBBox(elements);
  136. modeling.moveElements(
  137. elements,
  138. { x: -boundingBox.x, y: -boundingBox.y },
  139. rootElement
  140. );
  141. });
  142. }
  143. if (selection && contextPad) {
  144. this._registerAction('replaceElement', function(event) {
  145. contextPad.triggerEntry('replace', 'click', event);
  146. });
  147. }
  148. };