Improve cross-browsers compatiblity for text editor
This commit is contained in:
4
assets/js/app.min.js
vendored
4
assets/js/app.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -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;
|
||||||
|
|||||||
@@ -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');
|
||||||
|
|||||||
@@ -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
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user