index.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // @ts-nocheck
  2. import CommandStack from 'diagram-js/lib/command/CommandStack';
  3. import { bpmnGroup } from '../config/variableName';
  4. class CustomCommandStack extends CommandStack {
  5. constructor(eventBus, modeling) {
  6. super(eventBus, modeling);
  7. }
  8. }
  9. let sameId = '';
  10. let isVerticalSameId = false;
  11. let isHorizontalSameId = false;
  12. CommandStack.prototype._internalExecute = function (action, redo) {
  13. const command = action.command;
  14. const context = action.context;
  15. const handler = this._getHandler(command);
  16. if (!handler) throw new Error('no command handler registered for <' + command + '>');
  17. this._pushAction(action);
  18. if (!redo) {
  19. this._fire(command, 'preExecute', action);
  20. if (handler.preExecute) handler.preExecute(context);
  21. this._fire(command, 'preExecuted', action);
  22. }
  23. // guard against illegal nested command stack invocations
  24. this._atomicDo(() => {
  25. this._fire(command, 'execute', action);
  26. if (handler.execute) this._markDirty(handler.execute(context));
  27. if (action.command == 'element.updateProperties' || sameId == action.id) {
  28. sameId = action.id;
  29. } else {
  30. if (action.id != 'updateProperties') this._executedAction(action, redo);
  31. }
  32. this._fire(command, 'executed', action);
  33. });
  34. if (!redo) {
  35. this._fire(command, 'postExecute', action);
  36. if (handler.postExecute) handler.postExecute(context);
  37. this._fire(command, 'postExecuted', action);
  38. }
  39. this._popAction(action);
  40. };
  41. CommandStack.prototype._pushAction = function (action, isSameId = false) {
  42. const execution = this._currentExecution;
  43. const actions = execution.actions;
  44. const baseAction = actions[0];
  45. if (execution.atomic) throw new Error('illegal invocation in <execute> or <revert> phase (action: ' + action.command + ')');
  46. if (action.command == 'element.updateProperties') action.id = 'updateProperties';
  47. if (!action.id) action.id = (baseAction && baseAction.id) || this._createId(isSameId);
  48. if (isVerticalSameId) action.type = 'beautification.vertical';
  49. if (isHorizontalSameId) action.type = 'beautification.horizontal';
  50. actions.push(action);
  51. };
  52. CommandStack.prototype._createId = function (isSameId = false) {
  53. if (isSameId) {
  54. this._uid = this._uid - 1;
  55. }
  56. return isHorizontalSameId || isVerticalSameId ? this._uid : this._uid++;
  57. };
  58. CommandStack.prototype.execute = function (command, context) {
  59. let isSameId = false;
  60. if (!command) throw new Error('command required');
  61. // 设置一个开始和一个结束状态 如果遇到遇到开始 则全局设置该值为true 后续的入栈id设置成相同id
  62. if (command === 'vertical.action.same.id.start') return (isVerticalSameId = true);
  63. if (command === 'vertical.action.same.id.end') {
  64. this._uid++;
  65. return (isVerticalSameId = false);
  66. }
  67. if (command === 'horizontal.action.same.id.start') return (isHorizontalSameId = true);
  68. if (command === 'horizontal.action.same.id.end') {
  69. this._uid++;
  70. return (isHorizontalSameId = false);
  71. }
  72. if (command === 'shape.create' && context.shape?.type === bpmnGroup) {
  73. isSameId = true;
  74. }
  75. if (command === 'shape.resize') {
  76. if (!(isVerticalSameId || isHorizontalSameId)) isSameId = true;
  77. }
  78. this._currentExecution.trigger = 'execute';
  79. const action = { command: command, context: context };
  80. this._pushAction(action, isSameId);
  81. this._internalExecute(action);
  82. this._popAction(action);
  83. };
  84. CommandStack.prototype.undo = function () {
  85. let action = this._getUndoAction(),
  86. next;
  87. if (action) {
  88. this._currentExecution.trigger = 'undo';
  89. this._pushAction(action);
  90. while (action) {
  91. this._internalUndo(action);
  92. next = this._getUndoAction();
  93. if (!next || next.id !== action.id) {
  94. break;
  95. }
  96. action = next;
  97. }
  98. if (action.command === 'shape.resize' || (action.command === 'shape.create' && action.context.shape?.type === bpmnGroup)) {
  99. let id = action.id - 1;
  100. while (action) {
  101. this._internalUndo(action);
  102. next = this._getUndoAction();
  103. if (!next || next.id !== id) break;
  104. action = next;
  105. }
  106. }
  107. this._popAction();
  108. }
  109. };
  110. CommandStack.prototype.redo = function () {
  111. let action = this._getRedoAction(),
  112. resizeNext,
  113. next;
  114. if (action) {
  115. this._currentExecution.trigger = 'redo';
  116. this._pushAction(action);
  117. while (action) {
  118. this._internalExecute(action, true);
  119. next = this._getRedoAction();
  120. if (!next || next.id !== action.id) {
  121. if (next?.command === 'shape.resize') {
  122. resizeNext = next;
  123. }
  124. break;
  125. }
  126. action = next;
  127. }
  128. if (resizeNext) {
  129. let id = resizeNext.id + 1;
  130. while (resizeNext) {
  131. this._internalExecute(resizeNext, true);
  132. next = this._getRedoAction();
  133. if (!next || next.id !== id) {
  134. break;
  135. }
  136. action = next;
  137. }
  138. }
  139. this._popAction();
  140. }
  141. };
  142. export default CustomCommandStack;