Skip to content

Commit

Permalink
feat(grid-snapping): integrate space tool
Browse files Browse the repository at this point in the history
  • Loading branch information
philippfromme committed Apr 26, 2019
1 parent 9b05fb0 commit 757c5fb
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
34 changes: 34 additions & 0 deletions lib/features/grid-snapping/behavior/SpaceToolBehavior.js
Original file line number Diff line number Diff line change
@@ -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'
];
7 changes: 5 additions & 2 deletions lib/features/grid-snapping/behavior/index.js
Original file line number Diff line number Diff line change
@@ -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 ]
};
55 changes: 55 additions & 0 deletions test/spec/features/grid-snapping/behavior/SpaceToolBehaviorSpec.js
Original file line number Diff line number Diff line change
@@ -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);
}));

});

0 comments on commit 757c5fb

Please sign in to comment.