Add a workaround for invalid Qt signal data.

This commit is contained in:
John Preston 2020-05-12 19:11:46 +04:00
parent 4d2f17d9fc
commit 6aa6a143bf
2 changed files with 26 additions and 7 deletions

View file

@ -2084,7 +2084,7 @@ void InputField::processFormatting(int insertPosition, int insertEnd) {
break; break;
} }
if (breakTagOnNotLetter && !ch->isLetter()) { if (breakTagOnNotLetter && !ch->isLetterOrNumber()) {
// Remove tag name till the end if no current action is prepared. // Remove tag name till the end if no current action is prepared.
if (action.type != ActionType::Invalid) { if (action.type != ActionType::Invalid) {
break; break;
@ -2185,6 +2185,22 @@ void InputField::onDocumentContentsChange(
return; return;
} }
// In case of input method events Qt emits
// document content change signals for a whole
// text block where the even took place.
// This breaks our wysiwyg markup, so we adjust
// the parameters to match the real change.
if (_inputMethodCommit.has_value()
&& charsAdded > _inputMethodCommit->size()
&& charsRemoved > 0) {
const auto inBlockBefore = charsAdded - _inputMethodCommit->size();
if (charsRemoved >= inBlockBefore) {
charsAdded -= inBlockBefore;
charsRemoved -= inBlockBefore;
position += inBlockBefore;
}
}
const auto document = _inner->document(); const auto document = _inner->document();
// Qt bug workaround https://bugreports.qt.io/browse/QTBUG-49062 // Qt bug workaround https://bugreports.qt.io/browse/QTBUG-49062
@ -2882,8 +2898,9 @@ void InputField::inputMethodEventInner(QInputMethodEvent *e) {
_lastPreEditText = preedit; _lastPreEditText = preedit;
startPlaceholderAnimation(); startPlaceholderAnimation();
} }
const auto text = e->commitString(); _inputMethodCommit = e->commitString();
_inner->QTextEdit::inputMethodEvent(e); _inner->QTextEdit::inputMethodEvent(e);
const auto text = *base::take(_inputMethodCommit);
if (!processMarkdownReplaces(text)) { if (!processMarkdownReplaces(text)) {
processInstantReplaces(text); processInstantReplaces(text);
} }

View file

@ -462,19 +462,21 @@ private:
int _maxLength = -1; int _maxLength = -1;
int _minHeight = -1; int _minHeight = -1;
int _maxHeight = -1; int _maxHeight = -1;
bool _forcePlaceholderHidden = false;
bool _reverseMarkdownReplacement = false;
const std::unique_ptr<Inner> _inner; const std::unique_ptr<Inner> _inner;
TextWithTags _lastTextWithTags;
std::vector<MarkdownTag> _lastMarkdownTags;
QString _lastPreEditText;
Fn<bool( Fn<bool(
EditLinkSelection selection, EditLinkSelection selection,
QString text, QString text,
QString link, QString link,
EditLinkAction action)> _editLinkCallback; EditLinkAction action)> _editLinkCallback;
TextWithTags _lastTextWithTags;
std::vector<MarkdownTag> _lastMarkdownTags;
QString _lastPreEditText;
std::optional<QString> _inputMethodCommit;
bool _forcePlaceholderHidden = false;
bool _reverseMarkdownReplacement = false;
// Tags list which we should apply while setText() call or insert from mime data. // Tags list which we should apply while setText() call or insert from mime data.
TagList _insertedTags; TagList _insertedTags;