From b540beff071dd745e037f64162cf326f67af1c6a 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/fields/input_field.cpp | 32 +++++++++++++++++++++++++++++++ ui/widgets/fields/input_field.h | 4 ++++ 2 files changed, 36 insertions(+) diff --git a/ui/widgets/fields/input_field.cpp b/ui/widgets/fields/input_field.cpp index eb7e7cc..95b6d4e 100644 --- a/ui/widgets/fields/input_field.cpp +++ b/ui/widgets/fields/input_field.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #ifdef Q_OS_WIN #include @@ -73,6 +74,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; @@ -938,6 +941,10 @@ struct FormattingAction { } // namespace +void AddCustomReplacement(QString from, QString to) { + customReplacesMap.insert(from, to); +} + // kTagUnderline is not used for Markdown. const QString InputField::kTagBold = u"**"_q; @@ -1086,6 +1093,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; @@ -1100,6 +1110,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; @@ -1451,6 +1475,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/fields/input_field.h b/ui/widgets/fields/input_field.h index 734477a..957509c 100644 --- a/ui/widgets/fields/input_field.h +++ b/ui/widgets/fields/input_field.h @@ -33,6 +33,8 @@ struct InputField; namespace Ui { +void AddCustomReplacement(QString from, QString to); + const auto kClearFormatSequence = QKeySequence("ctrl+shift+n"); const auto kStrikeOutSequence = QKeySequence("ctrl+shift+x"); const auto kBlockquoteSequence = QKeySequence("ctrl+shift+."); @@ -60,6 +62,7 @@ struct InstantReplaces { static const InstantReplaces &Default(); static const InstantReplaces &TextOnly(); + static const InstantReplaces &Custom(); int maxLength = 0; Node reverseMap; @@ -225,6 +228,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);