Commit bda30001 authored by Yang Wang's avatar Yang Wang Committed by Andreas Traczyk

chatview: fix pasting text not replacing the selection

Change-Id: I0937289a45c4e379e7d30dd7330a73951472981b
Gitlab: #491
parent eb4700b9
......@@ -188,7 +188,7 @@ bool MessageWebView::eventFilter(QObject *watched, QEvent *event)
void MessageWebView::setMessagesContent(const QString& text)
{
page()->runJavaScript(QStringLiteral("document.getElementById('message').value += '%1';").arg(text));
page()->runJavaScript(QStringLiteral("replaceText('%1');").arg(text));
}
void
......
......@@ -1761,3 +1761,28 @@ function pasteKeyDetected(e) {
e.preventDefault();
window.jsbridge.emitPasteKeyDetected();
}
// Set the curser to a target position
function setCaretPosition(elem, caretPos) {
var range;
if (elem.createTextRange) {
range = elem.createTextRange();
range.move('character', caretPos);
range.select();
} else {
elem.focus();
if (elem.selectionStart !== undefined) {
elem.setSelectionRange(caretPos, caretPos);
}
}
}
function replaceText(text) {
var input = messageBarInput;
var currentContent = input.value;
var start = input.selectionStart;
var end = input.selectionEnd;
var output = [currentContent.slice(0, start), text, currentContent.slice(end)].join('');
input.value = output;
setCaretPosition(input, start + text.length);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment