mirror of
https://github.com/Hopiu/fabric.js.git
synced 2026-03-16 22:10:32 +00:00
Remove chars from to (#4495)
* trying to fix * make removeChars * fixed doc * added test
This commit is contained in:
parent
0730e9cda2
commit
a9eacc4d74
2 changed files with 20 additions and 41 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
});
|
||||
})();
|
||||
|
|
|
|||
Loading…
Reference in a new issue