buildSpans method

List<TSpan> buildSpans({
  1. bool revealAll = true,
})

Builds a list of spans representing the entire text with censoring applied.

This method iterates through the text, creating spans for both normal and censored portions using buildPlainSpan and buildCensoredSpan.

Parameters:

  • revealAll - Whether to reveal all profane words (true) or censor them (false)

Returns a list of TSpan objects representing the complete text.

Implementation

List<TSpan> buildSpans({bool revealAll = true}) {
  final matches = censorIt.matches;
  if (matches.isEmpty) {
    return [
      buildPlainSpan(_text),
    ];
  }

  final spans = <TSpan>[];
  int lastEnd = 0;
  int wordIndex = 0;

  for (final m in matches) {
    if (m.start > lastEnd) {
      spans.add(buildPlainSpan(_text.substring(lastEnd, m.start)));
    }

    final word = _text.substring(m.start, m.end);
    spans.add(buildCensoredSpan(word, wordIndex, revealAll));

    lastEnd = m.end;
    wordIndex++;
  }

  if (lastEnd < _text.length) {
    spans.add(buildPlainSpan(_text.substring(lastEnd)));
  }

  return spans;
}