From fc7032241f3867a1f71d4977cc6e43bec51f840d Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Wed, 10 Dec 2014 16:52:57 -0500 Subject: [PATCH] Short-circuiting the render of nodes. This will reduce the depth of nesting of elements (and hopefully make final positioning easier) --- src/js/parser/javascript/match.js | 64 +++++++++++++--------- src/js/parser/javascript/match_fragment.js | 6 +- src/js/parser/javascript/regexp.js | 28 ++++++---- 3 files changed, 61 insertions(+), 37 deletions(-) diff --git a/src/js/parser/javascript/match.js b/src/js/parser/javascript/match.js index ab6d2e6..2e7ce06 100644 --- a/src/js/parser/javascript/match.js +++ b/src/js/parser/javascript/match.js @@ -5,42 +5,54 @@ export default _.extend({}, Base, { type: 'match', render() { - this.contents = {}; + var parts = this.parts(); - if (this.anchorStart()) { - this.contents.anchor_start = this.renderLabel(this.container, 'Start of line') - .addClass('anchor'); - } + if (this.anchorStart() || this.anchorEnd() || parts.length !== 1) { + this.contents = {}; - this.contents.parts = _.map(this.parts(), (function(part) { - part.setContainer(this.container.group()); - part.render(); - return part; - }).bind(this)); + if (this.anchorStart()) { + this.contents.anchor_start = this.renderLabel(this.container, 'Start of line') + .addClass('anchor'); + } - if (this.anchorEnd()) { - this.contents.anchor_end = this.renderLabel(this.container, 'End of line') - .addClass('anchor'); + this.contents.parts = _.map(parts, (function(part) { + part.setContainer(this.container.group()); + part.render(); + return part; + }).bind(this)); + + if (this.anchorEnd()) { + this.contents.anchor_end = this.renderLabel(this.container, 'End of line') + .addClass('anchor'); + } + } else { + this.content = parts[0]; + this.content.setContainer(this.container); + this.content.render(); } }, position() { var items; - if (this.anchorStart()) { - this.positionLabel(this.contents.anchor_start); + if (this.contents) { + if (this.contents.anchor_start) { + this.positionLabel(this.contents.anchor_start); + } + + if (this.contents.anchor_end) { + this.positionLabel(this.contents.anchor_end); + } + + _.invoke(this.contents.parts, 'position'); + + items = _(this.contents).values().flatten().value(); + this.spaceHorizontally(items, { + padding: 10 + }); + } else { + this.content.position(); } - - if (this.anchorEnd()) { - this.positionLabel(this.contents.anchor_end); - } - - _.invoke(this.contents.parts, 'position'); - - items = _(this.contents).values().flatten().value(); - this.spaceHorizontally(items, { - padding: 10 - }); }, anchorStart() { diff --git a/src/js/parser/javascript/match_fragment.js b/src/js/parser/javascript/match_fragment.js index f022b61..6485483 100644 --- a/src/js/parser/javascript/match_fragment.js +++ b/src/js/parser/javascript/match_fragment.js @@ -5,7 +5,11 @@ export default _.extend({}, Base, { type: 'match-fragment', render() { - this._content.setContainer(this.container.group()); + if (this._repeat.textValue === '') { + this._content.setContainer(this.container); + } else { + this._content.setContainer(this.container.group()); + } this._content.render(); }, diff --git a/src/js/parser/javascript/regexp.js b/src/js/parser/javascript/regexp.js index 0792944..62d285c 100644 --- a/src/js/parser/javascript/regexp.js +++ b/src/js/parser/javascript/regexp.js @@ -5,13 +5,21 @@ export default _.extend({}, Base, { type: 'regexp', render() { - this.matchContainer = this.container.group(); + var matches = this.matches(); - _.each(this.matches(), (match => { - match.setContainer(this.matchContainer.group()); - match.render(); - return match.container; - }).bind(this)); + if (matches.length === 1) { + matches[0].setContainer(this.container); + matches[0].render(); + } else { + this.matchContainer = this.container.group() + .addClass('regexp-matches'); + + _.each(matches, (match => { + match.setContainer(this.matchContainer.group()); + match.render(); + return match.container; + }).bind(this)); + } }, position() { @@ -22,11 +30,11 @@ export default _.extend({}, Base, { _.invoke(matches, 'position'); - this.spaceVertically(matches, { - padding: 5 - }); - if (includeLines) { + this.spaceVertically(matches, { + padding: 5 + }); + this.matchContainer.transform(Snap.matrix() .translate(20, 0));