Markdown: Enable action buttons with unselected text

Add appropriate indicators and adjust cursor position.

Change-Id: Ia4e3027899e0cedd83e94327bbeb0a2c4e518fdc
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Tasuku Suzuki <tasuku.suzuki@signal-slot.co.jp>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Tasuku Suzuki
2023-09-15 01:22:24 +09:00
committed by Eike Ziller
parent 91975f98af
commit e4ec7f0d14

View File

@@ -110,8 +110,13 @@ public:
button->setFont([button]{ auto f = button->font(); f.setItalic(true); return f; }()); button->setFont([button]{ auto f = button->font(); f.setItalic(true); return f; }());
button->setToolTip(Tr::tr("Emphasis")); button->setToolTip(Tr::tr("Emphasis"));
connect(button, &QToolButton::clicked, this, [this] { connect(button, &QToolButton::clicked, this, [this] {
triggerFormatingAction([](QString *selectedText) { triggerFormatingAction([](QString *selectedText, int *cursorOffset) {
*selectedText = QStringLiteral("*%1*").arg(*selectedText); if (selectedText->isEmpty()) {
*selectedText = QStringLiteral("**");
*cursorOffset = -1;
} else {
*selectedText = QStringLiteral("*%1*").arg(*selectedText);
}
}); });
}); });
m_markDownButtons.append(button); m_markDownButtons.append(button);
@@ -120,8 +125,13 @@ public:
button->setFont([button]{ auto f = button->font(); f.setBold(true); return f; }()); button->setFont([button]{ auto f = button->font(); f.setBold(true); return f; }());
button->setToolTip(Tr::tr("Strong")); button->setToolTip(Tr::tr("Strong"));
connect(button, &QToolButton::clicked, this, [this] { connect(button, &QToolButton::clicked, this, [this] {
triggerFormatingAction([](QString *selectedText) { triggerFormatingAction([](QString *selectedText, int *cursorOffset) {
*selectedText = QStringLiteral("**%1**").arg(*selectedText); if (selectedText->isEmpty()) {
*selectedText = QStringLiteral("****");
*cursorOffset = -2;
} else {
*selectedText = QStringLiteral("**%1**").arg(*selectedText);
}
}); });
}); });
m_markDownButtons.append(button); m_markDownButtons.append(button);
@@ -129,8 +139,13 @@ public:
button->setText("`"); button->setText("`");
button->setToolTip(Tr::tr("Inline Code")); button->setToolTip(Tr::tr("Inline Code"));
connect(button, &QToolButton::clicked, this, [this] { connect(button, &QToolButton::clicked, this, [this] {
triggerFormatingAction([](QString *selectedText) { triggerFormatingAction([](QString *selectedText, int *cursorOffset) {
*selectedText = QStringLiteral("`%1`").arg(*selectedText); if (selectedText->isEmpty()) {
*selectedText = QStringLiteral("``");
*cursorOffset = -1;
} else {
*selectedText = QStringLiteral("`%1`").arg(*selectedText);
}
}); });
}); });
m_markDownButtons.append(button); m_markDownButtons.append(button);
@@ -139,23 +154,22 @@ public:
button->setToolTip(Tr::tr("Hyper Link")); button->setToolTip(Tr::tr("Hyper Link"));
connect(button, &QToolButton::clicked, this, [this] { connect(button, &QToolButton::clicked, this, [this] {
triggerFormatingAction([](QString *selectedText, int *cursorOffset, int *selectionLength) { triggerFormatingAction([](QString *selectedText, int *cursorOffset, int *selectionLength) {
*selectedText = QStringLiteral("[%1](https://)").arg(*selectedText); if (selectedText->isEmpty()) {
*cursorOffset = -1; *selectedText = QStringLiteral("[](https://)");
*selectionLength = -8; *cursorOffset = -11; // ](https://) is 11 chars
} else {
*selectedText = QStringLiteral("[%1](https://)").arg(*selectedText);
*cursorOffset = -1;
*selectionLength = -8; // https:// is 8 chars
}
}); });
}); });
m_markDownButtons.append(button); m_markDownButtons.append(button);
for (auto button : m_markDownButtons) { for (auto button : m_markDownButtons) {
button->setEnabled(!m_textEditorWidget->selectedText().isEmpty());
// do not call setVisible(true) at this point, this destroys the hover effect on macOS // do not call setVisible(true) at this point, this destroys the hover effect on macOS
if (!showEditor) if (!showEditor)
button->setVisible(false); button->setVisible(false);
} }
connect(m_textEditorWidget, &QPlainTextEdit::copyAvailable, [this](bool yes) {
for (auto button : m_markDownButtons) {
button->setEnabled(yes);
}
});
auto swapViews = new QToolButton; auto swapViews = new QToolButton;
swapViews->setText(Tr::tr("Swap Views")); swapViews->setText(Tr::tr("Swap Views"));
@@ -362,11 +376,12 @@ public:
} }
private: private:
void triggerFormatingAction(std::function<void(QString *selectedText)> action) void triggerFormatingAction(std::function<void(QString *selectedText, int *cursorOffset)> action)
{ {
auto formattedText = m_textEditorWidget->selectedText(); auto formattedText = m_textEditorWidget->selectedText();
action(&formattedText); int cursorOffset = 0;
format(formattedText); action(&formattedText, &cursorOffset);
format(formattedText, cursorOffset);
} }
void triggerFormatingAction(std::function<void(QString *selectedText, int *cursorOffset, int *selectionLength)> action) void triggerFormatingAction(std::function<void(QString *selectedText, int *cursorOffset, int *selectionLength)> action)
{ {