Browse Source

Refactor Dragger

drag-rules
Artur Arseniev 7 years ago
parent
commit
bf4dc13b83
  1. 99
      src/utils/Dragger.js

99
src/utils/Dragger.js

@ -126,77 +126,54 @@ export default class Dragger {
delta.y = startPointer.y; delta.y = startPointer.y;
} }
let diffY = 0;
let diffX = 0;
let { trgX, trgY } = this; let { trgX, trgY } = this;
const offset = 20; const offset = 20;
this.guidesTarget.forEach(trg => { this.guidesTarget.forEach(trg => {
trg.active = 0; trg.active = 0;
// Skip the guide if its locked axis already exists
// TODO: allow near axis change (using diff)
if ((trg.x && this.trgX) || (trg.y && this.trgY)) return;
this.guidesStatic.forEach(stat => { this.guidesStatic.forEach(stat => {
if (trg.y && stat.y) { if ((trg.y && stat.x) || (trg.x && stat.y)) return;
// if ( const isY = trg.y && stat.y;
// (trg.y >= stat.y && trg.y <= stat.y + offset) || const trgPoint = isY ? trg.y : trg.x;
// (trg.y <= stat.y && trg.y >= stat.y - offset) const statPoint = isY ? stat.y : stat.x;
// ) { const deltaPoint = isY ? delta.y : delta.x;
// console.log('trg.y', trg.y, 'stat.y', stat.y, 'diff', trg.y - stat.y) const trgGuide = isY ? trgY : trgX;
// diffY = (trg.y - stat.y);
// } if (this.isPointIn(trgPoint, statPoint)) {
if (this.isPointIn(trg.y, stat.y)) { if (isUndefined(trgGuide)) {
if (isUndefined(trgY)) { const trgValue = deltaPoint - (trgPoint - statPoint);
trgY = this.setGuideLock(trg, delta.y - (trg.y - stat.y)); this.setGuideLock(trg, trgValue);
}
}
} else {
if (this.isPointIn(trg.x, stat.x)) {
if (isUndefined(trgX)) {
trgX = this.setGuideLock(trg, delta.x - (trg.x - stat.x));
}
} }
} }
}); });
if (trg.lock) trg.active = 1;
}); });
// if (diffY) { trgX = this.trgX;
// console.log( 'delta.y before', delta.y, 'diff Y ', diffY, 'delta.t after', delta.y - diffY); trgY = this.trgY;
// delta.y = delta.y - diffY;
// } ['x', 'y'].forEach(co => {
const axis = co.toUpperCase();
if (trgX && !this.isPointIn(delta.x, trgX.lock)) { let trg = this[`trg${axis}`];
this.setGuideLock(trgX, null);
trgX = null; if (trg && !this.isPointIn(delta[co], trg.lock)) {
} this.setGuideLock(trg, null);
trg = null;
if (trgY && !this.isPointIn(delta.y, trgY.lock)) { }
this.setGuideLock(trgY, null);
trgY = null; if (trg && !isUndefined(trg.lock)) {
} console.log(
`locked ${axis} at: ${trg.lock}`,
if (trgX && !isUndefined(trgX.lock)) { `(type: ${trg.type})`,
console.log( `delta: ${delta[co]}`,
'locked X at:', `range (${trg.lock - offset} - ${trg.lock + offset})`
trgX.lock, );
`(type: ${trgX.type})`, delta[co] = trg.lock;
'delta.x: ', }
delta.x, });
`range (${trgX.lock - offset} - ${trgX.lock + offset})`
);
delta.x = trgX.lock;
}
if (trgY && !isUndefined(trgY.lock)) {
console.log(
'locked Y at:',
trgY.lock,
`(type: ${trgY.type})`,
'delta.y: ',
delta.y,
`range (${trgY.lock - offset} - ${trgY.lock + offset})`
);
delta.y = trgY.lock;
}
['x', 'y'].forEach(co => (delta[co] = delta[co] * result(opts, 'scale'))); ['x', 'y'].forEach(co => (delta[co] = delta[co] * result(opts, 'scale')));
this.lockedAxis = lockedAxis; this.lockedAxis = lockedAxis;
@ -230,7 +207,7 @@ export default class Dragger {
this[trgName] = guide; this[trgName] = guide;
} else { } else {
console.log(`UNLOCK ${axis}`, guide.lock); console.log(`UNLOCK ${axis}`, guide.lock);
guide.active = 0; delete guide.active;
delete guide.lock; delete guide.lock;
delete this[trgName]; delete this[trgName];
} }

Loading…
Cancel
Save