moveHorizontal method

Position? moveHorizontal(
  1. EditorState editorState, {
  2. bool forward = true,
  3. SelectionRange selectionRange = SelectionRange.character,
})

Implementation

Position? moveHorizontal(
  EditorState editorState, {
  bool forward = true,
  SelectionRange selectionRange = SelectionRange.character,
}) {
  final node = editorState.document.nodeAtPath(path);
  if (node == null) {
    return null;
  }

  if (forward && offset == 0) {
    final previousEnd = node.previous?.selectable?.end();
    if (previousEnd != null) {
      return previousEnd;
    }
    return null;
  } else if (!forward) {
    final end = node.selectable?.end();
    if (end != null && offset >= end.offset) {
      return node.next?.selectable?.start();
    }
  }

  switch (selectionRange) {
    case SelectionRange.character:
      final delta = node.delta;
      if (delta != null) {
        return Position(
          path: path,
          offset: forward
              ? delta.prevRunePosition(offset)
              : delta.nextRunePosition(offset),
        );
      }

      return Position(path: path, offset: offset);
    case SelectionRange.word:
      final delta = node.delta;
      if (delta != null) {
        final result = forward
            ? node.selectable?.getWordBoundaryInPosition(
                Position(
                  path: path,
                  offset: delta.prevRunePosition(offset),
                ),
              )
            : node.selectable?.getWordBoundaryInPosition(this);
        if (result != null) {
          return forward ? result.start : result.end;
        }
      }

      return Position(path: path, offset: offset);
  }
}