Skip to content

Commit 227e626

Browse files
author
Dmitriy Kubyshkin
committed
Fixed issues with wrong handling of trailing \n in textarea.
1 parent 1171c05 commit 227e626

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

lib/CanvasTextEditor.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ var CanvasTextEditor = function(doc) {
2424

2525
module.exports = CanvasTextEditor;
2626

27+
/**
28+
* Determines if current browser is Opera
29+
* @type {Boolean}
30+
*/
31+
CanvasTextEditor.prototype.isOpera = ('opera' in window) && ('version' in window.opera);
32+
2733
/**
2834
* CSS class that is assigned to the wrapper.
2935
* @type {String}
@@ -81,18 +87,15 @@ CanvasTextEditor.prototype.selectionChange = function() {
8187
// if it's not we put together selected text from document
8288
if (!this._selection.isEmpty()) {
8389
var ranges = this._selection.lineRanges(),
84-
line;
90+
line = '';
8591
for(var key in ranges) {
8692
selectedText += this._document.getLine(parseInt(key)).slice(
8793
ranges[key][0], ranges[key][1] === true ? undefined : ranges[key][1]
8894
);
8995
}
9096
}
9197

92-
// Put selected text into our proxy
93-
this.inputEl.value = selectedText;
94-
this.inputEl.selectionStart = 0;
95-
this.inputEl.selectionEnd = selectedText.length;
98+
this.setInputText(selectedText, true);
9699

97100
// Updating canvas to show selection
98101
this.render();
@@ -197,17 +200,44 @@ CanvasTextEditor.prototype._createInput = function() {
197200
this.inputEl.addEventListener('blur', this.blur.bind(this), false);
198201
this.inputEl.addEventListener('focus', this._inputFocus.bind(this), false);
199202
this.inputEl.addEventListener('keydown', this.keydown.bind(this), false);
203+
this.inputEl.addEventListener('keypress', this.setInputText.bind(this, ''), false);
200204
this.inputEl.tabIndex = -1; // we don't want input to get focus by tabbing
201205
this.wrapper.appendChild(this.inputEl);
206+
this.setInputText('', true);
202207
};
203208

204209
/**
205210
* Handles regular text input into our proxy field
206211
* @param {Event} e
207212
*/
208213
CanvasTextEditor.prototype.handleInput = function(e) {
209-
this.insertTextAtCurrentPosition(e.target.value);
210-
e.target.value = '';
214+
var value = e.target.value;
215+
if (this.isOpera) {
216+
// Opera doesn't need a placeholder
217+
value = value.substring(0, value.length);
218+
} else {
219+
// Compensate for placeholder
220+
value = value.substring(0, value.length - 1);
221+
}
222+
this.insertTextAtCurrentPosition(value);
223+
this.needsClearing = true;
224+
};
225+
226+
/**
227+
* Makes input contain only placeholder character and places cursor at start
228+
*/
229+
CanvasTextEditor.prototype.setInputText = function(text, force) {
230+
if(this.needsClearing || force === true) {
231+
if (this.isOpera) {
232+
this.inputEl.value = text;
233+
this.inputEl.select();
234+
} else {
235+
this.inputEl.value = text + '#';
236+
this.inputEl.selectionStart = 0;
237+
this.inputEl.selectionEnd = text.length;
238+
}
239+
}
240+
this.needsClearing = false;
211241
};
212242

213243
/**

0 commit comments

Comments
 (0)