Skip to content

Commit

Permalink
Remove chars from to (#4495)
Browse files Browse the repository at this point in the history
* trying to fix

* make removeChars

* fixed doc

* added test
  • Loading branch information
asturur authored Nov 24, 2017
1 parent 0730e9c commit a9eacc4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 43 deletions.
56 changes: 13 additions & 43 deletions src/mixins/itext_key_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -591,55 +591,25 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
},

/**
* Removes characters selected by selection
* @param {Event} e Event object
*/
removeChars: function(e) {
if (this.selectionStart === this.selectionEnd) {
this._removeCharsNearCursor(e);
}
else {
this._removeCharsFromTo(this.selectionStart, this.selectionEnd);
}

* Removes characters from start/end
* start/end ar per grapheme position in _text array.
*
* @param {Number} start
* @param {Number} end default to start + 1
*/
removeChars: function(start, end) {
if (typeof end === 'undefined') {
end = start + 1;
}
this.removeStyleFromTo(start, end);
this._text.splice(start, end - start);
this.text = this._text.join('');
this.set('dirty', true);
this.setSelectionEnd(this.selectionStart);

this._removeExtraneousStyles();
if (this._shouldClearDimensionCache()) {
this.initDimensions();
this.setCoords();
}
this.canvas && this.canvas.requestRenderAll();
this.fire('changed');
this.canvas && this.canvas.fire('text:changed', { target: this });
},

/**
* @private
* @param {Event} e Event object
*/
_removeCharsNearCursor: function(e) {
if (this.selectionStart === 0) {
return;
}
if (e.metaKey) {
// remove all till the start of current line
var leftLineBoundary = this.findLineBoundaryLeft(this.selectionStart);

this._removeCharsFromTo(leftLineBoundary, this.selectionStart);
this.setSelectionStart(leftLineBoundary);
}
else if (e.altKey) {
// remove all till the start of current word
var leftWordBoundary = this.findWordBoundaryLeft(this.selectionStart);

this._removeCharsFromTo(leftWordBoundary, this.selectionStart);
this.setSelectionStart(leftWordBoundary);
}
else {
this._removeSingleCharAndStyle(this.selectionStart);
this.setSelectionStart(this.selectionStart - 1);
}
}
});
9 changes: 9 additions & 0 deletions test/unit/itext_key_behaviour.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,13 @@
assert.equal(iText.styles[0][1].fontSize, undefined, 'style had not fontSize');
assert.equal(fabric.copiedTextStyle[1].fontSize, 25, 'style took fontSize from text element');
});

QUnit.test('removeChars', function(assert) {
var iText = new fabric.IText('test', { fontSize: 25, styles: { 0: { 0: { fill: 'red' }, 1: { fill: 'blue' }}}});
assert.ok(typeof iText.removeChars === 'function');
iText.removeChars(1,3);
assert.equal(iText.text, 'tt', 'text has been remoed');
assert.deepEqual(iText._text, ['t','t'], 'text has been remoed');
assert.equal(iText.styles[0][1], undefined, 'style has been removed');
});
})();

0 comments on commit a9eacc4

Please sign in to comment.