From 02bed79265b7d65a2e0dce27e1373634cea1205c Mon Sep 17 00:00:00 2001 From: James Graham Date: Mon, 22 Dec 2025 15:28:43 +0000 Subject: [PATCH] Fix link insertion --- src/libneochat/chatdocumenthandler.cpp | 18 +++++++++++++----- src/libneochat/chatdocumenthandler.h | 3 ++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/libneochat/chatdocumenthandler.cpp b/src/libneochat/chatdocumenthandler.cpp index b8fd24226..18fc2b76b 100644 --- a/src/libneochat/chatdocumenthandler.cpp +++ b/src/libneochat/chatdocumenthandler.cpp @@ -213,10 +213,16 @@ void ChatDocumentHandler::setTextItem(QQuickItem *textItem) return; } cursor.setPosition(position); - cursor.select(QTextCursor::WordUnderCursor); + cursor.movePosition(QTextCursor::NextWord, QTextCursor::KeepAnchor); if (!cursor.selectedText().isEmpty()) { - cursor.mergeCharFormat(m_pendingFormat); - m_pendingFormat = {}; + if (m_pendingFormat) { + cursor.mergeCharFormat(*m_pendingFormat); + m_pendingFormat = std::nullopt; + } + if (m_pendingOverrideFormat) { + cursor.setCharFormat(*m_pendingOverrideFormat); + m_pendingOverrideFormat = std::nullopt; + } } }); initializeChars(); @@ -866,7 +872,8 @@ void ChatDocumentHandler::updateLink(const QString &linkUrl, const QString &link 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 // format for the block if (!linkUrl.isEmpty()) { @@ -902,8 +909,9 @@ void ChatDocumentHandler::updateLink(const QString &linkUrl, const QString &link _linkText = linkUrl; } cursor.insertText(_linkText, format); - cursor.endEditBlock(); + + m_pendingOverrideFormat = originalFormat; } QColor ChatDocumentHandler::linkColor() diff --git a/src/libneochat/chatdocumenthandler.h b/src/libneochat/chatdocumenthandler.h index 26327f567..6c45f0ec9 100644 --- a/src/libneochat/chatdocumenthandler.h +++ b/src/libneochat/chatdocumenthandler.h @@ -243,7 +243,8 @@ private: void setListFormat(RichFormat::Format format); QPointer m_markdownHelper; - QTextCharFormat m_pendingFormat = {}; + std::optional m_pendingFormat = std::nullopt; + std::optional m_pendingOverrideFormat = std::nullopt; SyntaxHighlighter *m_highlighter = nullptr;