123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- import {
- isArray,
- uniqueBy
- } from "./chunk-4AK4GF4H.js";
- import "./chunk-2LSFTFF7.js";
- // node_modules/.pnpm/diagram-js@11.9.1/node_modules/diagram-js/lib/command/CommandStack.js
- function CommandStack(eventBus, injector) {
- this._handlerMap = {};
- this._stack = [];
- this._stackIdx = -1;
- this._currentExecution = {
- actions: [],
- dirty: [],
- trigger: null
- };
- this._injector = injector;
- this._eventBus = eventBus;
- this._uid = 1;
- eventBus.on([
- "diagram.destroy",
- "diagram.clear"
- ], function() {
- this.clear(false);
- }, this);
- }
- CommandStack.$inject = ["eventBus", "injector"];
- CommandStack.prototype.execute = function(command, context) {
- if (!command) {
- throw new Error("command required");
- }
- this._currentExecution.trigger = "execute";
- const action = { command, context };
- this._pushAction(action);
- this._internalExecute(action);
- this._popAction(action);
- };
- CommandStack.prototype.canExecute = function(command, context) {
- const action = { command, context };
- const handler = this._getHandler(command);
- let result = this._fire(command, "canExecute", action);
- if (result === void 0) {
- if (!handler) {
- return false;
- }
- if (handler.canExecute) {
- result = handler.canExecute(context);
- }
- }
- return result;
- };
- CommandStack.prototype.clear = function(emit) {
- this._stack.length = 0;
- this._stackIdx = -1;
- if (emit !== false) {
- this._fire("changed", { trigger: "clear" });
- }
- };
- CommandStack.prototype.undo = function() {
- let action = this._getUndoAction(), next;
- if (action) {
- this._currentExecution.trigger = "undo";
- this._pushAction(action);
- while (action) {
- this._internalUndo(action);
- next = this._getUndoAction();
- if (!next || next.id !== action.id) {
- break;
- }
- action = next;
- }
- this._popAction();
- }
- };
- CommandStack.prototype.redo = function() {
- let action = this._getRedoAction(), next;
- if (action) {
- this._currentExecution.trigger = "redo";
- this._pushAction(action);
- while (action) {
- this._internalExecute(action, true);
- next = this._getRedoAction();
- if (!next || next.id !== action.id) {
- break;
- }
- action = next;
- }
- this._popAction();
- }
- };
- CommandStack.prototype.register = function(command, handler) {
- this._setHandler(command, handler);
- };
- CommandStack.prototype.registerHandler = function(command, handlerCls) {
- if (!command || !handlerCls) {
- throw new Error("command and handlerCls must be defined");
- }
- const handler = this._injector.instantiate(handlerCls);
- this.register(command, handler);
- };
- CommandStack.prototype.canUndo = function() {
- return !!this._getUndoAction();
- };
- CommandStack.prototype.canRedo = function() {
- return !!this._getRedoAction();
- };
- CommandStack.prototype._getRedoAction = function() {
- return this._stack[this._stackIdx + 1];
- };
- CommandStack.prototype._getUndoAction = function() {
- return this._stack[this._stackIdx];
- };
- CommandStack.prototype._internalUndo = function(action) {
- const command = action.command, context = action.context;
- const handler = this._getHandler(command);
- this._atomicDo(() => {
- this._fire(command, "revert", action);
- if (handler.revert) {
- this._markDirty(handler.revert(context));
- }
- this._revertedAction(action);
- this._fire(command, "reverted", action);
- });
- };
- CommandStack.prototype._fire = function(command, qualifier, event) {
- if (arguments.length < 3) {
- event = qualifier;
- qualifier = null;
- }
- const names = qualifier ? [command + "." + qualifier, qualifier] : [command];
- let result;
- event = this._eventBus.createEvent(event);
- for (const name of names) {
- result = this._eventBus.fire("commandStack." + name, event);
- if (event.cancelBubble) {
- break;
- }
- }
- return result;
- };
- CommandStack.prototype._createId = function() {
- return this._uid++;
- };
- CommandStack.prototype._atomicDo = function(fn) {
- const execution = this._currentExecution;
- execution.atomic = true;
- try {
- fn();
- } finally {
- execution.atomic = false;
- }
- };
- CommandStack.prototype._internalExecute = function(action, redo) {
- const command = action.command, context = action.context;
- const handler = this._getHandler(command);
- if (!handler) {
- throw new Error("no command handler registered for <" + command + ">");
- }
- this._pushAction(action);
- if (!redo) {
- this._fire(command, "preExecute", action);
- if (handler.preExecute) {
- handler.preExecute(context);
- }
- this._fire(command, "preExecuted", action);
- }
- this._atomicDo(() => {
- this._fire(command, "execute", action);
- if (handler.execute) {
- this._markDirty(handler.execute(context));
- }
- this._executedAction(action, redo);
- this._fire(command, "executed", action);
- });
- if (!redo) {
- this._fire(command, "postExecute", action);
- if (handler.postExecute) {
- handler.postExecute(context);
- }
- this._fire(command, "postExecuted", action);
- }
- this._popAction(action);
- };
- CommandStack.prototype._pushAction = function(action) {
- const execution = this._currentExecution, actions = execution.actions;
- const baseAction = actions[0];
- if (execution.atomic) {
- throw new Error("illegal invocation in <execute> or <revert> phase (action: " + action.command + ")");
- }
- if (!action.id) {
- action.id = baseAction && baseAction.id || this._createId();
- }
- actions.push(action);
- };
- CommandStack.prototype._popAction = function() {
- const execution = this._currentExecution, trigger = execution.trigger, actions = execution.actions, dirty = execution.dirty;
- actions.pop();
- if (!actions.length) {
- this._eventBus.fire("elements.changed", { elements: uniqueBy("id", dirty.reverse()) });
- dirty.length = 0;
- this._fire("changed", { trigger });
- execution.trigger = null;
- }
- };
- CommandStack.prototype._markDirty = function(elements) {
- const execution = this._currentExecution;
- if (!elements) {
- return;
- }
- elements = isArray(elements) ? elements : [elements];
- execution.dirty = execution.dirty.concat(elements);
- };
- CommandStack.prototype._executedAction = function(action, redo) {
- const stackIdx = ++this._stackIdx;
- if (!redo) {
- this._stack.splice(stackIdx, this._stack.length, action);
- }
- };
- CommandStack.prototype._revertedAction = function(action) {
- this._stackIdx--;
- };
- CommandStack.prototype._getHandler = function(command) {
- return this._handlerMap[command];
- };
- CommandStack.prototype._setHandler = function(command, handler) {
- if (!command || !handler) {
- throw new Error("command and handler required");
- }
- if (this._handlerMap[command]) {
- throw new Error("overriding handler for command <" + command + ">");
- }
- this._handlerMap[command] = handler;
- };
- export {
- CommandStack as default
- };
- //# sourceMappingURL=diagram-js_lib_command_CommandStack.js.map
|