Browse Source

Refactor Drag command

pull/1840/head
Artur Arseniev 8 years ago
parent
commit
c3af5b4116
  1. 108
      src/commands/view/Drag.js

108
src/commands/view/Drag.js

@ -1,65 +1,77 @@
import { isUndefined } from 'underscore'; import { isUndefined } from 'underscore';
import { on, off } from 'utils/mixins';
import Dragger from 'utils/Dragger'; import Dragger from 'utils/Dragger';
module.exports = { module.exports = {
run(editor, sender, opts = {}) { run(editor, sender, opts = {}) {
const { target } = opts; const { id } = this;
var el = (opts && opts.el) || ''; const { target, options = {} } = opts;
var canvas = editor.Canvas; const { onEnd, event } = options;
var dragger = this.dragger; const config = {
var options = opts.options || {}; ...options,
var canvasView = canvas.getCanvasView(); doc: target.getEl().ownerDocument,
options.prefix = editor.getConfig().stylePrefix; onStart() {
options.mousePosFetcher = canvas.getMouseRelativePos; target.addStyle({ position: 'absolute' });
options.posFetcher = canvasView.getElementPos.bind(canvasView); },
onEnd() {
onEnd && onEnd();
editor.stopCommand(id);
},
getPosition() {
const style = target.getStyle();
let { left, top } = style;
// Create the resizer for the canvas if not yet created if (isUndefined(left) || isUndefined(top)) {
if (!dragger) { const rect = target.getEl().getBoundingClientRect();
dragger = new Dragger({ left = rect.left;
...options, top = rect.top;
doc: target.getEl().ownerDocument, }
onStart() {
target.addStyle({ position: 'absolute' });
},
getPosition() {
const style = target.getStyle();
let { left, top } = style;
if (isUndefined(left) || isUndefined(top)) { const result = {
const rect = target.getEl().getBoundingClientRect(); x: parseFloat(left),
left = rect.left; y: parseFloat(top)
top = rect.top; };
}
const result = { return result;
x: parseFloat(left), },
y: parseFloat(top) setPosition({ x, y, end }) {
}; const unit = 'px';
return result; target.addStyle(
}, {
setPosition({ x, y, end }) { left: `${x}${unit}`,
const unit = 'px'; top: `${y}${unit}`,
e: !end ? 1 : '' // this will trigger the final change
},
{ avoidStore: !end }
);
}
};
target.addStyle( let dragger = this.dragger;
{
left: `${x}${unit}`, if (!dragger) {
top: `${y}${unit}`, dragger = new Dragger(config);
e: !end ? 1 : '' // this will trigger the final change
},
{ avoidStore: !end }
);
}
});
this.dragger = dragger; this.dragger = dragger;
} else {
dragger.setOptions(config);
} }
dragger.setOptions(options); event && dragger.start(event);
this.toggleDrag(1);
if (options.event) {
dragger.start(options.event);
}
return dragger; return dragger;
},
stop() {
this.toggleDrag();
},
toggleDrag(enable) {
const { ppfx } = this;
const methodCls = enable ? 'add' : 'remove';
const canvas = this.getCanvas();
const classes = [`${ppfx}is__grabbing`];
classes.forEach(cls => canvas.classList[methodCls](cls));
} }
}; };

Loading…
Cancel
Save