From a9eacc4d745675c5c718ddb8bb59a48371c3dbb5 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Fri, 24 Nov 2017 17:26:38 +0100 Subject: [PATCH] Remove chars from to (#4495) * trying to fix * make removeChars * fixed doc * added test --- src/mixins/itext_key_behavior.mixin.js | 52 ++++++-------------------- test/unit/itext_key_behaviour.js | 9 +++++ 2 files changed, 20 insertions(+), 41 deletions(-) diff --git a/src/mixins/itext_key_behavior.mixin.js b/src/mixins/itext_key_behavior.mixin.js index 86e7de57..fb1496e6 100644 --- a/src/mixins/itext_key_behavior.mixin.js +++ b/src/mixins/itext_key_behavior.mixin.js @@ -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); - } - } }); diff --git a/test/unit/itext_key_behaviour.js b/test/unit/itext_key_behaviour.js index 1890e9e9..61c8975e 100644 --- a/test/unit/itext_key_behaviour.js +++ b/test/unit/itext_key_behaviour.js @@ -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'); + }); })();