Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix itext onInput with selected text #2502

Merged
merged 6 commits into from
Oct 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
});
})();