Skip to content

Commit

Permalink
fix text minimum dimensions (#3312)
Browse files Browse the repository at this point in the history
* fix text dim
* add tests
  • Loading branch information
asturur committed Oct 2, 2016
1 parent 224309a commit cd4c465
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/shapes/text.class.js
Expand Up @@ -6,7 +6,8 @@
extend = fabric.util.object.extend,
clone = fabric.util.object.clone,
toFixed = fabric.util.toFixed,
NUM_FRACTION_DIGITS = fabric.Object.NUM_FRACTION_DIGITS;
NUM_FRACTION_DIGITS = fabric.Object.NUM_FRACTION_DIGITS,
MIN_TEXT_WIDTH = 2;

if (fabric.Text) {
fabric.warn('fabric.Text is already defined');
Expand Down Expand Up @@ -359,7 +360,7 @@
}
this._textLines = this._splitTextIntoLines();
this._clearCache();
this.width = this._getTextWidth(ctx);
this.width = this._getTextWidth(ctx) || this.cursorWidth || MIN_TEXT_WIDTH;
this.height = this._getTextHeight(ctx);
},

Expand Down
4 changes: 3 additions & 1 deletion src/shapes/textbox.class.js
Expand Up @@ -34,10 +34,12 @@

/**
* Minimum calculated width of a textbox, in pixels.
* fixed to 2 so that an empty textbox cannot go to 0
* and is still selectable without text.
* @type Number
* @default
*/
dynamicMinWidth: 0,
dynamicMinWidth: 2,

/**
* Cached array of text wrapping.
Expand Down
5 changes: 5 additions & 0 deletions test/unit/itext.js
Expand Up @@ -151,6 +151,11 @@
equal(iText.selectionEnd, 0);
});

test('empty itext', function() {
var iText = new fabric.IText('');
equal(iText.width, iText.cursorWidth);
})

test('setSelectionEnd', function() {
var iText = new fabric.IText('test');

Expand Down
5 changes: 5 additions & 0 deletions test/unit/text.js
Expand Up @@ -270,6 +270,11 @@
equal(text.width, CHAR_WIDTH * 2);
});

test('dimensions without text', function() {
var text = new fabric.Text('');
equal(text.width, 2);
});

test('setting fontFamily', function() {
var text = new fabric.Text('x');
text.path = 'foobar.js';
Expand Down

0 comments on commit cd4c465

Please sign in to comment.