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

Start to cleant setSelections #3119

Merged
merged 17 commits into from
Jul 31, 2016
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
269 changes: 162 additions & 107 deletions dist/fabric.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/fabric.min.js

Large diffs are not rendered by default.

Binary file modified dist/fabric.min.js.gz
Binary file not shown.
229 changes: 135 additions & 94 deletions dist/fabric.require.js

Large diffs are not rendered by default.

52 changes: 33 additions & 19 deletions src/mixins/itext_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
* Aborts cursor animation and clears all timeouts
*/
abortCursorAnimation: function() {
var shouldClear = this._currentTickState || this._currentTickCompleteState;
this._currentTickState && this._currentTickState.abort();
this._currentTickCompleteState && this._currentTickCompleteState.abort();

Expand All @@ -166,15 +167,20 @@
this._currentCursorOpacity = 0;
// to clear just itext area we need to transform the context
// it may not be worth it
this.canvas && this.canvas.clearContext(this.canvas.contextTop || this.ctx);
if (shouldClear) {
this.canvas && this.canvas.clearContext(this.canvas.contextTop || this.ctx);
}

},

/**
* Selects entire text
*/
selectAll: function() {
this.setSelectionStart(0);
this.setSelectionEnd(this.text.length);
this.selectionStart = 0;
this.selectionEnd = this.text.length;
this._fireSelectionChanged();
this._updateTextarea();
},

/**
Expand Down Expand Up @@ -305,24 +311,30 @@
* @param {Number} selectionStart Index of a character
*/
selectWord: function(selectionStart) {
selectionStart = selectionStart || this.selectionStart;
var newSelectionStart = this.searchWordBoundary(selectionStart, -1), /* search backwards */
newSelectionEnd = this.searchWordBoundary(selectionStart, 1);
/* search forward */
newSelectionEnd = this.searchWordBoundary(selectionStart, 1); /* search forward */

this.setSelectionStart(newSelectionStart);
this.setSelectionEnd(newSelectionEnd);
this.selectionStart = newSelectionStart;
this.selectionEnd = newSelectionEnd;
this._fireSelectionChanged();
this._updateTextarea();
this.renderCursorOrSelection();
},

/**
* Selects a line based on the index
* @param {Number} selectionStart Index of a character
*/
selectLine: function(selectionStart) {
selectionStart = selectionStart || this.selectionStart;
var newSelectionStart = this.findLineBoundaryLeft(selectionStart),
newSelectionEnd = this.findLineBoundaryRight(selectionStart);
newSelectionEnd = this.findLineBoundaryRight(selectionStart);

this.setSelectionStart(newSelectionStart);
this.setSelectionEnd(newSelectionEnd);
this.selectionStart = newSelectionStart;
this.selectionEnd = newSelectionEnd;
this._fireSelectionChanged();
this._updateTextarea();
},

/**
Expand Down Expand Up @@ -354,9 +366,8 @@
if (!this.canvas) {
return this;
}

this.canvas.renderAll();
this.canvas.fire('text:editing:entered', { target: this });
this.canvas.renderAll();
this.initMouseMoveHandler();
return this;
},
Expand Down Expand Up @@ -392,13 +403,15 @@
return;
}
if (newSelectionStart > this.__selectionStartOnMouseDown) {
this.setSelectionStart(this.__selectionStartOnMouseDown);
this.setSelectionEnd(newSelectionStart);
this.selectionStart = this.__selectionStartOnMouseDown;
this.selectionEnd = newSelectionStart;
}
else {
this.setSelectionStart(newSelectionStart);
this.setSelectionEnd(this.__selectionStartOnMouseDown);
this.selectionStart = newSelectionStart;
this.selectionEnd = this.__selectionStartOnMouseDown;
}
this._fireSelectionChanged();
this._updateTextarea();
this.renderCursorOrSelection();
},

Expand Down Expand Up @@ -565,7 +578,8 @@
this._removeSingleCharAndStyle(start + 1);
end--;
}
this.setSelectionStart(start);
this.selectionStart = start;
this.selectionEnd = start;
},

_removeSingleCharAndStyle: function(index) {
Expand All @@ -588,7 +602,6 @@

if (this.selectionEnd - this.selectionStart > 1) {
this._removeCharsFromTo(this.selectionStart, this.selectionEnd);
this.setSelectionEnd(this.selectionStart);
}
//short circuit for block paste
if (!useCopiedStyle && this.isEmptyStyles()) {
Expand Down Expand Up @@ -621,10 +634,11 @@
return;
}
this._updateTextarea();
this.canvas && this.canvas.renderAll();
this.setCoords();
this._fireSelectionChanged();
this.fire('changed');
this.canvas && this.canvas.fire('text:changed', { target: this });
this.canvas && this.canvas.renderAll();
},

/**
Expand Down
24 changes: 17 additions & 7 deletions src/mixins/itext_click_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot

if (this.isEditing) {
this.__selectionStartOnMouseDown = this.selectionStart;
this.initDelayedCursor(true);
if (this.selectionStart === this.selectionEnd) {
this.abortCursorAnimation();
}
this.renderCursorOrSelection();
}
});
},
Expand All @@ -130,7 +133,12 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot

if (this.__lastSelected && !this.__corner) {
this.enterEditing(options.e);
this.initDelayedCursor(true);
if (this.selectionStart === this.selectionEnd) {
this.initDelayedCursor(true);
}
else {
this.renderCursorOrSelection();
}
}
this.selected = true;
});
Expand All @@ -145,17 +153,19 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot

if (e.shiftKey) {
if (newSelectionStart < this.selectionStart) {
this.setSelectionEnd(this.selectionStart);
this.setSelectionStart(newSelectionStart);
this.selectionEnd = this.selectionStart;
this.selectionStart = newSelectionStart;
}
else {
this.setSelectionEnd(newSelectionStart);
this.selectionEnd = newSelectionStart;
}
}
else {
this.setSelectionStart(newSelectionStart);
this.setSelectionEnd(newSelectionStart);
this.selectionStart = newSelectionStart;
this.selectionEnd = newSelectionStart;
}
this._fireSelectionChanged();
this._updateTextarea();
},

/**
Expand Down
Loading