Browse Source

Add scroll listener in Dragger

drag-autoscroll-fix
Artur Arseniev 7 years ago
parent
commit
63d279ad24
  1. 24
      src/commands/view/ComponentDrag.js
  2. 69
      src/utils/Dragger.js

24
src/commands/view/ComponentDrag.js

@ -1,4 +1,5 @@
import { keys, bindAll, each, isUndefined } from 'underscore'; import { keys, bindAll, each, isUndefined } from 'underscore';
import { on, off } from 'utils/mixins';
import Dragger from 'utils/Dragger'; import Dragger from 'utils/Dragger';
module.exports = { module.exports = {
@ -12,14 +13,12 @@ module.exports = {
'getPosition', 'getPosition',
'getGuidesStatic', 'getGuidesStatic',
'renderGuide', 'renderGuide',
'getGuidesTarget' 'getGuidesTarget',
'handleScroll'
); );
const { target, event, mode, dragger = {} } = opts; const { target, event, mode, dragger = {} } = opts;
const { Canvas } = editor;
const el = target.getEl(); const el = target.getEl();
const scale = Canvas.getZoomMultiplier();
const config = { const config = {
scale,
doc: el.ownerDocument, doc: el.ownerDocument,
onStart: this.onStart, onStart: this.onStart,
onEnd: this.onEnd, onEnd: this.onEnd,
@ -416,12 +415,23 @@ module.exports = {
}); });
}, },
toggleDrag(on) { handleScroll(ev) {
console.log('Im scrolling', this.ppfx, { evY: ev.clientY, ev });
},
toggleDrag(enable) {
const { ppfx, editor } = this; const { ppfx, editor } = this;
const methodCls = on ? 'add' : 'remove'; const methods = { on, off };
const methodCls = enable ? 'add' : 'remove';
const canvas = this.getCanvas(); const canvas = this.getCanvas();
const classes = [`${ppfx}is__grabbing`]; const classes = [`${ppfx}is__grabbing`];
const { Canvas } = editor;
classes.forEach(cls => canvas.classList[methodCls](cls)); classes.forEach(cls => canvas.classList[methodCls](cls));
editor.Canvas[on ? 'startAutoscroll' : 'stopAutoscroll'](); Canvas[enable ? 'startAutoscroll' : 'stopAutoscroll']();
methods[enable ? 'on' : 'off'](
Canvas.getWindow(),
'scroll',
this.handleScroll
);
} }
}; };

69
src/utils/Dragger.js

