mirror of
https://github.com/Hopiu/fabric.js.git
synced 2026-05-05 20:34:45 +00:00
Fixing text insertion and deletion on edge cases. (#4420)
* fix insertion * fixed forward delete on line start
This commit is contained in:
parent
073a892993
commit
a090a980d0
2 changed files with 20 additions and 10 deletions
|
|
@ -839,10 +839,14 @@
|
|||
}
|
||||
linesLenght && this.insertNewlineStyleObject(
|
||||
cursorLoc.lineIndex, cursorLoc.charIndex + addedLines[0], linesLenght);
|
||||
for (var i = 1; i <= linesLenght; i++) {
|
||||
for (var i = 1; i < linesLenght; i++) {
|
||||
this.insertCharStyleObject(cursorLoc.lineIndex + i, 0, addedLines[i], copiedStyle);
|
||||
copiedStyle = copiedStyle && copiedStyle.slice(addedLines[i] + 1);
|
||||
}
|
||||
// we use i outside the loop to get it like linesLength
|
||||
if (addedLines[i] > 0) {
|
||||
this.insertCharStyleObject(cursorLoc.lineIndex + i, 0, addedLines[i], copiedStyle);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -145,7 +145,6 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
|
|||
nextCharCount = nextText.length,
|
||||
removedText, insertedText,
|
||||
charDiff = nextCharCount - charCount;
|
||||
|
||||
if (this.hiddenTextarea.value === '') {
|
||||
this.styles = { };
|
||||
this.updateFromTextArea();
|
||||
|
|
@ -157,24 +156,31 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
|
|||
return;
|
||||
}
|
||||
|
||||
if (this.selectionStart !== this.selectionEnd) {
|
||||
removedText = this._text.slice(this.selectionStart, this.selectionEnd);
|
||||
charDiff += this.selectionEnd - this.selectionStart;
|
||||
}
|
||||
else if (nextCharCount < charCount) {
|
||||
removedText = this._text.slice(this.selectionEnd + charDiff, this.selectionEnd);
|
||||
}
|
||||
var textareaSelection = this.fromStringToGraphemeSelection(
|
||||
this.hiddenTextarea.selectionStart,
|
||||
this.hiddenTextarea.selectionEnd,
|
||||
this.hiddenTextarea.value
|
||||
);
|
||||
var backDelete = this.selectionStart > textareaSelection.selectionStart;
|
||||
|
||||
if (this.selectionStart !== this.selectionEnd) {
|
||||
removedText = this._text.slice(this.selectionStart, this.selectionEnd);
|
||||
charDiff += this.selectionEnd - this.selectionStart;
|
||||
}
|
||||
else if (nextCharCount < charCount) {
|
||||
if (backDelete) {
|
||||
removedText = this._text.slice(this.selectionEnd + charDiff, this.selectionEnd);
|
||||
}
|
||||
else {
|
||||
removedText = this._text.slice(this.selectionStart, this.selectionStart - charDiff);
|
||||
}
|
||||
}
|
||||
insertedText = nextText.slice(textareaSelection.selectionEnd - charDiff, textareaSelection.selectionEnd);
|
||||
if (removedText && removedText.length) {
|
||||
if (this.selectionStart !== this.selectionEnd) {
|
||||
this.removeStyleFromTo(this.selectionStart, this.selectionEnd);
|
||||
}
|
||||
else if (this.selectionStart > textareaSelection.selectionStart) {
|
||||
else if (backDelete) {
|
||||
// detect differencies between forwardDelete and backDelete
|
||||
this.removeStyleFromTo(this.selectionEnd - removedText.length, this.selectionEnd);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue