selection.dart 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import 'package:flowy_editor/document/path.dart';
  2. import 'package:flowy_editor/document/position.dart';
  3. import 'package:flowy_editor/extensions/path_extensions.dart';
  4. class Selection {
  5. final Position start;
  6. final Position end;
  7. Selection({
  8. required this.start,
  9. required this.end,
  10. });
  11. Selection.single({
  12. required Path path,
  13. required int startOffset,
  14. int? endOffset,
  15. }) : start = Position(path: path, offset: startOffset),
  16. end = Position(path: path, offset: endOffset ?? startOffset);
  17. Selection.collapsed(Position position)
  18. : start = position,
  19. end = position;
  20. Selection collapse({bool atStart = false}) {
  21. if (atStart) {
  22. return Selection(start: start, end: start);
  23. } else {
  24. return Selection(start: end, end: end);
  25. }
  26. }
  27. bool get isCollapsed => start == end;
  28. bool get isUpward =>
  29. start.path >= end.path && !pathEquals(start.path, end.path);
  30. bool get isDownward =>
  31. start.path <= end.path && !pathEquals(start.path, end.path);
  32. Selection copyWith({Position? start, Position? end}) {
  33. return Selection(
  34. start: start ?? this.start,
  35. end: end ?? this.end,
  36. );
  37. }
  38. @override
  39. String toString() => '[Selection] start = $start, end = $end';
  40. }