1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | 1× 11× 11× 11× 7× 11× 11× 11× 11× 11× 2× 11× 1× 1× 9× 9× 9× 9× 8× 9× 9× 9× 1× 11× 11× 11× 11× 11× 11× 4× 7× 7× 5× 5× 7× 7× 7× 7× 7× 14× 14× 14× 7× 7× 7× 7× 7× 14× 14× 7× 1× 10× 10× 10× 10× 10× 10× 18× 11× 7× 4× 3× 1× 5× 1× 14× 14× 14× 14× 1× 14× 5× 14× 1× 2× 4× 1× 5× 5× 5× 1× 6× 12× | import { filter, forEach, debounce, bind } from 'min-dash'; import SnapContext from './SnapContext'; import { mid, isSnapped, setSnapped } from './SnapUtil'; var HIGHER_PRIORITY = 1250; import { append as svgAppend, attr as svgAttr, classes as svgClasses, create as svgCreate } from 'tiny-svg'; /** * A general purpose snapping component for diagram elements. * * @param {EventBus} eventBus * @param {Canvas} canvas */ export default function Snapping(eventBus, canvas) { this._canvas = canvas; var self = this; eventBus.on([ 'shape.move.start', 'create.start' ], function(event) { self.initSnap(event); }); eventBus.on([ 'shape.move.move', 'shape.move.end', 'create.move', 'create.end' ], HIGHER_PRIORITY, function(event) { Iif (event.originalEvent && event.originalEvent.ctrlKey) { return; } Iif (isSnapped(event)) { return; } self.snap(event); }); eventBus.on([ 'shape.move.cleanup', 'create.cleanup' ], function(event) { self.hide(); }); // delay hide by 1000 seconds since last match this._asyncHide = debounce(bind(this.hide, this), 1000); } Snapping.$inject = [ 'eventBus', 'canvas' ]; Snapping.prototype.initSnap = function(event) { var context = event.context, shape = context.shape, snapContext = context.snapContext; if (!snapContext) { snapContext = context.snapContext = new SnapContext(); } var snapMid = mid(shape, event); snapContext.setSnapOrigin('mid', { x: snapMid.x - event.x, y: snapMid.y - event.y }); return snapContext; }; Snapping.prototype.snap = function(event) { var context = event.context, snapContext = context.snapContext, shape = context.shape, target = context.target, snapLocations = snapContext.getSnapLocations(); if (!target) { return; } var snapPoints = snapContext.pointsForTarget(target); if (!snapPoints.initialized) { this.addTargetSnaps(snapPoints, shape, target); snapPoints.initialized = true; } var snapping = { x: isSnapped(event, 'x'), y: isSnapped(event, 'y') }; forEach(snapLocations, function(location) { var snapOrigin = snapContext.getSnapOrigin(location); var snapCurrent = { x: event.x + snapOrigin.x, y: event.y + snapOrigin.y }; // snap on both axis, if not snapped already forEach([ 'x', 'y' ], function(axis) { var locationSnapping; Eif (!snapping[axis]) { locationSnapping = snapPoints.snap(snapCurrent, location, axis, 7); if (locationSnapping !== undefined) { snapping[axis] = { value: locationSnapping, originValue: locationSnapping - snapOrigin[axis] }; } } }); // no more need to snap, drop out of interation Iif (snapping.x && snapping.y) { return false; } }); // show snap visuals this.showSnapLine('vertical', snapping.x && snapping.x.value); this.showSnapLine('horizontal', snapping.y && snapping.y.value); // adjust event { x, y, dx, dy } and mark as snapping forEach([ 'x', 'y' ], function(axis) { var axisSnapping = snapping[axis]; if (typeof axisSnapping === 'object') { // set as snapped and adjust the x and/or y position of the event setSnapped(event, axis, axisSnapping.originValue); } }); }; Snapping.prototype._createLine = function(orientation) { var root = this._canvas.getLayer('snap'); // var line = root.path('M0,0 L0,0').addClass('djs-snap-line'); var line = svgCreate('path'); svgAttr(line, { d: 'M0,0 L0,0' }); svgClasses(line).add('djs-snap-line'); svgAppend(root, line); return { update: function(position) { if (typeof position !== 'number') { svgAttr(line, { display: 'none' }); } else { if (orientation === 'horizontal') { svgAttr(line, { d: 'M-100000,' + position + ' L+100000,' + position, display: '' }); } else { svgAttr(line, { d: 'M ' + position + ',-100000 L ' + position + ', +100000', display: '' }); } } } }; }; Snapping.prototype._createSnapLines = function() { this._snapLines = { horizontal: this._createLine('horizontal'), vertical: this._createLine('vertical') }; }; Snapping.prototype.showSnapLine = function(orientation, position) { var line = this.getSnapLine(orientation); Eif (line) { line.update(position); } this._asyncHide(); }; Snapping.prototype.getSnapLine = function(orientation) { if (!this._snapLines) { this._createSnapLines(); } return this._snapLines[orientation]; }; Snapping.prototype.hide = function() { forEach(this._snapLines, function(l) { l.update(); }); }; Snapping.prototype.addTargetSnaps = function(snapPoints, shape, target) { var siblings = this.getSiblings(shape, target); forEach(siblings, function(s) { snapPoints.add('mid', mid(s)); }); }; Snapping.prototype.getSiblings = function(element, target) { // snap to all siblings that are not hidden, labels, attached to element or element itself return target && filter(target.children, function(e) { return !e.hidden && !e.labelTarget && e.host !== element && e !== element; }); }; |