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

On input for Itext #2352

Merged
merged 3 commits into from
Jul 22, 2015
Merged
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
35 changes: 20 additions & 15 deletions src/mixins/itext_key_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
fabric.document.body.appendChild(this.hiddenTextarea);

fabric.util.addListener(this.hiddenTextarea, 'keydown', this.onKeyDown.bind(this));
fabric.util.addListener(this.hiddenTextarea, 'keypress', this.onKeyPress.bind(this));
fabric.util.addListener(this.hiddenTextarea, 'input', this.onInput.bind(this));
fabric.util.addListener(this.hiddenTextarea, 'copy', this.copy.bind(this));
fabric.util.addListener(this.hiddenTextarea, 'paste', this.paste.bind(this));

Expand Down Expand Up @@ -76,6 +76,24 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
this.canvas && this.canvas.renderAll();
},

/**
* Handles onInput event
* @param {Event} e Event object
*/
onInput: function(e) {
if (!this.isEditing || this._cancelOnInput) {
this._cancelOnInput = false;
return;
}
var offset = this.selectionStart || 0,
textLength = this.text.length,
newTextLength = this.hiddenTextarea.value.length,
diff = newTextLength - textLength,
charsToInsert = this.hiddenTextarea.value.slice(offset, offset + diff);
this.insertChars(charsToInsert);
e.stopPropagation();
},

/**
* Forward delete
*/
Expand Down Expand Up @@ -131,6 +149,7 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
if (copiedText) {
this.insertChars(copiedText, useCopiedStyle);
}
this._cancelOnInput = true;
},

/**
Expand All @@ -155,20 +174,6 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
return e && (e.clipboardData || fabric.window.clipboardData);
},

/**
* Handles keypress event
* @param {Event} e Event object
*/
onKeyPress: function(e) {
if (!this.isEditing || e.metaKey || e.ctrlKey) {
return;
}
if (e.which !== 0) {
this.insertChars(String.fromCharCode(e.which));
}
e.stopPropagation();
},

/**
* Gets start offset of a selection
* @param {Event} e Event object
Expand Down