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
No known key found for this signature in database
GPG Key ID: 92D77191BA7FBC99
4 changed files with 35 additions and 18 deletions

File diff suppressed because one or more lines are too long

View File

@ -35,14 +35,17 @@ KB.component('suggest-menu', function(containerElement, options) {
}
function insertSelectedItem() {
containerElement.focus();
var element = KB.find('.suggest-menu-item.active');
var value = element.data('value');
var trigger = element.data('trigger');
var content = containerElement.value;
var text = getLastWord(containerElement);
var substitute = trigger + value + ' ';
var before = content.substring(0, containerElement.selectionStart - text.length);
var after = content.substring(containerElement.selectionEnd);
var selectionPosition = KB.utils.getSelectionPosition(containerElement);
var before = content.substring(0, selectionPosition.selectionStart - text.length);
var after = content.substring(selectionPosition.selectionEnd);
var position = before.length + substitute.length;
containerElement.value = before + substitute + after;

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');

View File

@ -11,3 +11,24 @@ KB.utils.formatDuration = function (d) {
return d + "s";
};
KB.utils.getSelectionPosition = function (element) {
var selectionStart, selectionEnd;
if (element.value.length < element.selectionStart) {
selectionStart = element.value.length;
} else {
selectionStart = element.selectionStart;
}
if (element.selectionStart === element.selectionEnd) {
selectionEnd = selectionStart;
} else {
selectionEnd = element.selectionEnd;
}
return {
selectionStart: selectionStart,
selectionEnd: selectionEnd
};
};