Browse Source

Add snapGuides option in Dragge

pull/1918/head
Artur Arseniev 8 years ago
parent
commit
fc14739b47
  1. 19
      src/commands/view/ComponentDrag.js
  2. 17
      src/utils/Dragger.js

19
src/commands/view/ComponentDrag.js

@ -14,7 +14,7 @@ module.exports = {
'renderGuide',
'getGuidesTarget'
);
const { target, event, mode } = opts;
const { target, event, mode, dragger = {} } = opts;
const { Canvas } = editor;
const el = target.getEl();
const scale = Canvas.getZoomMultiplier();
@ -27,7 +27,8 @@ module.exports = {
getPosition: this.getPosition,
setPosition: this.setPosition,
getGuidesStatic: () => this.guidesStatic,
getGuidesTarget: () => this.guidesTarget
getGuidesTarget: () => this.guidesTarget,
...dragger
};
this.setupGuides();
this.opts = opts;
@ -39,19 +40,19 @@ module.exports = {
this.guidesTarget = this.getGuidesTarget();
this.guidesStatic = this.getGuidesStatic();
window.guidesTarget = this.guidesTarget;
let dragger = this.dragger;
let drg = this.dragger;
if (!dragger) {
dragger = new Dragger(config);
this.dragger = dragger;
if (!drg) {
drg = new Dragger(config);
this.dragger = drg;
} else {
dragger.setOptions(config);
drg.setOptions(config);
}
event && dragger.start(event);
event && drg.start(event);
this.toggleDrag(1);
return dragger;
return drg;
},
stop() {

17
src/utils/Dragger.js

@ -42,6 +42,12 @@ export default class Dragger {
*/
getPosition: null,
// Offset before snap to guides
snapGuides: 1,
// Offset before snap to guides
snapOffset: 5,
/**
* Get static guides
*/
@ -52,9 +58,6 @@ export default class Dragger {
*/
getGuidesTarget: () => [],
// Offset before snap to guides
snapOffset: 5,
// Document on which listen to pointer events
doc: 0,
@ -107,7 +110,7 @@ export default class Dragger {
*/
drag(ev) {
const { opts } = this;
const { onDrag } = opts;
const { onDrag, snapGuides } = opts;
const { startPointer } = this;
const currentPos = this.getPointerPos(ev);
const delta = {
@ -140,8 +143,10 @@ export default class Dragger {
this.lockedAxis = lockedAxis;
moveDelta(delta);
const { newDelta, trgX, trgY } = this.snapGuides(deltaPre);
(trgX || trgY) && moveDelta(newDelta);
if (snapGuides) {
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);

Loading…
Cancel
Save