extractStyles function

void extractStyles(
  1. String value,
  2. List<String> styleList
)

Implementation

void extractStyles(String value, List<String> styleList) {
  String under = "###und###";
  String over = "###ove###";
  String strike = "###str###";
  String sub = "###sub###";
  String sup = "###sup###";
  String bold = "###bol###";
  String italic = "###ita###";
  String code = "###code###";
  List<String> styles = [];
  int? i;

  value = value.trim();

  while (true) {
    List<int> leastIndex = [
      value.indexOf(under),
      value.indexOf(over),
      value.indexOf(strike),
      value.indexOf(sub),
      value.indexOf(sup),
      value.indexOf(bold),
      value.indexOf(italic),
      value.indexOf(code),
    ];

    if (leastIndex.isNotEmpty) {
      leastIndex.removeWhere((element) => element == -1);
    }
    if (leastIndex.isNotEmpty) i = leastIndex.reduce(min);

    if (leastIndex.isEmpty) {
      //check for no matches
      if (isNullOrEmpty(value)) return;
      textValues.add(TextValue(List.from(styles), value.substring(0)));
      return;
    }

    if (i! > 0) {
      //if index > 0 return value with styles attached
      textValues.add(TextValue(
          List.from(styles),
          value.substring(
              0, i))); //return everything before the match with styles attached
      value = value.substring(i);
    }

    if (value.substring(0, 9) == under) {
      if (styles.contains("underline")) {
        styles.remove("underline");
      } else {
        styles.add("underline");
      }

      value = value.substring(9);
    } else if (value.substring(0, 9) == over) {
      if (styles.contains("overline")) {
        styles.remove("overline");
      } else {
        styles.add("overline");
      }

      value = value.substring(9);
    } else if (value.substring(0, 9) == strike) {
      if (styles.contains("strikethrough")) {
        styles.remove("strikethrough");
      } else {
        styles.add("strikethrough");
      }

      value = value.substring(9);
    } else if (value.substring(0, 9) == sub) {
      if (styles.contains("subscript")) {
        styles.remove("subscript");
      } else {
        styles.add("subscript");
      }

      value = value.substring(9);
    } else if (value.substring(0, 9) == sup) {
      if (styles.contains("superscript")) {
        styles.remove("superscript");
      } else {
        styles.add("superscript");
      }

      value = value.substring(9);
    } else if (value.substring(0, 9) == bold) {
      if (styles.contains("bold")) {
        styles.remove("bold");
      } else {
        styles.add("bold");
      }

      value = value.substring(9);
    } else if (value.substring(0, 9) == italic) {
      if (styles.contains("italic")) {
        styles.remove("italic");
      } else {
        styles.add("italic");
      }

      value = value.substring(9);
    } else if (value.substring(0, 10) == code) {
      if (styles.contains("code")) {
        styles.remove("code");
      } else {
        styles.add("code");
      }

      value = value.substring(10);
    } else {
      return;
    }
  }
}