Просмотр исходного кода

refactor: move position to core/selection

Lucas.Xu 2 лет назад
Родитель
Сommit
b0257a626d
15 измененных файлов с 50 добавлено и 26 удалено
  1. 1 1
      frontend/app_flowy/packages/appflowy_editor/lib/appflowy_editor.dart
  2. 11 13
      frontend/app_flowy/packages/appflowy_editor/lib/src/core/selection/position.dart
  3. 1 1
      frontend/app_flowy/packages/appflowy_editor/lib/src/document/selection.dart
  4. 1 1
      frontend/app_flowy/packages/appflowy_editor/lib/src/extensions/text_node_extensions.dart
  5. 1 1
      frontend/app_flowy/packages/appflowy_editor/lib/src/operation/transaction_builder.dart
  6. 1 1
      frontend/app_flowy/packages/appflowy_editor/lib/src/render/image/image_node_widget.dart
  7. 1 1
      frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/default_selectable.dart
  8. 1 1
      frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/flowy_rich_text.dart
  9. 1 1
      frontend/app_flowy/packages/appflowy_editor/lib/src/render/selection/selectable.dart
  10. 1 1
      frontend/app_flowy/packages/appflowy_editor/lib/src/service/default_text_operations/format_rich_text_style.dart
  11. 1 1
      frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/select_all_handler.dart
  12. 1 1
      frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/whitespace_handler.dart
  13. 1 1
      frontend/app_flowy/packages/appflowy_editor/lib/src/service/selection_service.dart
  14. 26 0
      frontend/app_flowy/packages/appflowy_editor/test/core/selection/position_test.dart
  15. 1 1
      frontend/app_flowy/packages/appflowy_editor/test/legacy/flowy_editor_test.dart

+ 1 - 1
frontend/app_flowy/packages/appflowy_editor/lib/appflowy_editor.dart

@@ -5,7 +5,7 @@ export 'src/infra/log.dart';
 export 'src/render/style/editor_style.dart';
 export 'src/core/document/node.dart';
 export 'src/core/document/path.dart';
-export 'src/document/position.dart';
+export 'src/core/selection/position.dart';
 export 'src/document/selection.dart';
 export 'src/document/state_tree.dart';
 export 'src/core/document/text_delta.dart';

+ 11 - 13
frontend/app_flowy/packages/appflowy_editor/lib/src/document/position.dart → frontend/app_flowy/packages/appflowy_editor/lib/src/core/selection/position.dart

