diagram-js_lib_command_CommandStack.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import {
  2. isArray,
  3. uniqueBy
  4. } from "./chunk-4AK4GF4H.js";
  5. import "./chunk-2LSFTFF7.js";
  6. // node_modules/.pnpm/diagram-js@11.9.1/node_modules/diagram-js/lib/command/CommandStack.js
  7. function CommandStack(eventBus, injector) {
  8. this._handlerMap = {};
  9. this._stack = [];
  10. this._stackIdx = -1;
  11. this._currentExecution = {
  12. actions: [],
  13. dirty: [],
  14. trigger: null
  15. };
  16. this._injector = injector;
  17. this._eventBus = eventBus;
  18. this._uid = 1;
  19. eventBus.on([
  20. "diagram.destroy",
  21. "diagram.clear"
  22. ], function() {
  23. this.clear(false);
  24. }, this);
  25. }
  26. CommandStack.$inject = ["eventBus", "injector"];
  27. CommandStack.prototype.execute = function(command, context) {
  28. if (!command) {
  29. throw new Error("command required");
  30. }
  31. this._currentExecution.trigger = "execute";
  32. const action = { command, context };
  33. this._pushAction(action);
  34. this._internalExecute(action);
  35. this._popAction(action);
  36. };
  37. CommandStack.prototype.canExecute = function(command, context) {
  38. const action = { command, context };
  39. const handler = this._getHandler(command);
  40. let result = this._fire(command, "canExecute", action);
  41. if (result === void 0) {
  42. if (!handler) {
  43. return false;
  44. }
  45. if (handler.canExecute) {
  46. result = handler.canExecute(context);
  47. }
  48. }
  49. return result;
  50. };
  51. CommandStack.prototype.clear = function(emit) {
  52. this._stack.length = 0;
  53. this._stackIdx = -1;
  54. if (emit !== false) {
  55. this._fire("changed", { trigger: "clear" });
  56. }
  57. };
  58. CommandStack.prototype.undo = function() {
  59. let action = this._getUndoAction(), next;
  60. if (action) {
  61. this._currentExecution.trigger = "undo";
  62. this._pushAction(action);
  63. while (action) {
  64. this._internalUndo(action);
  65. next = this._getUndoAction();
  66. if (!next || next.id !== action.id) {
  67. break;
  68. }
  69. action = next;
  70. }
  71. this._popAction();
  72. }
  73. };
  74. CommandStack.prototype.redo = function() {
  75. let action = this._getRedoAction(), next;
  76. if (action) {
  77. this._currentExecution.trigger = "redo";
  78. this._pushAction(action);
  79. while (action) {
  80. this._internalExecute(action, true);
  81. next = this._getRedoAction();
  82. if (!next || next.id !== action.id) {
  83. break;
  84. }
  85. action = next;
  86. }
  87. this._popAction();
  88. }
  89. };
  90. CommandStack.prototype.register = function(command, handler) {
  91. this._setHandler(command, handler);
  92. };
  93. CommandStack.prototype.registerHandler = function(command, handlerCls) {
  94. if (!command || !handlerCls) {
  95. throw new Error("command and handlerCls must be defined");
  96. }
  97. const handler = this._injector.instantiate(handlerCls);
  98. this.register(command, handler);
  99. };
  100. CommandStack.prototype.canUndo = function() {
  101. return !!this._getUndoAction();
  102. };
  103. CommandStack.prototype.canRedo = function() {
  104. return !!this._getRedoAction();
  105. };
  106. CommandStack.prototype._getRedoAction = function() {
  107. return this._stack[this._stackIdx + 1];
  108. };
  109. CommandStack.prototype._getUndoAction = function() {
  110. return this._stack[this._stackIdx];
  111. };
  112. CommandStack.prototype._internalUndo = function(action) {
  113. const command = action.command, context = action.context;
  114. const handler = this._getHandler(command);
  115. this._atomicDo(() => {
  116. this._fire(command, "revert", action);
  117. if (handler.revert) {
  118. this._markDirty(handler.revert(context));
  119. }
  120. this._revertedAction(action);
  121. this._fire(command, "reverted", action);
  122. });
  123. };
  124. CommandStack.prototype._fire = function(command, qualifier, event) {
  125. if (arguments.length < 3) {
  126. event = qualifier;
  127. qualifier = null;
  128. }
  129. const names = qualifier ? [command + "." + qualifier, qualifier] : [command];
  130. let result;
  131. event = this._eventBus.createEvent(event);
  132. for (const name of names) {
  133. result = this._eventBus.fire("commandStack." + name, event);
  134. if (event.cancelBubble) {
  135. break;
  136. }
  137. }
  138. return result;
  139. };
  140. CommandStack.prototype._createId = function() {
  141. return this._uid++;
  142. };
  143. CommandStack.prototype._atomicDo = function(fn) {
  144. const execution = this._currentExecution;
  145. execution.atomic = true;
  146. try {
  147. fn();
  148. } finally {
  149. execution.atomic = false;
  150. }
  151. };
  152. CommandStack.prototype._internalExecute = function(action, redo) {
  153. const command = action.command, context = action.context;
  154. const handler = this._getHandler(command);
  155. if (!handler) {
  156. throw new Error("no command handler registered for <" + command + ">");
  157. }
  158. this._pushAction(action);
  159. if (!redo) {
  160. this._fire(command, "preExecute", action);
  161. if (handler.preExecute) {
  162. handler.preExecute(context);
  163. }
  164. this._fire(command, "preExecuted", action);
  165. }
  166. this._atomicDo(() => {
  167. this._fire(command, "execute", action);
  168. if (handler.execute) {
  169. this._markDirty(handler.execute(context));
  170. }
  171. this._executedAction(action, redo);
  172. this._fire(command, "executed", action);
  173. });
  174. if (!redo) {
  175. this._fire(command, "postExecute", action);
  176. if (handler.postExecute) {
  177. handler.postExecute(context);
  178. }
  179. this._fire(command, "postExecuted", action);
  180. }
  181. this._popAction(action);
  182. };
  183. CommandStack.prototype._pushAction = function(action) {
  184. const execution = this._currentExecution, actions = execution.actions;
  185. const baseAction = actions[0];
  186. if (execution.atomic) {
  187. throw new Error("illegal invocation in <execute> or <revert> phase (action: " + action.command + ")");
  188. }
  189. if (!action.id) {
  190. action.id = baseAction && baseAction.id || this._createId();
  191. }
  192. actions.push(action);
  193. };
  194. CommandStack.prototype._popAction = function() {
  195. const execution = this._currentExecution, trigger = execution.trigger, actions = execution.actions, dirty = execution.dirty;
  196. actions.pop();
  197. if (!actions.length) {
  198. this._eventBus.fire("elements.changed", { elements: uniqueBy("id", dirty.reverse()) });
  199. dirty.length = 0;
  200. this._fire("changed", { trigger });
  201. execution.trigger = null;
  202. }
  203. };
  204. CommandStack.prototype._markDirty = function(elements) {
  205. const execution = this._currentExecution;
  206. if (!elements) {
  207. return;
  208. }
  209. elements = isArray(elements) ? elements : [elements];
  210. execution.dirty = execution.dirty.concat(elements);
  211. };
  212. CommandStack.prototype._executedAction = function(action, redo) {
  213. const stackIdx = ++this._stackIdx;
  214. if (!redo) {
  215. this._stack.splice(stackIdx, this._stack.length, action);
  216. }
  217. };
  218. CommandStack.prototype._revertedAction = function(action) {
  219. this._stackIdx--;
  220. };
  221. CommandStack.prototype._getHandler = function(command) {
  222. return this._handlerMap[command];
  223. };
  224. CommandStack.prototype._setHandler = function(command, handler) {
  225. if (!command || !handler) {
  226. throw new Error("command and handler required");
  227. }
  228. if (this._handlerMap[command]) {
  229. throw new Error("overriding handler for command <" + command + ">");
  230. }
  231. this._handlerMap[command] = handler;
  232. };
  233. export {
  234. CommandStack as default
  235. };
  236. //# sourceMappingURL=diagram-js_lib_command_CommandStack.js.map