Skip to content

Commit

Permalink
Merge pull request #2502 from asturur/fix-ite
Browse files Browse the repository at this point in the history
Fix itext onInput with selected text
  • Loading branch information
kangax committed Oct 5, 2015
2 parents 06ed44a + c75756d commit b4a2bf6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 52 deletions.
24 changes: 12 additions & 12 deletions src/mixins/itext_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,35 +703,35 @@
lineIndex = cursorLocation.lineIndex,
charIndex = cursorLocation.charIndex;

if (isBeginningOfLine) {
this._removeStyleObject(isBeginningOfLine, cursorLocation, lineIndex, charIndex);
},

_getTextOnPreviousLine: function(lIndex) {
return this._textLines[lIndex - 1];
},

_removeStyleObject: function(isBeginningOfLine, cursorLocation, lineIndex, charIndex) {

var textOnPreviousLine = this._textLines[lineIndex - 1],
newCharIndexOnPrevLine = textOnPreviousLine
? textOnPreviousLine.length
: 0;
if (isBeginningOfLine) {
var textOnPreviousLine = this._getTextOnPreviousLine(cursorLocation.lineIndex),
newCharIndexOnPrevLine = textOnPreviousLine ? textOnPreviousLine.length : 0;

if (!this.styles[lineIndex - 1]) {
this.styles[lineIndex - 1] = {};
}

for (charIndex in this.styles[lineIndex]) {
this.styles[lineIndex - 1][parseInt(charIndex, 10) + newCharIndexOnPrevLine]
= this.styles[lineIndex][charIndex];
}

this.shiftLineStyles(lineIndex, -1);

this.shiftLineStyles(cursorLocation.lineIndex, -1);
}
else {
var currentLineStyles = this.styles[lineIndex];

if (currentLineStyles) {
delete currentLineStyles[charIndex];
//console.log('deleting', lineIndex, charIndex + offset);
}

var currentLineStylesCloned = clone(currentLineStyles);

// shift all styles by 1 backwards
for (var i in currentLineStylesCloned) {
var numericIndex = parseInt(i, 10);
Expand Down
9 changes: 5 additions & 4 deletions src/mixins/itext_key_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
* @param {Event} e Event object
*/
onInput: function(e) {
if (!this.isEditing || this._cancelOnInput) {
this._cancelOnInput = false;
if (!this.isEditing) {
return;
}
var offset = this.selectionStart || 0,
offsetEnd = this.selectionEnd || 0,
textLength = this.text.length,
newTextLength = this.hiddenTextarea.value.length,
diff = newTextLength - textLength,
diff = newTextLength - textLength + offsetEnd - offset,
charsToInsert = this.hiddenTextarea.value.slice(offset, offset + diff);
this.insertChars(charsToInsert);
e.stopPropagation();
Expand Down Expand Up @@ -149,7 +149,8 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
if (copiedText) {
this.insertChars(copiedText, useCopiedStyle);
}
this._cancelOnInput = true;
e.stopImmediatePropagation();
e.preventDefault();
},

/**
Expand Down
37 changes: 1 addition & 36 deletions src/mixins/textbox_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,42 +142,7 @@
map = this._styleMap[cursorLocation.lineIndex],
lineIndex = map.line,
charIndex = map.offset + cursorLocation.charIndex;

if (isBeginningOfLine) {
var textOnPreviousLine = this._getTextOnPreviousLine(cursorLocation.lineIndex),
newCharIndexOnPrevLine = textOnPreviousLine ? textOnPreviousLine.length : 0;

if (!this.styles[lineIndex - 1]) {
this.styles[lineIndex - 1] = {};
}

for (charIndex in this.styles[lineIndex]) {
this.styles[lineIndex - 1][parseInt(charIndex, 10) + newCharIndexOnPrevLine]
= this.styles[lineIndex][charIndex];
}

this.shiftLineStyles(cursorLocation.lineIndex, -1);

}
else {
var currentLineStyles = this.styles[lineIndex];

if (currentLineStyles) {
delete currentLineStyles[charIndex];
//console.log('deleting', lineIndex, charIndex + offset);
}

var currentLineStylesCloned = clone(currentLineStyles);

// shift all styles by 1 backwards
for (var i in currentLineStylesCloned) {
var numericIndex = parseInt(i, 10);
if (numericIndex >= charIndex && numericIndex !== 0) {
currentLineStyles[numericIndex - 1] = currentLineStylesCloned[numericIndex];
delete currentLineStyles[numericIndex];
}
}
}
this._removeStyleObject(isBeginningOfLine, cursorLocation, lineIndex, charIndex);
}
});
})();

0 comments on commit b4a2bf6

Please sign in to comment.