selection.dart 949 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import 'package:flowy_editor/document/path.dart';
  2. import 'package:flowy_editor/document/position.dart';
  3. class Selection {
  4. final Position start;
  5. final Position end;
  6. Selection({
  7. required this.start,
  8. required this.end,
  9. });
  10. Selection.single({
  11. required Path path,
  12. required int startOffset,
  13. int? endOffset,
  14. }) : start = Position(path: path, offset: startOffset),
  15. end = Position(path: path, offset: endOffset ?? startOffset);
  16. Selection.collapsed(Position position)
  17. : start = position,
  18. end = position;
  19. Selection collapse({bool atStart = false}) {
  20. if (atStart) {
  21. return Selection(start: start, end: start);
  22. } else {
  23. return Selection(start: end, end: end);
  24. }
  25. }
  26. bool isCollapsed() {
  27. return start == end;
  28. }
  29. Selection copyWith({Position? start, Position? end}) {
  30. return Selection(
  31. start: start ?? this.start,
  32. end: end ?? this.end,
  33. );
  34. }
  35. }