Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions lib/src/annotation_editing_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ class AnnotationEditingController extends TextEditingController {

// Generate the Regex pattern for matching all the suggestions in one.
AnnotationEditingController(this._mapping)
: _pattern = _mapping.keys.isNotEmpty
? "(${_mapping.keys.map((key) => RegExp.escape(key)).join('|')})"
: null;
{
_pattern = null;

if(_mapping.keys.isNotEmpty){
var result = _mapping.keys.map((key) => RegExp.escape(key)).toList();
result.sort((b,a) => a.toLowerCase().compareTo(b.toLowerCase()));
var finalresult = result.join('|');
_pattern = finalresult;
}
Comment on lines +14 to +19
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NEEDS TO BE MERGED PLS :)

}

/// Can be used to get the markup from the controller directly.
String get markupText {
Expand Down Expand Up @@ -51,7 +58,11 @@ class AnnotationEditingController extends TextEditingController {
set mapping(Map<String, Annotation> _mapping) {
this._mapping = _mapping;

_pattern = "(${_mapping.keys.map((key) => RegExp.escape(key)).join('|')})";
var result = _mapping.keys.map((key) => RegExp.escape(key)).toList();
result.sort((b,a) => a.toLowerCase().compareTo(b.toLowerCase()));
var finalresult = result.join('|');
_pattern = finalresult;

}

@override
Expand Down
57 changes: 55 additions & 2 deletions lib/src/mention_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,59 @@ class FlutterMentionsState extends State<FlutterMentions> {

final lengthMap = <LengthMap>[];

// split on each word and generate a list with start & end position of each word.
controller!.value.text.split(RegExp(r'(\s)')).forEach((element) {
List<String> textList = controller!.value.text.split(RegExp(r'(\s)'));

_pattern = widget.mentions.map((e) => e.trigger).join('|');

var mentionIndex = -2;

List triggerList = widget.mentions.map((e) => e.trigger).toList();

triggerList.forEach((element) {
var triggerIndex = textList.lastIndexWhere((e) => e.contains(element));
if (triggerIndex > mentionIndex) {
mentionIndex = triggerIndex;
}
});

if (textList.length - 1 > mentionIndex && mentionIndex != -1) {
var nextWordIndex = mentionIndex + 1;

var mention = textList[mentionIndex] + ' ' + textList[nextWordIndex];

_pattern = widget.mentions.map((e) => e.trigger).join('|');

// Filter the list based on the latest entered mention
final list =
widget.mentions.firstWhere((e) => mention.contains(e.trigger)).data;

// Loop until the the mention is contain in given mention list or not
while (list.indexWhere((element) {
final displayName = element['display'].toLowerCase();
return displayName == mention.substring(1).toLowerCase() ||
displayName.contains(mention.substring(1).toLowerCase());
}) !=
-1) {
// Assign full name mention to the list if the mention is is exist in the list
textList[mentionIndex] = mention;

// Assign null to the next word because it's already concatenate to the mention index word
textList[nextWordIndex] = "null";

// If the word is exist on the next index then concatenate it otherwise break the loop
if (textList.length - 1 > nextWordIndex) {
// concatenate the next word to the mention and again iterate the while loop with condition of check weather the mention is available or in the list or not
mention = mention + ' ' + textList[++nextWordIndex];
} else {
break;
}
}
}

// Remove all the null entries from the list
textList.removeWhere((element) => element == "null");

textList.forEach((element) {
lengthMap.add(
LengthMap(str: element, start: _pos, end: _pos + element.length));

Expand Down Expand Up @@ -380,6 +431,8 @@ class FlutterMentionsState extends State<FlutterMentions> {
controller!.text = widget.defaultText!;
}

_pattern = widget.mentions.map((e) => e.trigger).join('|');

// setup a listener to figure out which suggestions to show based on the trigger
controller!.addListener(suggestionListerner);

Expand Down