mergeAttributedText method

List<AttributedText> mergeAttributedText(
  1. List<AttributedText> attributedTexts,
  2. List<AttributedText> existingAttributedTexts
)

mergeAttributedText is a function which is used to merge the attributed text of various formatters

Implementation

List<AttributedText> mergeAttributedText(
  List<AttributedText> attributedTexts,
  List<AttributedText> existingAttributedTexts,
) {
  // Combine both lists into a single list
  List<AttributedText> combinedList = [
    ...attributedTexts,
    ...existingAttributedTexts
  ];

  // Sort the combined list based on the start property
  combinedList.sort((a, b) => a.start.compareTo(b.start));

  // Merge overlapping elements
  List<AttributedText> mergedList = [];
  AttributedText? currentElement;
  for (var element in combinedList) {
    if (currentElement == null) {
      currentElement = element;
      mergedList.add(element);
      continue;
    }

    // Check for overlap
    if (element.start >= currentElement.end) {
      // Update end if necessary
      //   currentElement.end =
      //       element.end > currentElement.end ? element.end : currentElement.end;
      // } else {
      // No overlap, add current element and start a new one
      currentElement = element;
      mergedList.add(element);
    }
  }

  return mergedList;
}