diff --git a/ui/widgets/input_fields.cpp b/ui/widgets/input_fields.cpp index f2dafbe..c265910 100644 --- a/ui/widgets/input_fields.cpp +++ b/ui/widgets/input_fields.cpp @@ -27,6 +27,7 @@ #include #include #include +#include namespace Ui { namespace { @@ -58,6 +59,8 @@ const auto kNewlineChars = QString("\r\n") + QChar(QChar::ParagraphSeparator) + QChar(QChar::LineSeparator); +QMap customReplacesMap; + // We need unique tags otherwise same custom emoji would join in a single // QTextCharFormat with the same properties, including kCustomEmojiText. auto GlobalCustomEmojiCounter = 0; @@ -932,6 +935,10 @@ struct FormattingAction { } // namespace +void AddCustomReplacement(QString from, QString to) { + customReplacesMap.insert(from, to); +} + // kTagUnderline is not used for Markdown. const QString InputField::kTagBold = QStringLiteral("**"); @@ -1052,6 +1059,9 @@ const InstantReplaces &InstantReplaces::Default() { Assert(emoji != nullptr); result.add(what, emoji->text()); } + for (auto i = customReplacesMap.constBegin(), e = customReplacesMap.constEnd(); i != e; ++i) { + result.add(i.key(), i.value()); + } return result; }(); return result; @@ -1066,6 +1076,20 @@ const InstantReplaces &InstantReplaces::TextOnly() { result.add( ":shrug:", QChar(175) + QString("\\_(") + QChar(12484) + ")_/" + QChar(175)); + for (auto i = customReplacesMap.constBegin(), e = customReplacesMap.constEnd(); i != e; ++i) { + result.add(i.key(), i.value()); + } + return result; + }(); + return result; +} + +const InstantReplaces &InstantReplaces::Custom() { + static const auto result = [] { + auto result = InstantReplaces(); + for (auto i = customReplacesMap.constBegin(), e = customReplacesMap.constEnd(); i != e; ++i) { + result.add(i.key(), i.value()); + } return result; }(); return result; @@ -1652,6 +1676,14 @@ void InputField::setInstantReplaces(const InstantReplaces &replaces) { _mutableInstantReplaces = replaces; } +void InputField::setInstantReplaces(rpl::producer producer) { + std::move( + producer + ) | rpl::start_with_next([=](InstantReplaces replaces) { + _mutableInstantReplaces = replaces; + }, lifetime()); +} + void InputField::setInstantReplacesEnabled(rpl::producer enabled) { std::move( enabled diff --git a/ui/widgets/input_fields.h b/ui/widgets/input_fields.h index 1da7845..286b8a1 100644 --- a/ui/widgets/input_fields.h +++ b/ui/widgets/input_fields.h @@ -29,6 +29,8 @@ class CustomEmoji; namespace Ui { +void AddCustomReplacement(QString from, QString to); + const auto kClearFormatSequence = QKeySequence("ctrl+shift+n"); const auto kStrikeOutSequence = QKeySequence("ctrl+shift+x"); const auto kMonospaceSequence = QKeySequence("ctrl+shift+m"); @@ -57,6 +59,7 @@ struct InstantReplaces { static const InstantReplaces &Default(); static const InstantReplaces &TextOnly(); + static const InstantReplaces &Custom(); int maxLength = 0; Node reverseMap; @@ -299,6 +302,7 @@ public: void setAdditionalMargin(int margin); void setInstantReplaces(const InstantReplaces &replaces); + void setInstantReplaces(rpl::producer producer); void setInstantReplacesEnabled(rpl::producer enabled); void setMarkdownReplacesEnabled(rpl::producer enabled); void setExtendedContextMenu(rpl::producer value);