@ -1,5 +1,5 @@
import { bindAll, isFunction, result, isUndefined } from 'underscore'; import { bindAll, isFunction, result, isUndefined } from 'underscore';
import { on, off, isEscKey } from 'utils/mixins'; import { on, off, isEscKey, getPointerEvent } from 'utils/mixins';
export default class Dragger { export default class Dragger {
/** /**
@ -61,7 +61,7 @@ export default class Dragger {
// Scale result points, can also be a function // Scale result points, can also be a function
scale: 1 scale: 1
}; };
bindAll(this, 'drag', 'stop', 'keyHandle'); bindAll(this, 'drag', 'stop', 'keyHandle', 'handleScroll');
this.setOptions(opts); this.setOptions(opts);
this.delta = { x: 0, y: 0 }; this.delta = { x: 0, y: 0 };
return this; return this;
@ -81,11 +81,31 @@ export default class Dragger {
toggleDrag(enable) { toggleDrag(enable) {
const docs = this.getDocumentEl(); const docs = this.getDocumentEl();
const container = this.getContainerEl(); const container = this.getContainerEl();
const win = this.getWindowEl();
const method = enable ? 'on' : 'off'; const method = enable ? 'on' : 'off';
const methods = { on, off }; const methods = { on, off };
methods[method](container, 'mousemove dragover', this.drag); methods[method](container, 'mousemove dragover', this.drag);
methods[method](docs, 'mouseup dragend touchend', this.stop); methods[method](docs, 'mouseup dragend touchend', this.stop);
methods[method](docs, 'keydown', this.keyHandle); methods[method](docs, 'keydown', this.keyHandle);
methods[method](win, 'scroll', this.handleScroll);
}
handleScroll() {
const { lastScrollTop, lastScroll, delta } = this;
// const { doc } = opts;
// const lastPos = this.currentPointer;
const actualTop = this.opts.doc.body.scrollTop; // TO REMOVE
const diff = actualTop - lastScrollTop; // TO REMOVE
const actualScroll = this.getScrollInfo();
const scroll = {
x: actualScroll.x - lastScroll.x,
y: actualScroll.y - lastScroll.y
};
this.lastDiff = diff; // TO REMOVE
this.move(delta.x + scroll.x, delta.y + diff);
this.lastScrollDiff = scroll;
// console.log('handle scroll from Dragger', { actualTop, lastScrollTop, diff });
console.log('handle scroll from Dragger', { scroll });
} }
/** /**
@ -101,6 +121,10 @@ export default class Dragger {
this.guidesTarget = result(opts, 'guidesTarget') || []; this.guidesTarget = result(opts, 'guidesTarget') || [];
isFunction(onStart) && onStart(ev, this); isFunction(onStart) && onStart(ev, this);
this.startPosition = this.getStartPosition(); this.startPosition = this.getStartPosition();
this.lastDiff = 0;
this.globDiff = 0;
this.lastScrollDiff = { x: 0, y: 0 };
this.globScrollDiff = { x: 0, y: 0 };
this.drag(ev); this.drag(ev);
} }
@ -109,14 +133,23 @@ export default class Dragger {
* @param {Event} event * @param {Event} event
*/ */
drag(ev) { drag(ev) {
const { opts } = this; const { opts, lastDiff, globDiff, lastScrollDiff, globScrollDiff } = this;
const { onDrag } = opts; const { onDrag, doc } = opts;
const { startPointer } = this; const { startPointer } = this;
const currentPos = this.getPointerPos(ev); const currentPos = this.getPointerPos(ev);
// console.log({ lastDiff, globDiff });
this.globDiff += lastDiff;
const glDiff = {
x: globScrollDiff.x + lastScrollDiff.x,
y: globScrollDiff.y + lastScrollDiff.y
};
this.globScrollDiff = glDiff;
const delta = { const delta = {
x: currentPos.x - startPointer.x, x: currentPos.x - startPointer.x,
y: currentPos.y - startPointer.y y: currentPos.y - startPointer.y + this.globDiff
}; };
this.lastDiff = 0;
this.lastScrollDiff = { x: 0, y: 0 };
let { lockedAxis } = this; let { lockedAxis } = this;
// Lock one axis // Lock one axis
@ -141,6 +174,8 @@ export default class Dragger {
const deltaPre = { ...delta }; const deltaPre = { ...delta };
this.currentPointer = currentPos; this.currentPointer = currentPos;
this.lockedAxis = lockedAxis; this.lockedAxis = lockedAxis;
this.lastScrollTop = this.opts.doc ? this.opts.doc.body.scrollTop : 0; // TO REMOVE
this.lastScroll = this.getScrollInfo();
moveDelta(delta); moveDelta(delta);
if (this.guidesTarget.length) { if (this.guidesTarget.length) {
@ -282,6 +317,14 @@ export default class Dragger {
return container ? [container] : this.getDocumentEl(); return container ? [container] : this.getDocumentEl();
} }
getWindowEl() {
const cont = this.getContainerEl();
return cont.map(item => {
const doc = item.ownerDocument || item;
return doc.defaultView || doc.parentWindow;
});
}
/** /**
* Returns documents * Returns documents
*/ */
@ -306,11 +349,13 @@ export default class Dragger {
*/ */
getPointerPos(ev) { getPointerPos(ev) {
const getPos = this.opts.getPointerPosition; const getPos = this.opts.getPointerPosition;
const pEv = getPointerEvent(ev);
return getPos return getPos
? getPos(ev) ? getPos(ev)
: { : {
x: ev.clientX, x: pEv.clientX,
y: ev.clientY y: pEv.clientY
}; };
} }
@ -331,6 +376,16 @@ export default class Dragger {
return result; return result;
} }
getScrollInfo() {
const { doc } = this.opts;
const body = doc && doc.body;
return {
y: body ? body.scrollTop : 0,
x: body ? body.scrollLeft : 0
};
}
detectAxisLock(x, y) { detectAxisLock(x, y) {
const relX = x; const relX = x;
const relY = y; const relY = y;

Loading…
Cancel
Save