From cc2559f9cf30677ddcab2c6898fb1505eb2965bd Mon Sep 17 00:00:00 2001 From: RadRussianRus Date: Thu, 15 Sep 2022 23:19:02 +0300 Subject: [PATCH] [Option][JSON] Custom auto-replaces --- ui/widgets/input_fields.cpp | 32 ++++++++++++++++++++++++++++++++ ui/widgets/input_fields.h | 4 ++++ 2 files changed, 36 insertions(+) diff --git a/ui/widgets/input_fields.cpp b/ui/widgets/input_fields.cpp index f838103..7ee6ae6 100644 --- a/ui/widgets/input_fields.cpp +++ b/ui/widgets/input_fields.cpp @@ -28,6 +28,7 @@ #include #include #include +#include namespace Ui { namespace { @@ -59,6 +60,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; @@ -942,6 +945,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("**"); @@ -1076,6 +1083,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; @@ -1090,6 +1100,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; @@ -1370,6 +1394,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 8d7de73..833a4a7 100644 --- a/ui/widgets/input_fields.h +++ b/ui/widgets/input_fields.h @@ -30,6 +30,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"); @@ -56,6 +58,7 @@ struct InstantReplaces { static const InstantReplaces &Default(); static const InstantReplaces &TextOnly(); + static const InstantReplaces &Custom(); int maxLength = 0; Node reverseMap; @@ -218,6 +221,7 @@ public: void setAdditionalMargins(QMargins margins); 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);