appflowy-editor/lib/src/core/location/position.dart

54 lines
1 KiB
Dart
Raw Normal View History

2022-10-09 23:53:18 +08:00
import 'package:appflowy_editor/src/core/document/path.dart';
class Position {
final Path path;
final int offset;
Position({
required this.path,
this.offset = 0,
});
Position.invalid()
: path = [-1],
offset = -1;
2023-03-09 14:38:54 +07:00
factory Position.fromJson(Map<String, dynamic> json) {
final path = Path.from(json['path'] as List);
final offset = json['offset'];
2023-03-24 13:59:03 +01:00
2023-03-09 14:38:54 +07:00
return Position(
path: path,
offset: offset ?? 0,
);
}
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is Position &&
other.path.equals(path) &&
other.offset == offset;
}
@override
int get hashCode => Object.hash(offset, Object.hashAll(path));
@override
String toString() => 'path = $path, offset = $offset';
Position copyWith({Path? path, int? offset}) {
return Position(
path: path ?? this.path,
offset: offset ?? this.offset,
);
}
Map<String, dynamic> toJson() {
return {
'path': path,
'offset': offset,
};
}
}