From c2d8473425ba866bb1663a86b97ad278fec3465e Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Sun, 14 Dec 2014 19:08:14 -0500 Subject: [PATCH] Adding node-type-specific anchor code --- src/js/parser/javascript/charset.js | 18 +++++++++---- src/js/parser/javascript/match.js | 14 ++++++++++ src/js/parser/javascript/match_fragment.js | 19 ++++++++++--- src/js/parser/javascript/regexp.js | 31 +++++++++++++++------- src/js/parser/javascript/repeat.js | 5 ++-- src/js/parser/javascript/subexp.js | 11 ++++++++ 6 files changed, 76 insertions(+), 22 deletions(-) diff --git a/src/js/parser/javascript/charset.js b/src/js/parser/javascript/charset.js index 4b0cc85..3045b30 100644 --- a/src/js/parser/javascript/charset.js +++ b/src/js/parser/javascript/charset.js @@ -6,22 +6,30 @@ export default _.extend({}, Base, { type: 'charset', _render() { - var partContainer = this.container.group(); + this.partContainer = this.container.group(); - return Q.all(_.map(this.parts.elements, part => { - return part.render(partContainer.group()); - })) + return Q.all(_.map(this.parts.elements, (part => { + return part.render(this.partContainer.group()); + }).bind(this))) .then((() => { this.spaceVertically(this.parts.elements, { padding: 5 }); - return this.renderLabeledBox(this.invert() ? 'None of:' : 'One of:', partContainer, { + return this.renderLabeledBox(this.invert() ? 'None of:' : 'One of:', this.partContainer, { padding: 5 }); }).bind(this)); }, + _getAnchor() { + var matrix = this.transform().localMatrix; + + return _.extend(Base._getAnchor.call(this), { + ay: matrix.y(0, this.partContainer.getBBox().cy) + }); + }, + invert() { return this._invert.textValue !== ''; } diff --git a/src/js/parser/javascript/match.js b/src/js/parser/javascript/match.js index 526fc49..5a78d04 100644 --- a/src/js/parser/javascript/match.js +++ b/src/js/parser/javascript/match.js @@ -27,6 +27,7 @@ export default _.extend({}, Base, { return Q.all(_([start, partPromises, end]).flatten().compact().value()) .then(((items) => { + this.items = items; this.spaceHorizontally(items, { padding: 10 }); @@ -58,5 +59,18 @@ export default _.extend({}, Base, { return result; }, []); + }, + + _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) + }; } }); diff --git a/src/js/parser/javascript/match_fragment.js b/src/js/parser/javascript/match_fragment.js index cc985fe..b88d4da 100644 --- a/src/js/parser/javascript/match_fragment.js +++ b/src/js/parser/javascript/match_fragment.js @@ -17,17 +17,17 @@ export default _.extend({}, Base, { box = this._content.getBBox(); if (this._repeat.hasSkip()) { - paths.push(Snap.format('M0,{box.cy}q10,0 10,-10v-{vert}q0,-10 10,-10h{horiz}q10,0 10,10v{vert}q0,10 10,10', { + paths.push(Snap.format('M0,{box.ay}q10,0 10,-10v-{vert}q0,-10 10,-10h{horiz}q10,0 10,10v{vert}q0,10 10,10', { box, - vert: box.height / 2 - 10, + vert: Math.max(0, box.ay - box.y - 10), horiz: box.width - 10 })); } if (this._repeat.hasLoop()) { - paths.push(Snap.format('M{box.x},{box.cy}q-10,0 -10,10v{vert}q0,10 10,10h{box.width}q10,0 10,-10v-{vert}q0,-10 -10,-10', { + paths.push(Snap.format('M{box.x},{box.ay}q-10,0 -10,10v{vert}q0,10 10,10h{box.width}q10,0 10,-10v-{vert}q0,-10 -10,-10', { box, - vert: box.height / 2 - 10 + vert: box.y2 - box.ay - 10 })); } @@ -37,5 +37,16 @@ export default _.extend({}, Base, { } }).bind(this)); } + }, + + _getAnchor() { + var anchor = this._content.getAnchor(), + matrix = this.transform().localMatrix; + + return _.extend(anchor, { + ax: matrix.x(anchor.ax, anchor.ay), + ax2: matrix.x(anchor.ax2, anchor.ay), + ay: matrix.y(anchor.ax, anchor.ay) + }); } }); diff --git a/src/js/parser/javascript/regexp.js b/src/js/parser/javascript/regexp.js index 7513bda..1a784ad 100644 --- a/src/js/parser/javascript/regexp.js +++ b/src/js/parser/javascript/regexp.js @@ -31,16 +31,8 @@ export default _.extend({}, Base, { containerBox = this.getBBox(); paths = _.map(matches, this.makeConnectorLine.bind(this, containerBox)); - paths.push(Snap.format([ - 'M0,{box.cy}q10,0 10,-10V{top}', - 'M0,{box.cy}q10,0 10,10V{bottom}', - 'M{box.width},{box.cy}m40,0q-10,0 -10,-10V{top}', - 'M{box.width},{box.cy}m40,0q-10,0 -10,10V{bottom}' - ].join(''), { - box: containerBox, - top: _.first(matches).getBBox().ay + 10, - bottom: _.last(matches).getBBox().ay - 10 - })); + paths.push(this.makeSideLine(containerBox, _.first(matches))); + paths.push(this.makeSideLine(containerBox, _.last(matches))); this.container.prepend( this.container.path(paths.join(''))); @@ -48,6 +40,25 @@ export default _.extend({}, Base, { } }, + makeSideLine(containerBox, match) { + var box = match.getBBox(), + direction = box.ay > containerBox.cy ? 1 : -1, + distance = Math.abs(box.ay - containerBox.cy); + + if (distance >= 15) { + return Snap.format([ + 'M0,{box.cy}q10,0 10,{shift}V{edge}', + 'M{box.width},{box.cy}m40,0q-10,0 -10,{shift}V{edge}' + ].join(''), { + box: containerBox, + edge: box.ay - 10 * direction, + shift: 10 * direction + }); + } else { + return ''; + } + }, + makeConnectorLine(containerBox, match) { var box = match.getBBox(), direction = box.ay > containerBox.cy ? 1 : -1, diff --git a/src/js/parser/javascript/repeat.js b/src/js/parser/javascript/repeat.js index 259f66c..a6597ee 100644 --- a/src/js/parser/javascript/repeat.js +++ b/src/js/parser/javascript/repeat.js @@ -20,17 +20,16 @@ export default { }, contentPosition() { - var x = 0, y = 0; + var x = 0; if (this.hasLoop()) { x = 10; } if (this.hasSkip()) { - y = 10; x = 15; } - return Snap.matrix().translate(x, y); + return Snap.matrix().translate(x, 0); } } diff --git a/src/js/parser/javascript/subexp.js b/src/js/parser/javascript/subexp.js index b2b9334..4bec1b0 100644 --- a/src/js/parser/javascript/subexp.js +++ b/src/js/parser/javascript/subexp.js @@ -36,4 +36,15 @@ export default _.extend({}, Base, { resetCounter() { groupCounter = 1; }, + + _getAnchor() { + var anchor = this.regexp.getAnchor(), + matrix = this.transform().localMatrix; + + return _.extend(anchor, { + ax: matrix.x(anchor.ax, anchor.ay), + ax2: matrix.x(anchor.ax2, anchor.ay), + ay: matrix.y(anchor.ax, anchor.ay) + }); + } });