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

92 lines
2.3 KiB
JavaScript
Raw Normal View History

import _ from 'lodash';
import Q from 'q';
export default {
type: 'match',
_render() {
var start, end,
parts = this.parts(),
partPromises;
2014-12-11 00:58:36 +00:00
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
}
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
}
if (start || end || parts.length !== 1) {
2014-12-16 03:00:24 +00:00
partPromises = _.map(parts, part => {
return part.render(this.container.group());
2014-12-16 03:00:24 +00:00
});
return Q.all(_([start, partPromises, end]).flatten().compact().value())
2014-12-16 03:00:24 +00:00
.then(items => {
2014-12-15 02:37:56 +00:00
var prev, next, paths;
2014-12-15 00:08:14 +00:00
this.items = items;
this.spaceHorizontally(items, {
padding: 10
});
2014-12-15 02:37:56 +00:00
prev = this.normalizeBBox(_.first(items).getBBox());
2014-12-16 03:00:24 +00:00
paths = _.map(items.slice(1), item => {
2014-12-15 02:37:56 +00:00
var path;
next = this.normalizeBBox(item.getBBox());
path = `M${prev.ax2},${prev.ay}H${next.ax}`;
2014-12-15 02:37:56 +00:00
prev = next;
return path;
2014-12-16 03:00:24 +00:00
});
2014-12-15 02:37:56 +00:00
this.container.prepend(
this.container.path(paths.join('')));
2014-12-16 03:00:24 +00:00
});
} else {
return this.proxy(parts[0]);
}
},
2014-12-09 23:08:40 +00:00
anchorStart() {
return this._anchor_start.textValue !== '';
},
2014-12-09 23:08:40 +00:00
anchorEnd() {
return this._anchor_end.textValue !== '';
},
parts() {
return _.reduce(this._parts.elements, function(result, node) {
2014-12-11 01:11:51 +00:00
var last = _.last(result);
if (last && node.elements[0].type === 'literal' && node.elements[1].textValue === '' && last.elements[0].type === 'literal' && last.elements[1].textValue === '') {
last.textValue += node.textValue;
last.elements[0].textValue += node.elements[0].textValue;
last.elements[0].literal.textValue += node.elements[0].literal.textValue;
} else {
result.push(node);
}
return result;
}, []);
2014-12-15 00:08:14 +00:00
},
_getAnchor() {
var start = this.normalizeBBox(_.first(this.items).getBBox()),
end = this.normalizeBBox(_.last(this.items).getBBox()),
matrix = this.transform().localMatrix;
return {
atype: [start.atype, end.atype].join('/'),
ax: matrix.x(start.ax, start.ay),
ax2: matrix.x(end.ax2, end.ay),
ay: matrix.y(start.ax, start.ay)
};
}
};