From ce7ebd6d07d9ced0084bf53a13d15bf691763665 Mon Sep 17 00:00:00 2001 From: darshanp Date: Sun, 30 May 2021 18:59:14 +0530 Subject: [PATCH 1/3] Entering full name mention with space issue fixed --- lib/src/mention_view.dart | 52 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/lib/src/mention_view.dart b/lib/src/mention_view.dart index ec8be89..be0f8f8 100644 --- a/lib/src/mention_view.dart +++ b/lib/src/mention_view.dart @@ -327,8 +327,54 @@ class FlutterMentionsState extends State { final 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 strList = controller!.value.text.split(RegExp(r'(\s)')); + + _pattern = widget.mentions.map((e) => e.trigger).join('|'); + + var mentionIndex = -2; + + List str = widget.mentions.map((e) => e.trigger).toList(); + + str.forEach((element) { + + var tempIndex = strList.lastIndexWhere((e) => e.contains(element)); + + if(tempIndex > mentionIndex){ + mentionIndex = tempIndex; + } + }); + + if(strList.length -1 > mentionIndex) { + var i = mentionIndex + 1; + + var element = strList[mentionIndex] + ' ' + strList[i]; + + _pattern = widget.mentions.map((e) => e.trigger).join('|'); + + final list = widget.mentions + .firstWhere( + (e) => element.contains(e.trigger)) + .data; + + while (!strList[i].contains(_pattern) && list.indexWhere((element2) { + final ele = element2['display'].toLowerCase(); + return ele == element.substring(1).toLowerCase() || + ele.contains(element.substring(1).toLowerCase()); + }) != -1) { + strList[mentionIndex] = element; + strList[i] = "null"; + + if (strList.length - 1 > i) { + element = element + strList[++i]; + } else { + break; + } + } + } + + strList.removeWhere((element) => element == "null"); + + strList.forEach((element) { lengthMap.add( LengthMap(str: element, start: _pos, end: _pos + element.length)); @@ -380,6 +426,8 @@ class FlutterMentionsState extends State { 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); From 1a8a278fff1372f64c95223590e0202216fadffa Mon Sep 17 00:00:00 2001 From: darshanp Date: Sun, 30 May 2021 19:13:07 +0530 Subject: [PATCH 2/3] Same name mentioning issue fixed --- lib/src/annotation_editing_controller.dart | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/src/annotation_editing_controller.dart b/lib/src/annotation_editing_controller.dart index 7df935b..cf95544 100644 --- a/lib/src/annotation_editing_controller.dart +++ b/lib/src/annotation_editing_controller.dart @@ -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; + } + } /// Can be used to get the markup from the controller directly. String get markupText { @@ -51,7 +58,11 @@ class AnnotationEditingController extends TextEditingController { set mapping(Map _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 From 7e0385c79fa73e2bf023b4e976c30491dcd36a05 Mon Sep 17 00:00:00 2001 From: darshanp Date: Mon, 31 May 2021 11:51:11 +0530 Subject: [PATCH 3/3] Code refactoring --- lib/src/mention_view.dart | 61 +++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/lib/src/mention_view.dart b/lib/src/mention_view.dart index be0f8f8..f5a2017 100644 --- a/lib/src/mention_view.dart +++ b/lib/src/mention_view.dart @@ -327,54 +327,59 @@ class FlutterMentionsState extends State { final lengthMap = []; - List strList = controller!.value.text.split(RegExp(r'(\s)')); + List textList = controller!.value.text.split(RegExp(r'(\s)')); _pattern = widget.mentions.map((e) => e.trigger).join('|'); var mentionIndex = -2; - List str = widget.mentions.map((e) => e.trigger).toList(); + List triggerList = widget.mentions.map((e) => e.trigger).toList(); - str.forEach((element) { - - var tempIndex = strList.lastIndexWhere((e) => e.contains(element)); - - if(tempIndex > mentionIndex){ - mentionIndex = tempIndex; + triggerList.forEach((element) { + var triggerIndex = textList.lastIndexWhere((e) => e.contains(element)); + if (triggerIndex > mentionIndex) { + mentionIndex = triggerIndex; } }); - if(strList.length -1 > mentionIndex) { - var i = mentionIndex + 1; + if (textList.length - 1 > mentionIndex && mentionIndex != -1) { + var nextWordIndex = mentionIndex + 1; - var element = strList[mentionIndex] + ' ' + strList[i]; + var mention = textList[mentionIndex] + ' ' + textList[nextWordIndex]; _pattern = widget.mentions.map((e) => e.trigger).join('|'); - final list = widget.mentions - .firstWhere( - (e) => element.contains(e.trigger)) - .data; - - while (!strList[i].contains(_pattern) && list.indexWhere((element2) { - final ele = element2['display'].toLowerCase(); - return ele == element.substring(1).toLowerCase() || - ele.contains(element.substring(1).toLowerCase()); - }) != -1) { - strList[mentionIndex] = element; - strList[i] = "null"; - - if (strList.length - 1 > i) { - element = element + strList[++i]; + // 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; } } } - strList.removeWhere((element) => element == "null"); + // Remove all the null entries from the list + textList.removeWhere((element) => element == "null"); - strList.forEach((element) { + textList.forEach((element) { lengthMap.add( LengthMap(str: element, start: _pos, end: _pos + element.length));