@@ -11,17 +11,18 @@ class Position {
 
   @override
   bool operator ==(Object other) {
-    if (other is! Position) {
-      return false;
-    }
-    return path.equals(other.path) && offset == other.offset;
+    if (identical(this, other)) return true;
+
+    return other is Position &&
+        other.path.equals(path) &&
+        other.offset == offset;
   }
 
   @override
-  int get hashCode {
-    final pathHash = Object.hashAll(path);
-    return Object.hash(pathHash, offset);
-  }
+  int get hashCode => Object.hash(offset, Object.hashAll(path));
+
+  @override
+  String toString() => 'path = $path, offset = $offset';
 
   Position copyWith({Path? path, int? offset}) {
     return Position(
@@ -30,13 +31,10 @@ class Position {
     );
   }
 
-  @override
-  String toString() => 'path = $path, offset = $offset';
-
   Map<String, dynamic> toJson() {
     return {
-      "path": path.toList(),
-      "offset": offset,
+      'path': path,
+      'offset': offset,
     };
   }
 }

+ 1 - 1
frontend/app_flowy/packages/appflowy_editor/lib/src/document/selection.dart

@@ -1,5 +1,5 @@
 import 'package:appflowy_editor/src/core/document/path.dart';
-import 'package:appflowy_editor/src/document/position.dart';
+import 'package:appflowy_editor/src/core/selection/position.dart';
 
 /// Selection represents the selected area or the cursor area in the editor.
 ///

+ 1 - 1
frontend/app_flowy/packages/appflowy_editor/lib/src/extensions/text_node_extensions.dart

@@ -1,6 +1,6 @@
 import 'package:appflowy_editor/src/core/document/node.dart';
 import 'package:appflowy_editor/src/core/document/path.dart';
-import 'package:appflowy_editor/src/document/position.dart';
+import 'package:appflowy_editor/src/core/selection/position.dart';
 import 'package:appflowy_editor/src/document/selection.dart';
 import 'package:appflowy_editor/src/core/document/text_delta.dart';
 import 'package:appflowy_editor/src/document/built_in_attribute_keys.dart';

+ 1 - 1
frontend/app_flowy/packages/appflowy_editor/lib/src/operation/transaction_builder.dart

@@ -4,7 +4,7 @@ import 'dart:math';
 import 'package:appflowy_editor/src/core/document/attributes.dart';
 import 'package:appflowy_editor/src/core/document/node.dart';
 import 'package:appflowy_editor/src/core/document/path.dart';
-import 'package:appflowy_editor/src/document/position.dart';
+import 'package:appflowy_editor/src/core/selection/position.dart';
 import 'package:appflowy_editor/src/document/selection.dart';
 import 'package:appflowy_editor/src/core/document/text_delta.dart';
 import 'package:appflowy_editor/src/editor_state.dart';

+ 1 - 1
frontend/app_flowy/packages/appflowy_editor/lib/src/render/image/image_node_widget.dart

@@ -1,6 +1,6 @@
 import 'package:appflowy_editor/src/extensions/object_extensions.dart';
 import 'package:appflowy_editor/src/core/document/node.dart';
-import 'package:appflowy_editor/src/document/position.dart';
+import 'package:appflowy_editor/src/core/selection/position.dart';
 import 'package:appflowy_editor/src/document/selection.dart';
 import 'package:appflowy_editor/src/infra/flowy_svg.dart';
 import 'package:appflowy_editor/src/render/selection/selectable.dart';

+ 1 - 1
frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/default_selectable.dart

@@ -1,4 +1,4 @@
-import 'package:appflowy_editor/src/document/position.dart';
+import 'package:appflowy_editor/src/core/selection/position.dart';
 import 'package:appflowy_editor/src/document/selection.dart';
 import 'package:appflowy_editor/src/render/selection/selectable.dart';
 import 'package:flutter/material.dart';

+ 1 - 1
frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/flowy_rich_text.dart

@@ -7,7 +7,7 @@ import 'package:flutter/rendering.dart';
 
 import 'package:appflowy_editor/src/core/document/node.dart';
 import 'package:appflowy_editor/src/core/document/path.dart';
-import 'package:appflowy_editor/src/document/position.dart';
+import 'package:appflowy_editor/src/core/selection/position.dart';
 import 'package:appflowy_editor/src/document/selection.dart';
 import 'package:appflowy_editor/src/core/document/text_delta.dart';
 import 'package:appflowy_editor/src/editor_state.dart';

+ 1 - 1
frontend/app_flowy/packages/appflowy_editor/lib/src/render/selection/selectable.dart

@@ -1,4 +1,4 @@
-import 'package:appflowy_editor/src/document/position.dart';
+import 'package:appflowy_editor/src/core/selection/position.dart';
 import 'package:appflowy_editor/src/document/selection.dart';
 import 'package:flutter/material.dart';
 

+ 1 - 1
frontend/app_flowy/packages/appflowy_editor/lib/src/service/default_text_operations/format_rich_text_style.dart

@@ -1,7 +1,7 @@
 import 'package:appflowy_editor/src/core/document/attributes.dart';
 import 'package:appflowy_editor/src/core/document/node.dart';
 import 'package:appflowy_editor/src/core/document/path.dart';
-import 'package:appflowy_editor/src/document/position.dart';
+import 'package:appflowy_editor/src/core/selection/position.dart';
 import 'package:appflowy_editor/src/document/selection.dart';
 import 'package:appflowy_editor/src/editor_state.dart';
 import 'package:appflowy_editor/src/extensions/text_node_extensions.dart';

+ 1 - 1
frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/select_all_handler.dart

@@ -1,5 +1,5 @@
 import 'package:appflowy_editor/src/core/document/node.dart';
-import 'package:appflowy_editor/src/document/position.dart';
+import 'package:appflowy_editor/src/core/selection/position.dart';
 import 'package:appflowy_editor/src/document/selection.dart';
 import 'package:appflowy_editor/src/service/shortcut_event/shortcut_event_handler.dart';
 import 'package:flutter/material.dart';

+ 1 - 1
frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/whitespace_handler.dart

@@ -3,7 +3,7 @@ import 'package:flutter/material.dart';
 import 'package:flutter/services.dart';
 import 'package:appflowy_editor/src/document/built_in_attribute_keys.dart';
 import 'package:appflowy_editor/src/core/document/node.dart';
-import 'package:appflowy_editor/src/document/position.dart';
+import 'package:appflowy_editor/src/core/selection/position.dart';
 import 'package:appflowy_editor/src/document/selection.dart';
 import 'package:appflowy_editor/src/editor_state.dart';
 import 'package:appflowy_editor/src/operation/transaction_builder.dart';

+ 1 - 1
frontend/app_flowy/packages/appflowy_editor/lib/src/service/selection_service.dart

@@ -4,7 +4,7 @@ import 'package:flutter/material.dart';
 import 'package:appflowy_editor/src/core/document/node.dart';
 import 'package:appflowy_editor/src/core/document/node_iterator.dart';
 import 'package:appflowy_editor/src/core/document/path.dart';
-import 'package:appflowy_editor/src/document/position.dart';
+import 'package:appflowy_editor/src/core/selection/position.dart';
 import 'package:appflowy_editor/src/document/selection.dart';
 import 'package:appflowy_editor/src/editor_state.dart';
 import 'package:appflowy_editor/src/extensions/node_extensions.dart';

+ 26 - 0
frontend/app_flowy/packages/appflowy_editor/test/core/selection/position_test.dart

@@ -0,0 +1,26 @@
+import 'package:appflowy_editor/appflowy_editor.dart';
+import 'package:flutter_test/flutter_test.dart';
+
+void main() async {
+  group('position.dart', () {
+    test('test position equality', () {
+      final positionA = Position(path: [0, 1, 2], offset: 3);
+      final positionB = Position(path: [0, 1, 2], offset: 3);
+      expect(positionA, positionB);
+
+      final positionC = positionA.copyWith(offset: 4);
+      final positionD = positionB.copyWith(path: [1, 2, 3]);
+      expect(positionC.offset, 4);
+      expect(positionD.path, [1, 2, 3]);
+
+      expect(positionA.toJson(), {
+        'path': [0, 1, 2],
+        'offset': 3,
+      });
+      expect(positionC.toJson(), {
+        'path': [0, 1, 2],
+        'offset': 4,
+      });
+    });
+  });
+}

+ 1 - 1
frontend/app_flowy/packages/appflowy_editor/test/legacy/flowy_editor_test.dart

@@ -1,5 +1,5 @@
 import 'package:appflowy_editor/src/core/document/path.dart';
-import 'package:appflowy_editor/src/document/position.dart';
+import 'package:appflowy_editor/src/core/selection/position.dart';
 import 'package:appflowy_editor/src/document/selection.dart';
 import 'package:flutter_test/flutter_test.dart';