Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for <sub> and <sup> in "extra" hover label #3443

Merged
merged 6 commits into from
Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions src/components/fx/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -925,15 +925,10 @@ function createHoverText(hoverData, opts, gd) {
if(d.nameOverride !== undefined) d.name = d.nameOverride;

if(d.name) {
// strip out our pseudo-html elements from d.name (if it exists at all)
name = svgTextUtils.plainText(d.name || '');

var nameLength = Math.round(d.nameLength);

if(nameLength > -1 && name.length > nameLength) {
if(nameLength > 3) name = name.substr(0, nameLength - 3) + '...';
else name = name.substr(0, nameLength);
}
name = svgTextUtils.plainText(d.name || '', {
len: d.nameLength,
allowedTags: ['br', 'sub', 'sup', 'b', 'i', 'em']
});
}

if(d.zLabel !== undefined) {
Expand Down Expand Up @@ -995,6 +990,7 @@ function createHoverText(hoverData, opts, gd) {

var tx2 = g.select('text.name');
var tx2width = 0;
var tx2height = 0;

// secondary label for non-empty 'name'
if(name && name !== text) {
Expand All @@ -1006,18 +1002,20 @@ function createHoverText(hoverData, opts, gd) {
.attr('data-notex', 1)
.call(svgTextUtils.positionText, 0, 0)
.call(svgTextUtils.convertToTspans, gd);
tx2width = tx2.node().getBoundingClientRect().width + 2 * HOVERTEXTPAD;
}
else {

var t2bb = tx2.node().getBoundingClientRect();
tx2width = t2bb.width + 2 * HOVERTEXTPAD;
tx2height = t2bb.height + 2 * HOVERTEXTPAD;
} else {
tx2.remove();
g.select('rect').remove();
}

g.select('path')
.style({
fill: numsColor,
stroke: contrastColor
});
g.select('path').style({
fill: numsColor,
stroke: contrastColor
});

var tbb = tx.node().getBoundingClientRect();
var htx = d.xa._offset + (d.x0 + d.x1) / 2;
var hty = d.ya._offset + (d.y0 + d.y1) / 2;
Expand All @@ -1028,7 +1026,7 @@ function createHoverText(hoverData, opts, gd) {

d.ty0 = outerTop - tbb.top;
d.bx = tbb.width + 2 * HOVERTEXTPAD;
d.by = tbb.height + 2 * HOVERTEXTPAD;
d.by = Math.max(tbb.height + 2 * HOVERTEXTPAD, tx2height);
d.anchor = 'start';
d.txwidth = tbb.width;
d.tx2width = tx2width;
Expand Down
66 changes: 60 additions & 6 deletions src/lib/svg_text_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,6 @@ var ZERO_WIDTH_SPACE = '\u200b';
*/
var PROTOCOLS = ['http:', 'https:', 'mailto:', '', undefined, ':'];

var STRIP_TAGS = new RegExp('</?(' + Object.keys(TAG_STYLES).join('|') + ')( [^>]*)?/?>', 'g');

var NEWLINES = /(\r\n?|\n)/g;

var SPLIT_TAGS = /(<[^<>]*>)/;
Expand Down Expand Up @@ -315,10 +313,66 @@ function getQuotedMatch(_str, re) {

var COLORMATCH = /(^|;)\s*color:/;

exports.plainText = function(_str) {
// strip out our pseudo-html so we have a readable
// version to put into text fields
return (_str || '').replace(STRIP_TAGS, ' ');
/**
* Strip string of tags
*
* @param {string} _str : input string
* @param {object} opts :
* - maxLen {number} max length of output string
* - allowedTags {array} list of pseudo-html tags to NOT strip
* @return {string}
*/
exports.plainText = function(_str, opts) {
opts = opts || {};

var len = (opts.len !== undefined && opts.len !== -1) ? opts.len : Infinity;
var allowedTags = opts.allowedTags !== undefined ? opts.allowedTags : ['br'];

var ellipsis = '...';
var eLen = ellipsis.length;

var oldParts = _str.split(SPLIT_TAGS);
var newParts = [];
var prevTag = '';
var l = 0;

for(var i = 0; i < oldParts.length; i++) {
var p = oldParts[i];
var match = p.match(ONE_TAG);
var tagType = match && match[2].toLowerCase();

if(tagType) {
// N.B. tags do not count towards string length
if(allowedTags.indexOf(tagType) !== -1) {
newParts.push(p);
prevTag = tagType;
}
} else {
var pLen = p.length;

if((l + pLen) < len) {
newParts.push(p);
l += pLen;
} else if(l < len) {
var pLen2 = len - l;

if(prevTag && (prevTag !== 'br' || pLen2 <= eLen || pLen <= eLen)) {
newParts.pop();
}

if(len > eLen) {
newParts.push(p.substr(0, pLen2 - eLen) + ellipsis);
} else {
newParts.push(p.substr(0, pLen2));
}
break;
}

prevTag = '';
}
}

return newParts.join('');
};

/*
Expand Down
Loading