setJump method

WidgetElement setJump(
  1. Map<int, WidgetElement> map
)
inherited

Splits the value into the specified elements. Accepts a map with the widget key, works together with the getJump function.

Implementation

WidgetElement setJump(Map<int, WidgetElement> map) {
  if (_node.value.isEmpty) return this;

  String value = _node.value;

  /// Allows you to see if there is anything other than
  /// a tag in the value. Sometimes instead of a value,
  /// only a jump is used.
  bool onlyJump = isJump(value);

  List<int> keys = map.keys.toList()..sort();
  List<int> keysPosition = [];
  List<int> keysLength = [];

  for (int key in keys) {
    // The jump function is used so as not
    // to duplicate it, since its name
    // is incorrect in this context.
    keysPosition.add(value.indexOf(getJump(key)));
    keysLength.add(getJump(key).length);
  }

  void add(WidgetElement element) {
    _node.children?.add(element.node);
  }

  for (int i = 0; i < keysPosition.length; i++) {
    int key = keysPosition[i];
    int keyLength = keysLength[i];

    WidgetElement element = map[keys[i]]!;

    int nextKey = 0;
    if ((keysPosition.length - 1) != i) {
      nextKey = keysPosition[i + 1];
    } else {
      // If it is the last position in the array,
      // there will be no next key,
      // but the value must be saved.
      nextKey = value.length;
    }

    if (key == keysPosition.first) {
      if (!onlyJump) add(Span(value.substring(0, key)));
      add(element);
      if (!onlyJump) add(Span(value.substring(key + keyLength, nextKey)));
      continue;
    }

    if (key == keysPosition.last) {
      add(element);
      add(Span(value.substring(key + keyLength, value.length)));
      continue;
    }

    add(element);
    add(Span(value.substring(key + keyLength, nextKey)));
  }

  _node.value = '';
  return this;
}