forked from krille-chan/fluffychat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
krille-chan#39 Highlight search term in citations
- Loading branch information
1 parent
36ef733
commit 507f25e
Showing
4 changed files
with
76 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import 'package:matrix/matrix.dart'; | ||
|
||
class SearchResultFormatter { | ||
String? _searchTerm = ""; | ||
RegExp? _searchExp; | ||
|
||
SearchResultFormatter(String? searchTerm) { | ||
_searchTerm = searchTerm; | ||
if (isSearchResult()) { | ||
_searchExp = RegExp(searchTerm!, caseSensitive: false); | ||
} | ||
} | ||
|
||
bool isSearchResult() { | ||
return _searchTerm != null && _searchTerm!.isNotEmpty; | ||
} | ||
|
||
String getEventTextFormatted(Event event) { | ||
var text = event.formattedText; | ||
if (isSearchResult()) { | ||
// search result messages may not be a 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 startIndex = _getStartIndex(text); | ||
text = text.replaceAllMapped( | ||
_searchExp!, (match) => "<b><i>${match[0]}</i></b>"); | ||
|
||
return text; | ||
} | ||
|
||
/*int _getStartIndex(String text) { | ||
const replyEndText = "</mx-reply>"; | ||
final replyIndex = text.indexOf(replyEndText); | ||
if(replyIndex < 0) { | ||
return 0; | ||
} | ||
else { | ||
// if text contains a replay start after reply | ||
return replyIndex+replyEndText.length; | ||
} | ||
} | ||
*/ | ||
} |