Skip to content

Commit

Permalink
krille-chan#39 Highlight search term - not working in citations
Browse files Browse the repository at this point in the history
  • Loading branch information
carowebtec authored and Matthias committed Mar 6, 2023
1 parent d7afb61 commit 36ef733
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
28 changes: 22 additions & 6 deletions lib/pages/chat/events/message_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,33 @@ class MessageContent extends StatelessWidget {
);
}

bool _isSearchResult() {
return searchTerm != null && searchTerm!.isNotEmpty;
}

String _getEventTextFormatted() {
var text = event.formattedText;
if (searchTerm != null && searchTerm!.isNotEmpty) {
text = text.replaceAll(searchTerm!,
"<span style='background-color:$AppConfig.primaryColorLight'>$searchTerm</span>");
return text;
if (_isSearchResult()) {
// search result messages may not be formatted-messages originally
// -> take normal text in this case
if (text.isEmpty) {
text = event.text;
}

return _replaceSearchResults(text);
} else {
return text;
}
}

String _replaceSearchResults(String text) {
final searchExp = RegExp(searchTerm!, caseSensitive: false);
text = text.replaceAllMapped(
searchExp, (match) => "<b><i>${match[0]}</i></b>");

return text;
}

@override
Widget build(BuildContext context) {
final fontSize = AppConfig.messageFontSize * AppConfig.fontSizeFactor;
Expand Down Expand Up @@ -144,8 +160,8 @@ class MessageContent extends StatelessWidget {
case MessageTypes.Emote:
if (AppConfig.renderHtml &&
!event.redacted &&
event.isRichMessage) {
var html = _getEventTextFormatted();
(event.isRichMessage || _isSearchResult())) {
String html = _getEventTextFormatted();
if (event.messageType == MessageTypes.Emote) {
html = '* $html';
}
Expand Down
17 changes: 10 additions & 7 deletions lib/pages/chat_search/chat_search_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,16 @@ class ChatSearchView extends StatelessWidget {
crossAxisAlignment:
CrossAxisAlignment.stretch,
children: <Widget>[
Message(snapshot.data![i - 1],
onSwipe: (direction) => {},
onSelect:
controller.onSelectMessage,
scrollToEventId:
controller.scrollToEventId,
timeline: controller.timeline!),
Message(
snapshot.data![i - 1],
onSwipe: (direction) => {},
onSelect:
controller.onSelectMessage,
scrollToEventId:
controller.scrollToEventId,
timeline: controller.timeline!,
searchTerm: controller.searchTerm,
),
if (i == snapshot.data?.length &&
controller.searchState ==
SearchState.searching)
Expand Down

0 comments on commit 36ef733

Please sign in to comment.