regexper-static/src/js/parser/javascript/match.js

88 lines
2.1 KiB
JavaScript
Raw Normal View History

import util from '../../util.js';
import _ from 'lodash';
import Q from 'q';
export default {
type: 'match',
2014-12-17 21:04:55 +00:00
definedProperties: {
_anchor: {
get: function() {
var start = util.normalizeBBox(_.first(this.items).getBBox()),
end = util.normalizeBBox(_.last(this.items).getBBox()),
2014-12-17 21:04:55 +00:00
matrix = this.transform().localMatrix;
return {
ax: matrix.x(start.ax, start.ay),
ax2: matrix.x(end.ax2, end.ay),
ay: matrix.y(start.ax, start.ay)
};
}
}
},
_render() {
var start, end,
partPromises;
if (this.anchorStart) {
2014-12-13 18:42:55 +00:00
start = this.renderLabel('Start of line')
.invoke('addClass', 'anchor');
2014-12-11 00:58:36 +00:00
}
if (this.anchorEnd) {
2014-12-13 18:42:55 +00:00
end = this.renderLabel('End of line')
.invoke('addClass', 'anchor');
2014-12-11 00:58:36 +00:00
}
2014-12-17 20:12:04 +00:00
partPromises = _.map(this.parts, part => {
return part.render(this.container.group());
});
2014-12-15 02:37:56 +00:00
2014-12-17 20:12:04 +00:00
return Q.all(_([start, partPromises, end]).flatten().compact().value())
.then(items => {
var prev, next, paths;
2014-12-15 02:37:56 +00:00
2014-12-17 20:12:04 +00:00
this.items = items;
util.spaceHorizontally(items, {
2014-12-17 20:12:04 +00:00
padding: 10
});
2014-12-15 02:37:56 +00:00
prev = util.normalizeBBox(_.first(items).getBBox());
2014-12-17 20:12:04 +00:00
paths = _.map(items.slice(1), item => {
var path;
2014-12-15 02:37:56 +00:00
next = util.normalizeBBox(item.getBBox());
2014-12-17 20:12:04 +00:00
path = `M${prev.ax2},${prev.ay}H${next.ax}`;
prev = next;
2014-12-15 02:37:56 +00:00
2014-12-17 20:12:04 +00:00
return path;
2014-12-16 03:00:24 +00:00
});
2014-12-17 20:12:04 +00:00
this.container.prepend(
this.container.path(paths.join('')));
});
},
setup() {
this.parts = _.reduce(this.properties.parts.elements, function(result, node) {
2014-12-11 01:11:51 +00:00
var last = _.last(result);
2014-12-17 19:56:02 +00:00
if (last && node.canMerge && last.canMerge) {
last.elements[0].merge(node.elements[0]);
2014-12-11 01:11:51 +00:00
} else {
result.push(node);
}
return result;
}, []);
2014-12-15 00:08:14 +00:00
this.anchorStart = this.properties.anchor_start.textValue !== '';
this.anchorEnd = this.properties.anchor_end.textValue !== '';
2014-12-17 20:12:04 +00:00
if (!this.anchorStart && !this.anchorEnd && this.parts.length === 1) {
this.proxy = this.parts[0];
}
}
};