mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
Merge pull request #763 from AppFlowy-IO/refactor/use-node-iterator-for-selection
Refactor: use node iterator for selection
This commit is contained in:
commit
60072c5ae2
@ -61,4 +61,14 @@ class NodeIterator implements Iterator<Node> {
|
|||||||
Node get current {
|
Node get current {
|
||||||
return _currentNode!;
|
return _currentNode!;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<Node> toList() {
|
||||||
|
final result = <Node>[];
|
||||||
|
|
||||||
|
while (moveNext()) {
|
||||||
|
result.add(current);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flowy_editor/document/node_iterator.dart';
|
||||||
|
import 'package:flowy_editor/document/state_tree.dart';
|
||||||
import 'package:flutter/gestures.dart';
|
import 'package:flutter/gestures.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
@ -212,7 +214,7 @@ class _FlowySelectionState extends State<FlowySelection>
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
List<Node> getNodesInSelection(Selection selection) =>
|
List<Node> getNodesInSelection(Selection selection) =>
|
||||||
_selectedNodesInSelection(editorState.document.root, selection);
|
_selectedNodesInSelection(editorState.document, selection);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@ -462,8 +464,7 @@ class _FlowySelectionState extends State<FlowySelection>
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _updateSelection(Selection selection) {
|
void _updateSelection(Selection selection) {
|
||||||
final nodes =
|
final nodes = _selectedNodesInSelection(editorState.document, selection);
|
||||||
_selectedNodesInSelection(editorState.document.root, selection);
|
|
||||||
|
|
||||||
currentSelection = selection;
|
currentSelection = selection;
|
||||||
currentSelectedNodes.value = nodes;
|
currentSelectedNodes.value = nodes;
|
||||||
@ -572,16 +573,10 @@ class _FlowySelectionState extends State<FlowySelection>
|
|||||||
currentState?.show();
|
currentState?.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Node> _selectedNodesInSelection(Node node, Selection selection) {
|
List<Node> _selectedNodesInSelection(
|
||||||
List<Node> result = [];
|
StateTree stateTree, Selection selection) {
|
||||||
if (node.parent != null) {
|
final startNode = stateTree.nodeAtPath(selection.start.path)!;
|
||||||
if (node.inSelection(selection)) {
|
final endNode = stateTree.nodeAtPath(selection.end.path)!;
|
||||||
result.add(node);
|
return NodeIterator(stateTree, startNode, endNode).toList();
|
||||||
}
|
|
||||||
}
|
|
||||||
for (final child in node.children) {
|
|
||||||
result.addAll(_selectedNodesInSelection(child, selection));
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user