getGlobalOffsetInParagraph method

int? getGlobalOffsetInParagraph(
  1. String paragraphId,
  2. String fragmentId,
  3. int localOffset
)

Calculates the global offset within paragraphId for a local (fragmentId, localOffset) pair. Returns null if the fragment is not found.

Implementation

int? getGlobalOffsetInParagraph(String paragraphId, String fragmentId, int localOffset) {
  final node = nodeById(paragraphId);
  if (node is! Paragraph) return null;
  int global = 0;
  bool found = false;
  void visit(FNode child) {
    if (found) return;
    if (child is Fragment) {
      if (child.id == fragmentId) {
        global += localOffset;
        found = true;
        return;
      }
      global += child.text.length;
    } else if (child is Link) {
      for (final linkChild in child.fragments) {
        if (found) return;
        if (linkChild is Fragment) {
          if (linkChild.id == fragmentId) {
            global += localOffset;
            found = true;
            return;
          }
          global += linkChild.text.length;
        }
      }
    }
  }
  for (final child in node.fragments) {
    visit(child);
  }
  return found ? global : null;
}