Commit 7c49e82b authored by Igor Poboiko's avatar Igor Poboiko Committed by Laurent Montel
Browse files

Grab character format after the link if cursor points at its end

The default QTextEdit behavior is following:
 - the current char format corresponds to the symbol right _before_ the cursor position.
 - if the cursor is at the start of the block, the format is grabbed from the symbol _after_ the cursor.

It works well with standard formatting (bold, underline, etc), which can easily be enabled / disabled using corresponding QActions.
Note that the behavior is also the same for, say, LibreOffice.

However, it does not work well with hyperlinks. If the document ends with a link, it is virtually impossible
to start typing normal text right after this link (i.e. to reset the char format). The only way to do it
is to use copy-paste (i.e. paste some normal text, preserving formatting), which is quite frustrating.

For the same reason it's not easy to write some text right after hyperlink.
I believe it's the reason behind this tiny whitespace in `insertHtml` inside richTextComposer.

The workaround is following: alter the default behiour and take the character format from symbol right _after_
the link if the cursor points at the end of the link (this is also the way LibreOffice behaves).
parent 7e64f50f
Pipeline #28940 passed with stage
in 13 minutes and 40 seconds
......@@ -596,7 +596,7 @@ void RichTextComposer::insertFromMimeData(const QMimeData *source)
|| sourceText.startsWith(QLatin1String("fish://"))
|| sourceText.startsWith(QLatin1String("tel:"))
) {
insertHtml(QStringLiteral("<a href=\"%1\">%1</a> ").arg(sourceText));
insertHtml(QStringLiteral("<a href=\"%1\">%1</a>").arg(sourceText));
return;
}
}
......
......@@ -545,14 +545,6 @@ void RichTextComposerControler::RichTextComposerControlerPrivate::updateLink(con
}
cursor.insertText(_linkText, format);
// Insert a space after the link if at the end of the block so that
// typing some text after the link does not carry link formatting
if (!linkUrl.isEmpty() && cursor.atBlockEnd()) {
cursor.setPosition(cursor.selectionEnd());
cursor.setCharFormat(originalFormat);
cursor.insertText(QStringLiteral(" "));
}
cursor.endEditBlock();
}
......
......@@ -69,6 +69,31 @@ public:
supportFeatures |= RichTextEditor::TextToSpeech;
supportFeatures |= RichTextEditor::AllowTab;
supportFeatures |= RichTextEditor::AllowWebShortcut;
// Workaround QTextEdit behavior: if the cursor points right after the link
// and start typing, the char format is kept. If user wants to write normal
// text right after the link, the only way is to move cursor at the next character
// (say for "<a>text</a>more text" the character has to be before letter "o"!)
// It's impossible if the whole document ends with a link.
// The same happens when text starts with a link: it's impossible to write normal text before it.
QObject::connect(q, &RichTextEditor::cursorPositionChanged, q, [this](){
QTextCursor c = q->textCursor();
if (c.charFormat().isAnchor() && !c.hasSelection()) {
QTextCharFormat fmt;
// If we are at block start or end (and at anchor), we just set the "default" format
if (!c.atBlockEnd() && !c.atBlockStart() && !c.hasSelection()) {
QTextCursor probe = c;
// Otherwise, if the next character is not a link, we just grab it's format
probe.movePosition(QTextCursor::NextCharacter);
if (!probe.charFormat().isAnchor()) {
fmt = probe.charFormat();
}
}
c.setCharFormat(fmt);
q->setTextCursor(c);
}
});
}
~RichTextEditorPrivate()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment