Skip to content

Commit fc2515f

Browse files
committed
#3427 highlighting: improved encrypted text highlighting
Signed-off-by: Patrizio Bekerle <[email protected]>
1 parent 82cf355 commit fc2515f

File tree

5 files changed

+54
-12
lines changed

5 files changed

+54
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
- Fixed Evernote import code block handling to preserve leading indentation and code fences
1717
- Code blocks imported from Evernote now maintain their original formatting
1818
- Fixed an issue where PHP and other code blocks would lose their indentation or code fence markers
19+
- **Improved encrypted text highlighting** in the note editor (for [#3427](https://github.com/pbek/QOwnNotes/issues/3427))
20+
- Encrypted text blocks are now visually distinguished with smaller font size for better readability
21+
- Encrypted text blocks now skip markdown highlighting, spell checking, and link detection to improve performance
1922

2023
## 26.1.3
2124

src/entities/note.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3409,6 +3409,14 @@ QString Note::parseEncryptedNoteText(const QString &noteText) {
34093409
return match.hasMatch() ? match.captured(1) : QLatin1String("");
34103410
}
34113411

3412+
bool Note::isEncryptedTextBegin(const QString &text) {
3413+
return text == NOTE_TEXT_ENCRYPTION_PRE_STRING;
3414+
}
3415+
3416+
bool Note::isEncryptedTextEnd(const QString &text) {
3417+
return text == NOTE_TEXT_ENCRYPTION_POST_STRING;
3418+
}
3419+
34123420
/**
34133421
* Returns encrypted note text if it is encrypted
34143422
*/

src/entities/note.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,8 @@ class Note {
405405
[[nodiscard]] QString getDecryptedNoteText(const QString &encryptedNoteText) const;
406406

407407
static QString parseEncryptedNoteText(const QString &noteText);
408+
static bool isEncryptedTextBegin(const QString &text);
409+
static bool isEncryptedTextEnd(const QString &text);
408410

409411
QString getNextcloudFileLink() const;
410412

src/helpers/qownnotesmarkdownhighlighter.cpp

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "mainwindow.h"
2828
#include "qownspellchecker.h"
2929

30+
constexpr qreal ENCRYPTED_TEXT_FONT_SCALE = 1.5;
31+
3032
QOwnNotesMarkdownHighlighter::QOwnNotesMarkdownHighlighter(QTextDocument *parent,
3133
HighlightingOptions highlightingOptions)
3234
: MarkdownHighlighter(parent, highlightingOptions) {
@@ -41,6 +43,9 @@ void QOwnNotesMarkdownHighlighter::updateCurrentNote(Note *note) {
4143
if (note != nullptr) {
4244
_currentNote = note;
4345
}
46+
47+
_hasEncrypted = _currentNote ? _currentNote->hasEncryptedNoteText() : false;
48+
_highlightEncrypted = false; // Reset state when note changes
4449
}
4550

4651
/**
@@ -57,22 +62,44 @@ void QOwnNotesMarkdownHighlighter::highlightBlock(const QString &text) {
5762
setCurrentBlockState(HighlighterState::NoState);
5863
currentBlock().setUserState(HighlighterState::NoState);
5964

60-
// do the Markdown highlighting before the spellcheck highlighting
61-
// if we do it afterward, it overwrites the spellcheck highlighting
62-
MarkdownHighlighter::highlightMarkdown(text);
63-
if (text.contains(QLatin1String("note://")) ||
64-
text.contains(QChar('.') + _defaultNoteFileExt)) {
65-
highlightBrokenNotesLink(text);
65+
// Only check for encryption markers if the note has encrypted content
66+
if (_hasEncrypted) {
67+
if (Note::isEncryptedTextBegin(text)) {
68+
_highlightEncrypted = true;
69+
}
6670
}
6771

68-
// skip spell checking empty blocks and blocks with just "spaces"
69-
// the rest of the highlighting needs to be done e.g. for code blocks with
70-
// empty lines
71-
if (!text.isEmpty() && QOwnSpellChecker::instance()->isActive()) {
72-
highlightSpellChecking(text);
72+
if (_highlightEncrypted) {
73+
auto format = _formats[MaskedSyntax];
74+
// Reduce the font size for encrypted text
75+
format.setFontPointSize(format.fontPointSize() / ENCRYPTED_TEXT_FONT_SCALE);
76+
setFormat(0, text.length(), format);
77+
} else {
78+
// do the Markdown highlighting before the spellcheck highlighting
79+
// if we do it afterward, it overwrites the spellcheck highlighting
80+
highlightMarkdown(text);
7381
}
7482

75-
highlightScriptingRules(ScriptingService::instance()->getHighlightingRules(), text);
83+
if (!_highlightEncrypted) {
84+
if (text.contains(QLatin1String("note://")) ||
85+
text.contains(QChar('.') + _defaultNoteFileExt)) {
86+
highlightBrokenNotesLink(text);
87+
}
88+
89+
// skip spell checking empty blocks and blocks with just "spaces"
90+
// the rest of the highlighting needs to be done e.g. for code blocks with
91+
// empty lines
92+
if (!text.isEmpty() && QOwnSpellChecker::instance()->isActive()) {
93+
highlightSpellChecking(text);
94+
}
95+
96+
highlightScriptingRules(ScriptingService::instance()->getHighlightingRules(), text);
97+
}
98+
99+
// Only check for encryption end marker if we're highlighting encrypted text
100+
if (_hasEncrypted && _highlightEncrypted && Note::isEncryptedTextEnd(text)) {
101+
_highlightEncrypted = false;
102+
}
76103

77104
_highlightingFinished = true;
78105
}

src/helpers/qownnotesmarkdownhighlighter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class QOwnNotesMarkdownHighlighter : public MarkdownHighlighter {
6464

6565
private:
6666
Note *_currentNote = nullptr;
67+
bool _hasEncrypted = false;
68+
bool _highlightEncrypted = false;
6769

6870
QString _defaultNoteFileExt;
6971
QRegularExpression _regexTagStyleLink;

0 commit comments

Comments
 (0)