|
@@ -1,7 +1,7 @@
|
|
-import 'package:flowy_editor/document/path.dart';
|
|
|
|
import 'package:flowy_editor/document/node.dart';
|
|
import 'package:flowy_editor/document/node.dart';
|
|
import 'package:flowy_editor/document/position.dart';
|
|
import 'package:flowy_editor/document/position.dart';
|
|
import 'package:flowy_editor/document/selection.dart';
|
|
import 'package:flowy_editor/document/selection.dart';
|
|
|
|
+import 'package:flowy_editor/render/selection/selectable.dart';
|
|
import 'package:flowy_editor/render/selection/cursor_widget.dart';
|
|
import 'package:flowy_editor/render/selection/cursor_widget.dart';
|
|
import 'package:flowy_editor/render/selection/flowy_selection_widget.dart';
|
|
import 'package:flowy_editor/render/selection/flowy_selection_widget.dart';
|
|
import 'package:flowy_editor/extensions/object_extensions.dart';
|
|
import 'package:flowy_editor/extensions/object_extensions.dart';
|
|
@@ -28,6 +28,10 @@ mixin FlowySelectionService<T extends StatefulWidget> on State<T> {
|
|
///
|
|
///
|
|
void clearSelection();
|
|
void clearSelection();
|
|
|
|
|
|
|
|
+ List<Rect> rects();
|
|
|
|
+
|
|
|
|
+ Position? hitTest(Offset? offset);
|
|
|
|
+
|
|
///
|
|
///
|
|
List<Node> getNodesInSelection(Selection selection);
|
|
List<Node> getNodesInSelection(Selection selection);
|
|
|
|
|
|
@@ -50,7 +54,7 @@ mixin FlowySelectionService<T extends StatefulWidget> on State<T> {
|
|
/// [start] is the offset under the global coordinate system.
|
|
/// [start] is the offset under the global coordinate system.
|
|
Node? computeNodeInOffset(Node node, Offset offset);
|
|
Node? computeNodeInOffset(Node node, Offset offset);
|
|
|
|
|
|
- /// Return the [Node]s in multiple selection. Emtpy list would be returned
|
|
|
|
|
|
+ /// Return the [Node]s in multiple selection. Empty list would be returned
|
|
/// if no nodes are in range.
|
|
/// if no nodes are in range.
|
|
///
|
|
///
|
|
/// [start] is the offset under the global coordinate system.
|
|
/// [start] is the offset under the global coordinate system.
|
|
@@ -110,6 +114,8 @@ class _FlowySelectionState extends State<FlowySelection>
|
|
/// Tap
|
|
/// Tap
|
|
Offset? tapOffset;
|
|
Offset? tapOffset;
|
|
|
|
|
|
|
|
+ final List<Rect> _rects = [];
|
|
|
|
+
|
|
EditorState get editorState => widget.editorState;
|
|
EditorState get editorState => widget.editorState;
|
|
|
|
|
|
@override
|
|
@override
|
|
@@ -171,8 +177,13 @@ class _FlowySelectionState extends State<FlowySelection>
|
|
);
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ List<Rect> rects() {
|
|
|
|
+ return _rects;
|
|
|
|
+ }
|
|
|
|
+
|
|
@override
|
|
@override
|
|
void updateSelection(Selection selection) {
|
|
void updateSelection(Selection selection) {
|
|
|
|
+ _rects.clear();
|
|
_clearSelection();
|
|
_clearSelection();
|
|
|
|
|
|
// cursor
|
|
// cursor
|
|
@@ -274,16 +285,32 @@ class _FlowySelectionState extends State<FlowySelection>
|
|
|
|
|
|
tapOffset = details.globalPosition;
|
|
tapOffset = details.globalPosition;
|
|
|
|
|
|
- final nodes = getNodesInRange(tapOffset!);
|
|
|
|
- if (nodes.isNotEmpty) {
|
|
|
|
- assert(nodes.length == 1);
|
|
|
|
- final selectable = nodes.first.selectable;
|
|
|
|
- if (selectable != null) {
|
|
|
|
- final position = selectable.getPositionInOffset(tapOffset!);
|
|
|
|
- final selection = Selection.collapsed(position);
|
|
|
|
- updateSelection(selection);
|
|
|
|
- }
|
|
|
|
|
|
+ final position = hitTest(tapOffset);
|
|
|
|
+ if (position == null) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ final selection = Selection.collapsed(position);
|
|
|
|
+ editorState.updateCursorSelection(selection);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @override
|
|
|
|
+ Position? hitTest(Offset? offset) {
|
|
|
|
+ if (offset == null) {
|
|
|
|
+ editorState.updateCursorSelection(null);
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ final nodes = getNodesInRange(offset);
|
|
|
|
+ if (nodes.isEmpty) {
|
|
|
|
+ editorState.updateCursorSelection(null);
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ assert(nodes.length == 1);
|
|
|
|
+ final selectable = nodes.first.selectable;
|
|
|
|
+ if (selectable == null) {
|
|
|
|
+ editorState.updateCursorSelection(null);
|
|
|
|
+ return null;
|
|
}
|
|
}
|
|
|
|
+ return selectable.getPositionInOffset(offset);
|
|
}
|
|
}
|
|
|
|
|
|
void _onPanStart(DragStartDetails details) {
|
|
void _onPanStart(DragStartDetails details) {
|
|
@@ -314,7 +341,7 @@ class _FlowySelectionState extends State<FlowySelection>
|
|
final selection = Selection(
|
|
final selection = Selection(
|
|
start: isDownward ? start : end, end: isDownward ? end : start);
|
|
start: isDownward ? start : end, end: isDownward ? end : start);
|
|
debugPrint('[_onPanUpdate] $selection');
|
|
debugPrint('[_onPanUpdate] $selection');
|
|
- updateSelection(selection);
|
|
|
|
|
|
+ editorState.updateCursorSelection(selection);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -385,6 +412,7 @@ class _FlowySelectionState extends State<FlowySelection>
|
|
final rects = selectable.getRectsInSelection(newSelection);
|
|
final rects = selectable.getRectsInSelection(newSelection);
|
|
|
|
|
|
for (final rect in rects) {
|
|
for (final rect in rects) {
|
|
|
|
+ _rects.add(_transformRectToGlobal(selectable, rect));
|
|
final overlay = OverlayEntry(
|
|
final overlay = OverlayEntry(
|
|
builder: ((context) => SelectionWidget(
|
|
builder: ((context) => SelectionWidget(
|
|
color: widget.selectionColor,
|
|
color: widget.selectionColor,
|
|
@@ -399,6 +427,11 @@ class _FlowySelectionState extends State<FlowySelection>
|
|
Overlay.of(context)?.insertAll(_selectionOverlays);
|
|
Overlay.of(context)?.insertAll(_selectionOverlays);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ Rect _transformRectToGlobal(Selectable selectable, Rect r) {
|
|
|
|
+ final Offset topLeft = selectable.localToGlobal(Offset(r.left, r.top));
|
|
|
|
+ return Rect.fromLTWH(topLeft.dx, topLeft.dy, r.width, r.height);
|
|
|
|
+ }
|
|
|
|
+
|
|
void _updateCursor(Position position) {
|
|
void _updateCursor(Position position) {
|
|
final node = editorState.document.root.childAtPath(position.path);
|
|
final node = editorState.document.root.childAtPath(position.path);
|
|
|
|
|
|
@@ -413,6 +446,7 @@ class _FlowySelectionState extends State<FlowySelection>
|
|
final selectable = node.selectable;
|
|
final selectable = node.selectable;
|
|
final rect = selectable?.getCursorRectInPosition(position);
|
|
final rect = selectable?.getCursorRectInPosition(position);
|
|
if (rect != null) {
|
|
if (rect != null) {
|
|
|
|
+ _rects.add(_transformRectToGlobal(selectable!, rect));
|
|
final cursor = OverlayEntry(
|
|
final cursor = OverlayEntry(
|
|
builder: ((context) => CursorWidget(
|
|
builder: ((context) => CursorWidget(
|
|
key: _cursorKey,
|
|
key: _cursorKey,
|
|
@@ -423,9 +457,15 @@ class _FlowySelectionState extends State<FlowySelection>
|
|
);
|
|
);
|
|
_cursorOverlays.add(cursor);
|
|
_cursorOverlays.add(cursor);
|
|
Overlay.of(context)?.insertAll(_cursorOverlays);
|
|
Overlay.of(context)?.insertAll(_cursorOverlays);
|
|
|
|
+ _forceShowCursor();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ _forceShowCursor() {
|
|
|
|
+ final currentState = _cursorKey.currentState as CursorWidgetState?;
|
|
|
|
+ currentState?.show();
|
|
|
|
+ }
|
|
|
|
+
|
|
List<Node> _selectedNodesInSelection(Node node, Selection selection) {
|
|
List<Node> _selectedNodesInSelection(Node node, Selection selection) {
|
|
List<Node> result = [];
|
|
List<Node> result = [];
|
|
if (node.parent != null) {
|
|
if (node.parent != null) {
|