Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

refactor: move position to core/selection

Lucas.Xub0257a626d22
diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/appflowy_editor.dart b/frontend/app_flowy/packages/appflowy_editor/lib/appflowy_editor.dart
index 6138c79c0e..72a515e3fa 100644
--- a/frontend/app_flowy/packages/appflowy_editor/lib/appflowy_editor.dart
+++ b/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';
diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/document/position.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/core/selection/position.dart
similarity index 65%
rename from frontend/app_flowy/packages/appflowy_editor/lib/src/document/position.dart
rename to frontend/app_flowy/packages/appflowy_editor/lib/src/core/selection/position.dart
index 1fd6c90f24..e793faa625 100644
--- a/frontend/app_flowy/packages/appflowy_editor/lib/src/document/position.dart
+++ b/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,
};
}
}
diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/document/selection.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/document/selection.dart
index ca15ee51f0..eaf3f5f4fb 100644
--- a/frontend/app_flowy/packages/appflowy_editor/lib/src/document/selection.dart
+++ b/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.
///
diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/extensions/text_node_extensions.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/extensions/text_node_extensions.dart
index e671b47645..2c31008c79 100644
--- a/frontend/app_flowy/packages/appflowy_editor/lib/src/extensions/text_node_extensions.dart
+++ b/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';
diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/operation/transaction_builder.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/operation/transaction_builder.dart
index c1dce826dd..31b2be659e 100644
--- a/frontend/app_flowy/packages/appflowy_editor/lib/src/operation/transaction_builder.dart
+++ b/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';
diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/render/image/image_node_widget.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/render/image/image_node_widget.dart
index 765164866d..6f173a3c98 100644
--- a/frontend/app_flowy/packages/appflowy_editor/lib/src/render/image/image_node_widget.dart
+++ b/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';
diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/default_selectable.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/default_selectable.dart
index fd86f84831..69176c544e 100644
--- a/frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/default_selectable.dart
+++ b/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';
diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/flowy_rich_text.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/flowy_rich_text.dart
index c6bf271e5d..1b4acaf24f 100644
--- a/frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/flowy_rich_text.dart
+++ b/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';
diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/render/selection/selectable.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/render/selection/selectable.dart
index 6f4f92c2e9..56ebc9cc71 100644
--- a/frontend/app_flowy/packages/appflowy_editor/lib/src/render/selection/selectable.dart
+++ b/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';
diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/default_text_operations/format_rich_text_style.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/default_text_operations/format_rich_text_style.dart
index aabc51389d..9cc45c7f11 100644
--- a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/default_text_operations/format_rich_text_style.dart
+++ b/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';
diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/select_all_handler.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/select_all_handler.dart
index fa632ed585..e11deb188b 100644
--- a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/select_all_handler.dart
+++ b/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';
diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/whitespace_handler.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/whitespace_handler.dart
index 41823623a4..917831cc3b 100644
--- a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/whitespace_handler.dart
+++ b/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';
diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/selection_service.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/selection_service.dart
index c3a9bd6866..3badb2de2f 100644
--- a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/selection_service.dart
+++ b/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';
diff --git a/frontend/app_flowy/packages/appflowy_editor/test/core/selection/position_test.dart b/frontend/app_flowy/packages/appflowy_editor/test/core/selection/position_test.dart
new file mode 100644
index 0000000000..ded398e968
--- /dev/null
+++ b/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,
+ });
+ });
+ });
+}
diff --git a/frontend/app_flowy/packages/appflowy_editor/test/legacy/flowy_editor_test.dart b/frontend/app_flowy/packages/appflowy_editor/test/legacy/flowy_editor_test.dart
index bdfd4e0394..8fd6e71b87 100644
--- a/frontend/app_flowy/packages/appflowy_editor/test/legacy/flowy_editor_test.dart
+++ b/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';