Fix link insertion

This commit is contained in:
James Graham
2025-12-22 15:28:43 +00:00
parent aadc441686
commit 02bed79265
2 changed files with 15 additions and 6 deletions

View File

@@ -213,10 +213,16 @@ void ChatDocumentHandler::setTextItem(QQuickItem *textItem)
return; return;
} }
cursor.setPosition(position); cursor.setPosition(position);
cursor.select(QTextCursor::WordUnderCursor); cursor.movePosition(QTextCursor::NextWord, QTextCursor::KeepAnchor);
if (!cursor.selectedText().isEmpty()) { if (!cursor.selectedText().isEmpty()) {
cursor.mergeCharFormat(m_pendingFormat); if (m_pendingFormat) {
m_pendingFormat = {}; cursor.mergeCharFormat(*m_pendingFormat);
m_pendingFormat = std::nullopt;
}
if (m_pendingOverrideFormat) {
cursor.setCharFormat(*m_pendingOverrideFormat);
m_pendingOverrideFormat = std::nullopt;
}
} }
}); });
initializeChars(); initializeChars();
@@ -866,7 +872,8 @@ void ChatDocumentHandler::updateLink(const QString &linkUrl, const QString &link
cursor.select(QTextCursor::WordUnderCursor); cursor.select(QTextCursor::WordUnderCursor);
} }
QTextCharFormat format = cursor.charFormat(); const auto originalFormat = cursor.charFormat();
auto format = cursor.charFormat();
// Save original format to create an extra space with the existing char // Save original format to create an extra space with the existing char
// format for the block // format for the block
if (!linkUrl.isEmpty()) { if (!linkUrl.isEmpty()) {
@@ -902,8 +909,9 @@ void ChatDocumentHandler::updateLink(const QString &linkUrl, const QString &link
_linkText = linkUrl; _linkText = linkUrl;
} }
cursor.insertText(_linkText, format); cursor.insertText(_linkText, format);
cursor.endEditBlock(); cursor.endEditBlock();
m_pendingOverrideFormat = originalFormat;
} }
QColor ChatDocumentHandler::linkColor() QColor ChatDocumentHandler::linkColor()

View File

@@ -243,7 +243,8 @@ private:
void setListFormat(RichFormat::Format format); void setListFormat(RichFormat::Format format);
QPointer<ChatMarkdownHelper> m_markdownHelper; QPointer<ChatMarkdownHelper> m_markdownHelper;
QTextCharFormat m_pendingFormat = {}; std::optional<QTextCharFormat> m_pendingFormat = std::nullopt;
std::optional<QTextCharFormat> m_pendingOverrideFormat = std::nullopt;
SyntaxHighlighter *m_highlighter = nullptr; SyntaxHighlighter *m_highlighter = nullptr;