insertMention method

void insertMention(
  1. MentionData<Object?> mentionModel
)

Inserts the mention in the place of the current editable mention

Implementation

void insertMention(MentionData mentionModel) {
  final start = lastMention.value?.locationStart;
  final end = lastMention.value?.locationEnd;

  if (start != null && end != null) {
    final needAddSpaceAtEnd =
        end >= text.length || text.codeUnitAt(end) != kSpaceUTF16Code;

    final mentionName = mentionModel.mentionName ?? '';
    final mentionText =
        needAddSpaceAtEnd ? '$tag$mentionName ' : '$tag$mentionName';

    final newText = text.replaceRange(
      start,
      end,
      mentionText,
    );

    /// Shift cursor by one symbol after the mention (immediately after
    /// the space symbol, which is sure to be after the mention), so that
    /// suggestions no longer come
    ///
    /// 2 = one [tag] symbol before mention + one space symbol after mention
    final newCursorPos = start + mentionName.length + 2;
    value = value.copyWith(
      text: newText,
      selection: TextSelection.collapsed(offset: newCursorPos),
      composing: TextRange.empty,
    );

    _mentionedObject.add(mentionModel);
  }
}