Browse Source

Refactor dragger and move snap check in snapGuides method

pull/1918/head
Artur Arseniev 8 years ago
parent
commit
74acb1086f
  1. 34
      src/utils/Dragger.js

34
src/utils/Dragger.js

@ -140,8 +140,20 @@ export default class Dragger {
this.lockedAxis = lockedAxis; this.lockedAxis = lockedAxis;
moveDelta(delta); moveDelta(delta);
// Check if to snap on guides const { newDelta, trgX, trgY } = this.snapGuides(deltaPre);
(trgX || trgY) && moveDelta(newDelta);
// In case the mouse button was released outside of the window
ev.which === 0 && this.stop(ev);
}
/**
* Check if the delta hits some guide
*/
snapGuides(delta) {
const newDelta = delta;
let { trgX, trgY } = this; let { trgX, trgY } = this;
this.guidesTarget.forEach(trg => { this.guidesTarget.forEach(trg => {
// Skip the guide if its locked axis already exists // Skip the guide if its locked axis already exists
if ((trg.x && this.trgX) || (trg.y && this.trgY)) return; if ((trg.x && this.trgX) || (trg.y && this.trgY)) return;
@ -150,9 +162,10 @@ export default class Dragger {
this.guidesStatic.forEach(stat => { this.guidesStatic.forEach(stat => {
if ((trg.y && stat.x) || (trg.x && stat.y)) return; if ((trg.y && stat.x) || (trg.x && stat.y)) return;
const isY = trg.y && stat.y; const isY = trg.y && stat.y;
const trgPoint = isY ? trg.y : trg.x; const axs = isY ? 'y' : 'x';
const statPoint = isY ? stat.y : stat.x; const trgPoint = trg[axs];
const deltaPoint = isY ? deltaPre.y : deltaPre.x; const statPoint = stat[axs];
const deltaPoint = delta[axs];
const trgGuide = isY ? trgY : trgX; const trgGuide = isY ? trgY : trgX;
if (this.isPointIn(trgPoint, statPoint)) { if (this.isPointIn(trgPoint, statPoint)) {
@ -171,20 +184,21 @@ export default class Dragger {
const axis = co.toUpperCase(); const axis = co.toUpperCase();
let trg = this[`trg${axis}`]; let trg = this[`trg${axis}`];
if (trg && !this.isPointIn(deltaPre[co], trg.lock)) { if (trg && !this.isPointIn(delta[co], trg.lock)) {
this.setGuideLock(trg, null); this.setGuideLock(trg, null);
trg = null; trg = null;
} }
if (trg && !isUndefined(trg.lock)) { if (trg && !isUndefined(trg.lock)) {
deltaPre[co] = trg.lock; newDelta[co] = trg.lock;
} }
}); });
(this.trgX || this.trgY) && moveDelta(deltaPre); return {
newDelta,
// In case the mouse button was released outside of the window trgX: this.trgX,
ev.which === 0 && this.stop(ev); trgY: this.trgY
};
} }
isPointIn(src, trg, { offset } = {}) { isPointIn(src, trg, { offset } = {}) {

Loading…
Cancel
Save