Browse Source

Refactor autoscroll and add autoscrollLimit option in canvas

config-autoscroll
artf 7 years ago
parent
commit
05cce65b90
  1. 5
      src/canvas/config/config.js
  2. 24
      src/canvas/index.js
  3. 10
      src/utils/mixins.js

5
src/canvas/config/config.js

@ -26,6 +26,11 @@ module.exports = {
*/ */
customBadgeLabel: '', customBadgeLabel: '',
/**
* Indicate when to start the auto scroll of the canvas on component/block dragging (value in px )
*/
autoscrollLimit: 50,
/** /**
* When some textable component is selected and focused (eg. input or text component) the editor * When some textable component is selected and focused (eg. input or text component) the editor
* stops some commands (eg. disables the copy/paste of components with CTRL+C/V to allow the copy/paste of the text). * stops some commands (eg. disables the copy/paste of components with CTRL+C/V to allow the copy/paste of the text).

24
src/canvas/index.js

@ -28,9 +28,11 @@
* @module Canvas * @module Canvas
*/ */
import { on, off, hasDnd, getElement } from 'utils/mixins'; import { on, off, hasDnd, getElement, getPointerEvent } from 'utils/mixins';
import Droppable from 'utils/Droppable'; import Droppable from 'utils/Droppable';
const { requestAnimationFrame } = window;
module.exports = () => { module.exports = () => {
var c = {}, var c = {},
defaults = require('./config/config'), defaults = require('./config/config'),
@ -81,6 +83,7 @@ module.exports = () => {
this.startAutoscroll = this.startAutoscroll.bind(this); this.startAutoscroll = this.startAutoscroll.bind(this);
this.stopAutoscroll = this.stopAutoscroll.bind(this); this.stopAutoscroll = this.stopAutoscroll.bind(this);
this.autoscroll = this.autoscroll.bind(this); this.autoscroll = this.autoscroll.bind(this);
this.updateClientY = this.updateClientY.bind(this);
return this; return this;
}, },
@ -452,22 +455,27 @@ module.exports = () => {
// By detaching those from the stack avoid browsers lags // By detaching those from the stack avoid browsers lags
// Noticeable with "fast" drag of blocks // Noticeable with "fast" drag of blocks
setTimeout(() => { setTimeout(() => {
on(toListen, 'mousemove', this.autoscroll); on(toListen, 'mousemove dragover', this.updateClientY);
on(toListen, 'mouseup', this.stopAutoscroll); on(toListen, 'mouseup', this.stopAutoscroll);
requestAnimationFrame(this.autoscroll);
}, 0); }, 0);
}, },
updateClientY(ev) {
ev.preventDefault();
this.lastClientY = getPointerEvent(ev).clientY;
},
/** /**
* @private * @private
*/ */
autoscroll(e) { autoscroll() {
e.preventDefault();
if (this.dragging) { if (this.dragging) {
let frameWindow = this.getFrameEl().contentWindow; let frameWindow = this.getFrameEl().contentWindow;
let actualTop = frameWindow.document.body.scrollTop; let actualTop = frameWindow.document.body.scrollTop;
let nextTop = actualTop; let nextTop = actualTop;
let clientY = e.clientY; let clientY = this.lastClientY;
let limitTop = 50; let limitTop = this.getConfig().autoscrollLimit;
let limitBottom = frameRect.height - limitTop; let limitBottom = frameRect.height - limitTop;
if (clientY < limitTop) { if (clientY < limitTop) {
@ -478,8 +486,8 @@ module.exports = () => {
nextTop += clientY - limitBottom; nextTop += clientY - limitBottom;
} }
//console.log(`actualTop: ${actualTop} clientY: ${clientY} nextTop: ${nextTop} frameHeigh: ${frameRect.height}`);
frameWindow.scrollTo(0, nextTop); frameWindow.scrollTo(0, nextTop);
requestAnimationFrame(this.autoscroll);
} }
}, },
@ -490,7 +498,7 @@ module.exports = () => {
stopAutoscroll() { stopAutoscroll() {
this.dragging = 0; this.dragging = 0;
let toListen = this.getScrollListeners(); let toListen = this.getScrollListeners();
off(toListen, 'mousemove', this.autoscroll); off(toListen, 'mousemove dragover', this.updateClientY);
off(toListen, 'mouseup', this.stopAutoscroll); off(toListen, 'mouseup', this.stopAutoscroll);
}, },

10
src/utils/mixins.js

@ -121,6 +121,13 @@ const getModel = (el, $) => {
return model; return model;
}; };
/**
* Get cross-device pointer event
* @param {Event} ev
* @return {Event}
*/
const getPointerEvent = (ev) => ev.touches && ev.touches[0] ? ev.touches[0] : ev;
export { export {
on, on,
off, off,
@ -132,5 +139,6 @@ export {
getElement, getElement,
shallowDiff, shallowDiff,
normalizeFloat, normalizeFloat,
getUnitFromValue getPointerEvent,
getUnitFromValue,
}; };

Loading…
Cancel
Save