mirror of
https://github.com/Hopiu/fabric.js.git
synced 2026-05-04 11:54:47 +00:00
Compare distance between cursor with absolute value. (#4295)
* fix with Math.abs * ok this works * added test * added test file * linting tests * wrong test
This commit is contained in:
parent
545686ed96
commit
0f265631b3
3 changed files with 34 additions and 2 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
1
test.js
1
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',
|
||||
|
|
|
|||
30
test/unit/itext_click_behaviour.js
Normal file
30
test/unit/itext_click_behaviour.js
Normal file
|
|
@ -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');
|
||||
});
|
||||
})();
|
||||
Loading…
Reference in a new issue