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

Compare distance between cursor with absolute value. #4295

Merged
merged 6 commits into from
Sep 7, 2017
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
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');
});
})();