From 645265ad7e4a47e80657c671068a027752d7504f Mon Sep 17 00:00:00 2001 From: Nico Rehwaldt Date: Sat, 27 Oct 2018 00:54:31 +0200 Subject: [PATCH] chore(editor-actions): make dependencies optional * Treat features as optional dependencies and register action only if feature exists * Explicitly add features to the Modeler and NavigatedViewer types --- lib/Modeler.js | 34 ++-- lib/NavigatedViewer.js | 2 + .../editor-actions/BpmnEditorActions.js | 148 ++++++++++++------ lib/features/editor-actions/index.js | 21 +-- 4 files changed, 123 insertions(+), 82 deletions(-) diff --git a/lib/Modeler.js b/lib/Modeler.js index 5845dfe4..931be60d 100644 --- a/lib/Modeler.js +++ b/lib/Modeler.js @@ -6,24 +6,29 @@ import Viewer from './Viewer'; import NavigatedViewer from './NavigatedViewer'; +import KeyboardMoveModule from 'diagram-js/lib/navigation/keyboard-move'; import MoveCanvasModule from 'diagram-js/lib/navigation/movecanvas'; import TouchModule from 'diagram-js/lib/navigation/touch'; import ZoomScrollModule from 'diagram-js/lib/navigation/zoomscroll'; +import AlignElementsModule from 'diagram-js/lib/features/align-elements'; +import AutoPlaceModule from './features/auto-place'; +import AutoResizeModule from './features/auto-resize'; import AutoScrollModule from 'diagram-js/lib/features/auto-scroll'; import BendpointsModule from 'diagram-js/lib/features/bendpoints'; -import MoveModule from 'diagram-js/lib/features/move'; -import ResizeModule from 'diagram-js/lib/features/resize'; -import AutoResizeModule from './features/auto-resize'; -import AutoPlaceModule from './features/auto-place'; -import EditorActionsModule from './features/editor-actions'; import ContextPadModule from './features/context-pad'; +import CopyPasteModule from 'diagram-js/lib/features/copy-paste'; +import DistributeElementsModule from './features/distribute-elements'; +import EditorActionsModule from './features/editor-actions'; import KeyboardModule from './features/keyboard'; import LabelEditingModule from './features/label-editing'; import ModelingModule from './features/modeling'; +import MoveModule from 'diagram-js/lib/features/move'; import PaletteModule from './features/palette'; import ReplacePreviewModule from './features/replace-preview'; +import ResizeModule from 'diagram-js/lib/features/resize'; import SnappingModule from './features/snapping'; +import SearchModule from './features/search'; var initialDiagram = @@ -187,6 +192,7 @@ Modeler.prototype._collectIds = function(definitions, context) { Modeler.prototype._interactionModules = [ // non-modeling components + KeyboardMoveModule, MoveCanvasModule, TouchModule, ZoomScrollModule @@ -194,20 +200,24 @@ Modeler.prototype._interactionModules = [ Modeler.prototype._modelingModules = [ // modeling components - AutoScrollModule, - BendpointsModule, - MoveModule, - ResizeModule, - AutoResizeModule, + AlignElementsModule, AutoPlaceModule, - EditorActionsModule, + AutoScrollModule, + AutoResizeModule, + BendpointsModule, ContextPadModule, + CopyPasteModule, + DistributeElementsModule, + EditorActionsModule, KeyboardModule, LabelEditingModule, ModelingModule, + MoveModule, PaletteModule, ReplacePreviewModule, - SnappingModule + ResizeModule, + SnappingModule, + SearchModule ]; diff --git a/lib/NavigatedViewer.js b/lib/NavigatedViewer.js index 1d799472..3e64fefc 100644 --- a/lib/NavigatedViewer.js +++ b/lib/NavigatedViewer.js @@ -2,6 +2,7 @@ import inherits from 'inherits'; import Viewer from './Viewer'; +import KeyboardMoveModule from 'diagram-js/lib/navigation/keyboard-move'; import MoveCanvasModule from 'diagram-js/lib/navigation/movecanvas'; import ZoomScrollModule from 'diagram-js/lib/navigation/zoomscroll'; @@ -17,6 +18,7 @@ export default function NavigatedViewer(options) { inherits(NavigatedViewer, Viewer); NavigatedViewer.prototype._navigationModules = [ + KeyboardMoveModule, MoveCanvasModule, ZoomScrollModule ]; diff --git a/lib/features/editor-actions/BpmnEditorActions.js b/lib/features/editor-actions/BpmnEditorActions.js index cb70ce41..d97c8126 100644 --- a/lib/features/editor-actions/BpmnEditorActions.js +++ b/lib/features/editor-actions/BpmnEditorActions.js @@ -13,18 +13,49 @@ import { /** * Registers and executes BPMN specific editor actions. + * + * @param {Injector} injector */ -export default function BpmnEditorActions( - injector, canvas, elementRegistry, - selection, spaceTool, lassoTool, - handTool, globalConnect, distributeElements, - alignElements, directEditing, searchPad, - modeling) { - +export default function BpmnEditorActions(injector) { injector.invoke(EditorActions, this); +} - this.register({ - selectElements: function() { +inherits(BpmnEditorActions, EditorActions); + +BpmnEditorActions.$inject = [ + 'injector' +]; + +/** + * Register default actions. + * + * @param {Injector} injector + */ +BpmnEditorActions.prototype._registerDefaultActions = function(injector) { + + // (0) invoke super method + + EditorActions.prototype._registerDefaultActions.call(this, injector); + + // (1) retrieve optional components to integrate with + + var canvas = injector.get('canvas', false); + var elementRegistry = injector.get('elementRegistry', false); + var selection = injector.get('selection', false); + var spaceTool = injector.get('spaceTool', false); + var lassoTool = injector.get('lassoTool', false); + var handTool = injector.get('handTool', false); + var globalConnect = injector.get('globalConnect', false); + var distributeElements = injector.get('distributeElements', false); + var alignElements = injector.get('alignElements', false); + var directEditing = injector.get('directEditing', false); + var searchPad = injector.get('searchPad', false); + var modeling = injector.get('modeling', false); + + // (2) check components and register actions + + if (canvas && elementRegistry && selection) { + this._registerAction('selectElements', function() { // select all elements except for the invisible // root element var rootElement = canvas.getRootElement(); @@ -36,28 +67,46 @@ export default function BpmnEditorActions( selection.select(elements); return elements; - }, - spaceTool: function() { + }); + } + + if (spaceTool) { + this._registerAction('spaceTool', function() { spaceTool.toggle(); - }, - lassoTool: function() { + }); + } + + if (lassoTool) { + this._registerAction('lassoTool', function() { lassoTool.toggle(); - }, - handTool: function() { + }); + } + + if (handTool) { + this._registerAction('handTool', function() { handTool.toggle(); - }, - globalConnectTool: function() { + }); + } + + if (globalConnect) { + this._registerAction('globalConnectTool', function() { globalConnect.toggle(); - }, - distributeElements: function(opts) { + }); + } + + if (selection && distributeElements) { + this._registerAction('distributeElements', function(opts) { var currentSelection = selection.get(), type = opts.type; if (currentSelection.length) { distributeElements.trigger(currentSelection, type); } - }, - alignElements: function(opts) { + }); + } + + if (selection && alignElements) { + this._registerAction('alignElements', function(opts) { var currentSelection = selection.get(), aligneableElements = [], type = opts.type; @@ -69,25 +118,37 @@ export default function BpmnEditorActions( alignElements.trigger(aligneableElements, type); } - }, - setColor: function(opts) { + }); + } + + if (selection && modeling) { + this._registerAction('setColor', function(opts) { var currentSelection = selection.get(); if (currentSelection.length) { modeling.setColor(currentSelection, opts); } - }, - directEditing: function() { + }); + } + + if (selection && directEditing) { + this._registerAction('directEditing', function() { var currentSelection = selection.get(); if (currentSelection.length) { directEditing.activate(currentSelection[0]); } - }, - find: function() { + }); + } + + if (searchPad) { + this._registerAction('find', function() { searchPad.toggle(); - }, - moveToOrigin: function() { + }); + } + + if (canvas && modeling) { + this._registerAction('moveToOrigin', function() { var rootElement = canvas.getRootElement(), boundingBox, elements; @@ -104,25 +165,12 @@ export default function BpmnEditorActions( boundingBox = getBBox(elements); - modeling.moveElements(elements, { x: -boundingBox.x, y: -boundingBox.y }, rootElement); - } - }); -} + modeling.moveElements( + elements, + { x: -boundingBox.x, y: -boundingBox.y }, + rootElement + ); + }); + } -inherits(BpmnEditorActions, EditorActions); - -BpmnEditorActions.$inject = [ - 'injector', - 'canvas', - 'elementRegistry', - 'selection', - 'spaceTool', - 'lassoTool', - 'handTool', - 'globalConnect', - 'distributeElements', - 'alignElements', - 'directEditing', - 'searchPad', - 'modeling' -]; \ No newline at end of file +}; \ No newline at end of file diff --git a/lib/features/editor-actions/index.js b/lib/features/editor-actions/index.js index 5d9fb7dc..bed7d7e3 100644 --- a/lib/features/editor-actions/index.js +++ b/lib/features/editor-actions/index.js @@ -1,29 +1,10 @@ -import AlignElementsModule from 'diagram-js/lib/features/align-elements'; import EditorActionsModule from 'diagram-js/lib/features/editor-actions'; -import HandToolModule from 'diagram-js/lib/features/hand-tool'; -import LassoToolModule from 'diagram-js/lib/features/lasso-tool'; -import SpaceToolModule from 'diagram-js/lib/features/space-tool'; -import GlobalConnectModule from 'diagram-js/lib/features/global-connect'; -import DirectEditingModule from 'diagram-js-direct-editing'; - -import CopyPasteModule from '../copy-paste'; -import DistributeElementsModule from '../distribute-elements'; -import SearchModule from '../search'; import BpmnEditorActions from './BpmnEditorActions'; export default { __depends__: [ - AlignElementsModule, - EditorActionsModule, - HandToolModule, - LassoToolModule, - SpaceToolModule, - DirectEditingModule, - GlobalConnectModule, - CopyPasteModule, - DistributeElementsModule, - SearchModule + EditorActionsModule ], editorActions: [ 'type', BpmnEditorActions ] };