Skip to content

Commit

Permalink
ISSUE-5867 Fix the extra new line selection with empty line (fabricjs…
Browse files Browse the repository at this point in the history
  • Loading branch information
asturur authored Dec 8, 2019
1 parent 17267a6 commit 9b5b4db
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
1 change: 1 addition & 0 deletions HEADER.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ fabric.SHARED_ATTRIBUTES = [
fabric.DPI = 96;
fabric.reNum = '(?:[-+]?(?:\\d+|\\d*\\.\\d+)(?:[eE][-+]?\\d+)?)';
fabric.rePathCommand = /([-+]?((\d+\.\d+)|((\d+)|(\.\d+)))(?:[eE][-+]?\d+)?)/ig;
fabric.reNonWord = /[ \n\.,;!\?\-]/;
fabric.fontPaths = { };
fabric.iMatrix = [1, 0, 0, 1, 0, 0];

Expand Down
14 changes: 8 additions & 6 deletions src/mixins/itext_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,17 @@
* @return {Number} Index of the beginning or end of a word
*/
searchWordBoundary: function(selectionStart, direction) {
var index = this._reSpace.test(this._text[selectionStart]) ? selectionStart - 1 : selectionStart,
_char = this._text[index],
reNonWord = /[ \n\.,;!\?\-]/;
var text = this._text,
index = this._reSpace.test(text[selectionStart]) ? selectionStart - 1 : selectionStart,
_char = text[index],
// wrong
reNonWord = fabric.reNonWord;

while (!reNonWord.test(_char) && index > 0 && index < this._text.length) {
while (!reNonWord.test(_char) && index > 0 && index < text.length) {
index += direction;
_char = this._text[index];
_char = text[index];
}
if (reNonWord.test(_char) && _char !== '\n') {
if (reNonWord.test(_char)) {
index += direction === 1 ? 0 : 1;
}
return index;
Expand Down
6 changes: 6 additions & 0 deletions src/mixins/itext_click_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,19 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
* Default handler for double click, select a word
*/
doubleClickHandler: function(options) {
if (!this.isEditing) {
return;
}
this.selectWord(this.getSelectionStartFromPointer(options.e));
},

/**
* Default handler for triple click, select a line
*/
tripleClickHandler: function(options) {
if (!this.isEditing) {
return;
}
this.selectLine(this.getSelectionStartFromPointer(options.e));
},

Expand Down
36 changes: 35 additions & 1 deletion test/unit/itext_click_behaviour.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
clientX: 40,
clientY: 10
};
iText.enterEditing();
iText.doubleClickHandler({
e: eventData
});
Expand All @@ -38,8 +39,24 @@
iText.doubleClickHandler({
e: eventData
});
assert.equal(iText.selectionStart, 19, 'second dblClcik selection start is');
assert.equal(iText.selectionStart, 20, 'second dblClcik selection start is');
assert.equal(iText.selectionEnd, 26, 'second dblClcik selection end is');
iText.exitEditing();
});
QUnit.test('doubleClickHandler no editing', function(assert) {
var iText = new fabric.IText('test need some word\nsecond line');
iText.canvas = canvas;
var eventData = {
which: 1,
target: canvas.upperCanvasEl,
clientX: 40,
clientY: 10
};
iText.doubleClickHandler({
e: eventData
});
assert.equal(iText.selectionStart, 0, 'dblClcik selection start is');
assert.equal(iText.selectionEnd, 0, 'dblClcik selection end is');
});
QUnit.test('tripleClickHandler', function(assert) {
var iText = new fabric.IText('test need some word\nsecond line');
Expand All @@ -50,6 +67,7 @@
clientX: 40,
clientY: 10
};
iText.enterEditing();
iText.tripleClickHandler({
e: eventData
});
Expand All @@ -66,6 +84,22 @@
});
assert.equal(iText.selectionStart, 20, 'second tripleClick selection start is');
assert.equal(iText.selectionEnd, 31, 'second tripleClick selection end is');
iText.exitEditing();
});
QUnit.test('tripleClickHandler', function(assert) {
var iText = new fabric.IText('test need some word\nsecond line');
iText.canvas = canvas;
var eventData = {
which: 1,
target: canvas.upperCanvasEl,
clientX: 40,
clientY: 10
};
iText.tripleClickHandler({
e: eventData
});
assert.equal(iText.selectionStart, 0, 'tripleClick selection start is');
assert.equal(iText.selectionEnd, 0, 'tripleClick selection end is');
});
QUnit.test('_getNewSelectionStartFromOffset end of line', function(assert) {
var iText = new fabric.IText('test need some word\nsecond line');
Expand Down

0 comments on commit 9b5b4db

Please sign in to comment.