Improve cross-browsers compatiblity for text editor

This commit is contained in:
Frederic Guillot
2016-12-01 20:54:33 -05:00
parent 9c06bc4ed2
commit be83821ef7
4 changed files with 35 additions and 18 deletions

View File

@@ -125,25 +125,18 @@ KB.component('text-editor', function (containerElement, options) {
insertText(lines.join('\n'));
}
setCursorBeforeClosingTag(tag, 1);
}
function insertText(replacedText) {
var result = false;
textarea.focus();
// Fix issue with IE11 (last line with wrong position)
if (textarea.value.length < textarea.selectionStart) {
selectionStart = textarea.value.length;
} else {
selectionStart = textarea.selectionStart;
}
var result = false;
var selectionPosition = KB.utils.getSelectionPosition(textarea);
if (textarea.selectionStart === textarea.selectionEnd) {
selectionEnd = selectionStart;
} else {
selectionEnd = textarea.selectionEnd;
}
selectionStart = selectionPosition.selectionStart;
selectionEnd = selectionPosition.selectionEnd;
if (document.queryCommandSupported('insertText')) {
result = document.execCommand('insertText', false, replacedText);
@@ -154,7 +147,7 @@ KB.component('text-editor', function (containerElement, options) {
document.execCommand('ms-beginUndoUnit');
} catch (error) {}
textarea.value = replaceTextRange(textarea.value, selectionStart, textarea.selectionEnd, replacedText);
textarea.value = replaceTextRange(textarea.value, selectionStart, selectionEnd, replacedText);
try {
document.execCommand('ms-endUndoUnit');