Remove chars from to (#4495)

* trying to fix

* make removeChars

* fixed doc

* added test
This commit is contained in:
Andrea Bogazzi 2017-11-24 17:26:38 +01:00 committed by GitHub
parent 0730e9cda2
commit a9eacc4d74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 41 deletions

View file

@ -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
* 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(e) {
if (this.selectionStart === this.selectionEnd) {
this._removeCharsNearCursor(e);
removeChars: function(start, end) {
if (typeof end === 'undefined') {
end = start + 1;
}
else {
this._removeCharsFromTo(this.selectionStart, this.selectionEnd);
}
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);
}
}
});

View file

@ -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');
});
})();