diff --git a/lib/network/modules/components/shared/Label.js b/lib/network/modules/components/shared/Label.js index d60d0c64c0..c8a52c20da 100644 --- a/lib/network/modules/components/shared/Label.js +++ b/lib/network/modules/components/shared/Label.js @@ -43,7 +43,7 @@ class Label { this.labelDirty = true; } else { // Bad label! Change the option value to prevent bad stuff happening - options.label = ''; + options.label = undefined } if (options.font !== undefined && options.font !== null) { // font options can be deleted at various levels diff --git a/lib/network/modules/components/shared/LabelSplitter.js b/lib/network/modules/components/shared/LabelSplitter.js index bc3d5dc60a..b9d6402852 100644 --- a/lib/network/modules/components/shared/LabelSplitter.js +++ b/lib/network/modules/components/shared/LabelSplitter.js @@ -290,7 +290,8 @@ class LabelSplitter { constructor(ctx, parent, selected, hover) { this.ctx = ctx; this.parent = parent; - + this.selected = selected; + this.hover = hover; /** * Callback to determine text width; passed to LabelAccumulator instance @@ -303,13 +304,11 @@ class LabelSplitter { if (text === undefined) return 0; // TODO: This can be done more efficiently with caching + // This will set the ctx.font correctly, depending on selected/hover and mod - so that ctx.measureText() will be accurate. let values = this.parent.getFormattingValues(ctx, selected, hover, mod); let width = 0; if (text !== '') { - // NOTE: The following may actually be *incorrect* for the mod fonts! - // This returns the size with a regular font, bold etc. may - // have different sizes. let measure = this.ctx.measureText(text); width = measure.width; } @@ -616,6 +615,11 @@ class LabelSplitter { * @private */ splitStringIntoLines(str, mod = 'normal', appendLast = false) { + // Set the canvas context font, based upon the current selected/hover state + // and the provided mod, so the text measurement performed by getLongestFit + // will be accurate - and not just use the font of whoever last used the canvas. + this.parent.getFormattingValues(this.ctx, this.selected, this.hover, mod) + // Still-present spaces are relevant, retain them str = str.replace(/^( +)/g, '$1\r'); str = str.replace(/([^\r][^ ]*)( +)/g, '$1\r$2\r'); diff --git a/test/Label.test.js b/test/Label.test.js index e434298a64..05d768cd5c 100644 --- a/test/Label.test.js +++ b/test/Label.test.js @@ -18,7 +18,7 @@ import canvasMockify from './canvas-mock'; import Label from '../lib/network/modules/components/shared/Label'; import NodesHandler from '../lib/network/modules/NodesHandler'; import Network from '../lib/network/Network'; - +import ComponentUtil from '../lib/network/modules/components/shared/ComponentUtil'; /************************************************************** * Dummy class definitions for minimal required functionality. @@ -1619,6 +1619,45 @@ describe('Shorthand Font Options', function() { var options = {}; var network = new Network(container, data, options); - done(); - }); -}); + done() + }) + + describe('visible function', function() { + it('correctly determines label is not visible when label is invalid', function(done) { + var invalidLabel = '' + assert( + !ComponentUtil.isValidLabel(invalidLabel), + 'An empty string should be identified as an invalid label' + ) + + var body = { + view: { + scale: 1 + } + } + + var options = { + label: invalidLabel, + font: { + size: 12 + }, + scaling: { + label: { + drawThreshold: 1 + } + } + } + + var label = new Label(body, options) + label.size.width = 1 + label.size.height = 1 + + assert( + !label.visible(), + 'Label should not be visible because the label text is invalid' + ) + + done() + }) + }) +}) diff --git a/test/network/textMeasurementOnHoverTest.html b/test/network/textMeasurementOnHoverTest.html new file mode 100644 index 0000000000..feba3b3e23 --- /dev/null +++ b/test/network/textMeasurementOnHoverTest.html @@ -0,0 +1,117 @@ + + + + Text Measurement Test + + + + + + + + + + +

Hover over and move the nodes - moving from one to the next and back and forward.

+

They should NOT change size and their line break should not change.

+ +
+ + + + +