From b762015ec5a5ff1161cf45ca17f72848a37cb152 Mon Sep 17 00:00:00 2001 From: Philipp Fromme Date: Thu, 18 Apr 2019 13:26:27 +0200 Subject: [PATCH] feat(grid-snapping): integrate space tool Related to camunda/camunda-modeler#1344 Related to camunda/camunda-modeler#1348 --- .../behavior/SpaceToolBehavior.js | 34 ++++++++++++ lib/features/grid-snapping/behavior/index.js | 7 ++- .../behavior/SpaceToolBehaviorSpec.js | 55 +++++++++++++++++++ 3 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 lib/features/grid-snapping/behavior/SpaceToolBehavior.js create mode 100644 test/spec/features/grid-snapping/behavior/SpaceToolBehaviorSpec.js diff --git a/lib/features/grid-snapping/behavior/SpaceToolBehavior.js b/lib/features/grid-snapping/behavior/SpaceToolBehavior.js new file mode 100644 index 000000000..b1fbc3762 --- /dev/null +++ b/lib/features/grid-snapping/behavior/SpaceToolBehavior.js @@ -0,0 +1,34 @@ +var HIGH_PRIORITY = 2000; + +/** + * Integrates space tool with grid snapping. + */ +export default function SpaceToolBehavior(eventBus, gridSnapping) { + eventBus.on([ + 'spaceTool.move', + 'spaceTool.end' + ], HIGH_PRIORITY, function(event) { + var context = event.context; + + if (!context.initialized) { + return; + } + + var axis = context.axis; + + if (axis === 'x') { + + // snap delta x to multiple of 10 + event.dx = gridSnapping.snapValue(event.dx); + } else { + + // snap delta y to multiple of 10 + event.dy = gridSnapping.snapValue(event.dy); + } + }); +} + +SpaceToolBehavior.$inject = [ + 'eventBus', + 'gridSnapping' +]; \ No newline at end of file diff --git a/lib/features/grid-snapping/behavior/index.js b/lib/features/grid-snapping/behavior/index.js index 346a39396..1143f23f7 100644 --- a/lib/features/grid-snapping/behavior/index.js +++ b/lib/features/grid-snapping/behavior/index.js @@ -1,13 +1,16 @@ import GridSnapping from '../GridSnapping'; import ResizeBehavior from './ResizeBehavior'; +import SpaceToolBehavior from './SpaceToolBehavior'; export default { __depends__: [ GridSnapping ], __init__: [ - 'gridSnappingResizeBehavior' + 'gridSnappingResizeBehavior', + 'gridSnappingSpaceToolBehavior' ], - gridSnappingResizeBehavior: [ 'type', ResizeBehavior ] + gridSnappingResizeBehavior: [ 'type', ResizeBehavior ], + gridSnappingSpaceToolBehavior: [ 'type', SpaceToolBehavior ] }; \ No newline at end of file diff --git a/test/spec/features/grid-snapping/behavior/SpaceToolBehaviorSpec.js b/test/spec/features/grid-snapping/behavior/SpaceToolBehaviorSpec.js new file mode 100644 index 000000000..492bae152 --- /dev/null +++ b/test/spec/features/grid-snapping/behavior/SpaceToolBehaviorSpec.js @@ -0,0 +1,55 @@ +import { + bootstrapDiagram, + inject +} from 'test/TestHelper'; + +import gridSnappingModule from 'lib/features/grid-snapping'; +import modelingModule from 'lib/features/modeling'; +import moveModule from 'lib/features/move'; +import spaceToolModule from 'lib/features/space-tool'; + +import { + createCanvasEvent as canvasEvent +} from '../../../../util/MockEvents'; + + +describe('features/grid-snapping - space tool', function() { + + var shape; + + beforeEach(bootstrapDiagram({ + modules: [ + gridSnappingModule, + modelingModule, + moveModule, + spaceToolModule + ] + })); + + beforeEach(inject(function(canvas, elementFactory) { + + shape = elementFactory.createShape({ + id: 'shape', + x: 100, y: 100, + width: 100, height: 100 + }); + + canvas.addShape(shape); + })); + + + it('should snap make space', inject(function(spaceTool, dragging) { + + // when + spaceTool.activateMakeSpace(canvasEvent({ x: 90, y: 100 })); + + dragging.move(canvasEvent({ x: 110, y: 100 })); + + dragging.end(); + + // then + expect(shape.x).to.eql(120); + expect(shape.y).to.eql(100); + })); + +}); \ No newline at end of file