offsetSnapshotFromEditorState function

TextOffsetStateSnapshot offsetSnapshotFromEditorState(
  1. TextDocument document,
  2. EditorState editorState, {
  3. required int textLength,
  4. bool preserveCollapsedSelection = false,
})

Implementation

TextOffsetStateSnapshot offsetSnapshotFromEditorState(
  TextDocument document,
  EditorState editorState, {
  required int textLength,
  bool preserveCollapsedSelection = false,
}) {
  final cursorOffset = document
      .offsetForPosition(editorState.cursor)
      .clamp(0, textLength);
  final selection = editorState.selection;
  if (selection == null ||
      (selection.isCollapsed && !preserveCollapsedSelection)) {
    return TextOffsetStateSnapshot(cursorOffset: cursorOffset);
  }

  return TextOffsetStateSnapshot(
    cursorOffset: cursorOffset,
    selectionBaseOffset: document
        .offsetForPosition(selection.base)
        .clamp(0, textLength),
    selectionExtentOffset: document
        .offsetForPosition(selection.extent)
        .clamp(0, textLength),
  );
}