2014-12-19 17:06:21 +00:00
|
|
|
import _ from 'lodash';
|
|
|
|
|
2014-12-20 15:25:32 +00:00
|
|
|
function customEvent(name, detail) {
|
2014-12-19 15:24:45 +00:00
|
|
|
var evt = document.createEvent('Event');
|
|
|
|
evt.initEvent(name, true, true);
|
|
|
|
evt.detail = detail;
|
|
|
|
return evt;
|
|
|
|
}
|
2014-12-19 17:06:21 +00:00
|
|
|
|
|
|
|
|
2014-12-20 15:25:32 +00:00
|
|
|
function normalizeBBox(box) {
|
2014-12-19 17:06:21 +00:00
|
|
|
return _.extend({
|
|
|
|
ax: box.x,
|
|
|
|
ax2: box.x2,
|
|
|
|
ay: box.cy
|
|
|
|
}, box);
|
|
|
|
}
|
2014-12-19 17:11:44 +00:00
|
|
|
|
2014-12-20 15:25:32 +00:00
|
|
|
function spaceHorizontally(items, options) {
|
2014-12-19 17:11:44 +00:00
|
|
|
var verticalCenter = 0;
|
|
|
|
|
|
|
|
options = _.defaults(options || {}, {
|
|
|
|
padding: 0
|
|
|
|
});
|
|
|
|
|
|
|
|
_.reduce(items, (offset, item) => {
|
|
|
|
var box;
|
|
|
|
|
|
|
|
item.transform(Snap.matrix()
|
|
|
|
.translate(offset, 0));
|
|
|
|
|
|
|
|
box = normalizeBBox(item.getBBox());
|
|
|
|
verticalCenter = Math.max(verticalCenter, box.ay);
|
|
|
|
|
|
|
|
return offset + options.padding + box.width;
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
for (var item of items) {
|
|
|
|
let box = normalizeBBox(item.getBBox());
|
|
|
|
|
|
|
|
item.transform(Snap.matrix()
|
|
|
|
.add(item.transform().localMatrix)
|
|
|
|
.translate(0, verticalCenter - box.ay));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-20 15:25:32 +00:00
|
|
|
function spaceVertically(items, options) {
|
2014-12-19 17:11:44 +00:00
|
|
|
var horizontalCenter = 0;
|
|
|
|
|
|
|
|
options = _.defaults(options || {}, {
|
|
|
|
padding: 0
|
|
|
|
});
|
|
|
|
|
|
|
|
_.reduce(items, (offset, item) => {
|
|
|
|
var box;
|
|
|
|
|
|
|
|
item.transform(Snap.matrix()
|
|
|
|
.translate(0, offset));
|
|
|
|
|
|
|
|
box = item.getBBox();
|
|
|
|
|
|
|
|
horizontalCenter = Math.max(horizontalCenter, box.cx);
|
|
|
|
|
|
|
|
return offset + options.padding + box.height;
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
for (var item of items) {
|
|
|
|
item.transform(Snap.matrix()
|
|
|
|
.add(item.transform().localMatrix)
|
|
|
|
.translate(horizontalCenter - item.getBBox().cx, 0));
|
|
|
|
}
|
|
|
|
}
|
2014-12-20 15:18:00 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
customEvent,
|
|
|
|
normalizeBBox,
|
|
|
|
spaceHorizontally,
|
|
|
|
spaceVertically
|
|
|
|
};
|