Skip to content

Commit

Permalink
Compare distance between cursor with absolute value. (#4295)
Browse files Browse the repository at this point in the history
* fix with Math.abs

* ok this works

* added test

* added test file

* linting tests

* wrong test
  • Loading branch information
asturur authored Sep 7, 2017
1 parent 545686e commit 0f26563
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/mixins/itext_click_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
30 changes: 30 additions & 0 deletions test/unit/itext_click_behaviour.js
Original file line number Diff line number Diff line change
@@ -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');
});
})();

0 comments on commit 0f26563

Please sign in to comment.