fix: selection move direction logic should visit children (#1156)

This commit is contained in:
Richard Shiue 2025-11-11 11:57:28 +08:00 • committed by GitHub
commit aacec0801e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 38 additions and 1 deletions

View file

@ -260,7 +260,10 @@ extension SelectionTransform on EditorState {
else if (direction == SelectionMoveDirection.backward &&
end != null &&
end.offset <= offset) {
final nextStart = node.next?.selectable?.start();
final nextStart = node
.nextNodeWhere((element) => element.selectable != null)
?.selectable
?.start();
if (nextStart != null) {
updateSelectionWithReason(
Selection.collapsed(nextStart),

View file

@ -38,6 +38,40 @@ extension NodeExtensions on Node {
return Rect.zero;
}
Node? nextNodeWhere(bool Function(Node element) test) {
for (final child in children) {
final matchingNode = child._thisOrDescendantMatching(test);
if (matchingNode != null) {
return matchingNode;
}
}
var next = this.next;
while (next != null) {
final nextDescendentMatch = next._thisOrDescendantMatching(test);
if (nextDescendentMatch != null) {
return nextDescendentMatch;
}
next = next.next;
}
return parent?.next?._thisOrDescendantMatching(test);
}
Node? _thisOrDescendantMatching(bool Function(Node element) test) {
if (test(this)) {
return this;
}
for (final child in children) {
final matchingNode = child._thisOrDescendantMatching(test);
if (matchingNode != null) {
return matchingNode;
}
}
return null;
}
/// Returns the first previous node in the subtree that satisfies the given predicate
Node? previousNodeWhere(bool Function(Node element) test) {
var previous = this.previous;