diff --git a/src/mixins/itext_click_behavior.mixin.js b/src/mixins/itext_click_behavior.mixin.js index 34d82ad1ed7..739e091253e 100644 --- a/src/mixins/itext_click_behavior.mixin.js +++ b/src/mixins/itext_click_behavior.mixin.js @@ -196,10 +196,11 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot * @private */ _getNewSelectionStartFromOffset: function(mouseOffset, prevWidth, width, index, jlen) { - + // we need Math.abs because when width is after the last char, the offset is given as 1, while is 0 var distanceBtwLastCharAndCursor = mouseOffset.x - prevWidth, distanceBtwNextCharAndCursor = width - mouseOffset.x, - offset = distanceBtwNextCharAndCursor > distanceBtwLastCharAndCursor ? 0 : 1, + offset = distanceBtwNextCharAndCursor > distanceBtwLastCharAndCursor || + distanceBtwNextCharAndCursor < 0 ? 0 : 1, newSelectionStart = index + offset; // if object is horizontally flipped, mirror cursor location from the end if (this.flipX) { diff --git a/test.js b/test.js index 9404a3b9e41..fe0a28deab1 100644 --- a/test.js +++ b/test.js @@ -39,6 +39,7 @@ testrunner.run({ './test/unit/object_geometry.js', './test/unit/object_origin.js', './test/unit/itext.js', + './test/unit/itext_click_behaviour.js', './test/unit/itext_key_behaviour.js', './test/unit/collection.js', './test/unit/point.js', diff --git a/test/unit/itext_click_behaviour.js b/test/unit/itext_click_behaviour.js new file mode 100644 index 00000000000..55b3ec0dd10 --- /dev/null +++ b/test/unit/itext_click_behaviour.js @@ -0,0 +1,30 @@ +(function(){ + test('_getNewSelectionStartFromOffset end of line', function() { + var iText = new fabric.IText('test need some word\nsecond line'); + var index = 10; + var jlen = 20; + var selection = iText._getNewSelectionStartFromOffset({ y: 1, x: 1000 }, 500, 520, index, jlen); + equal(selection, index, 'index value did not change'); + }); + test('_getNewSelectionStartFromOffset middle of line', function() { + var iText = new fabric.IText('test need some word\nsecond line'); + var index = 10; + var jlen = 20; + var selection = iText._getNewSelectionStartFromOffset({ y: 1, x: 519 }, 500, 520, index, jlen); + equal(selection, index + 1, 'index value was moved to next char, since is very near'); + }); + test('_getNewSelectionStartFromOffset middle of line', function() { + var iText = new fabric.IText('test need some word\nsecond line'); + var index = 10; + var jlen = 20; + var selection = iText._getNewSelectionStartFromOffset({ y: 1, x: 502 }, 500, 520, index, jlen); + equal(selection, index, 'index value was NOT moved to next char, since is very near to first one'); + }); + test('_getNewSelectionStartFromOffset middle of line', function() { + var iText = new fabric.IText('test need some word\nsecond line'); + var index = 10; + var jlen = 10; + var selection = iText._getNewSelectionStartFromOffset({ y: 1, x: 1000 }, 500, 520, index, jlen); + equal(selection, index, 'index value was NOT moved to next char, since is already at end of text'); + }); +})();