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

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() { function insertSelectedItem() {
containerElement.focus();
var element = KB.find('.suggest-menu-item.active'); var element = KB.find('.suggest-menu-item.active');
var value = element.data('value'); var value = element.data('value');
var trigger = element.data('trigger'); var trigger = element.data('trigger');
var content = containerElement.value; var content = containerElement.value;
var text = getLastWord(containerElement); var text = getLastWord(containerElement);
var substitute = trigger + value + ' '; var substitute = trigger + value + ' ';
var before = content.substring(0, containerElement.selectionStart - text.length); var selectionPosition = KB.utils.getSelectionPosition(containerElement);
var after = content.substring(containerElement.selectionEnd); var before = content.substring(0, selectionPosition.selectionStart - text.length);
var after = content.substring(selectionPosition.selectionEnd);
var position = before.length + substitute.length; var position = before.length + substitute.length;
containerElement.value = before + substitute + after; containerElement.value = before + substitute + after;

View File

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

View File

@@ -11,3 +11,24 @@ KB.utils.formatDuration = function (d) {
return d + "s"; 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
};
};