-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(grid-snapping): integrate grid snapping with existing features
Related to camunda/camunda-modeler#1344
- Loading branch information
1 parent
261832c
commit 3d05d2f
Showing
6 changed files
with
197 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import inherits from 'inherits'; | ||
|
||
import CommandInterceptor from '../../../command/CommandInterceptor'; | ||
import { isString } from 'min-dash'; | ||
|
||
|
||
/** | ||
* Integrates resizing with grid snapping. | ||
*/ | ||
export default function ResizeBehavior(eventBus, gridSnapping, modeling) { | ||
CommandInterceptor.call(this, eventBus); | ||
|
||
this.preExecute('shape.resize', function(event) { | ||
var context = event.context, | ||
hints = context.hints, | ||
autoResize = hints && hints.autoResize; | ||
|
||
var shape, direction, newBounds; | ||
|
||
if (autoResize) { | ||
shape = context.shape; | ||
newBounds = context.newBounds; | ||
|
||
if (isString(autoResize)) { | ||
|
||
// snap x, y, width and height depending on direction | ||
direction = autoResize; | ||
|
||
if (/e|w/.test(direction)) { | ||
newBounds.width = gridSnapping.snapValue(newBounds.width, { | ||
min: newBounds.width | ||
}); | ||
} | ||
|
||
if (/e/.test(direction)) { | ||
newBounds.x = shape.x + shape.width - newBounds.width; | ||
} | ||
|
||
if (/n|s/.test(direction)) { | ||
newBounds.height = gridSnapping.snapValue(newBounds.height, { | ||
min: newBounds.height | ||
}); | ||
} | ||
|
||
if (/n/.test(direction)) { | ||
newBounds.y = shape.y + shape.height - newBounds.height; | ||
} | ||
} else { | ||
|
||
// snap width and height without moving shape | ||
newBounds.width = gridSnapping.snapValue(newBounds.width, { | ||
min: newBounds.width | ||
}, 20); | ||
|
||
newBounds.height = gridSnapping.snapValue(newBounds.height, { | ||
min: newBounds.height | ||
}, 20); | ||
|
||
newBounds.x = shape.x + (shape.width / 2) - (newBounds.width / 2); | ||
newBounds.y = shape.y + (shape.height / 2) - (newBounds.height / 2); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
ResizeBehavior.$inject = [ | ||
'eventBus', | ||
'gridSnapping', | ||
'modeling' | ||
]; | ||
|
||
inherits(ResizeBehavior, CommandInterceptor); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Integrates space tool with grid snapping. | ||
*/ | ||
export default function SpaceToolBehavior(eventBus, gridSnapping) { | ||
eventBus.on('spaceTool.move', 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' | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import GridSnapping from '../GridSnapping'; | ||
|
||
import ResizeBehavior from './ResizeBehavior'; | ||
import SpaceToolBehavior from './SpaceToolBehavior'; | ||
|
||
export default { | ||
__depends__: [ | ||
GridSnapping | ||
], | ||
__init__: [ | ||
'gridSnappingResizeBehavior', | ||
'gridSnappingSpaceToolBehavior' | ||
], | ||
gridSnappingResizeBehavior: [ 'type', ResizeBehavior ], | ||
gridSnappingSpaceToolBehavior: [ 'type', SpaceToolBehavior ] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters