Skip to content

Commit f098c54

Browse files
committed
feat: #953 improve arrow keys handler
1 parent b61e3f4 commit f098c54

File tree

11 files changed

+698
-120
lines changed

11 files changed

+698
-120
lines changed

frontend/app_flowy/packages/appflowy_editor/lib/src/document/selection.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ class Selection {
4646
(start.path <= end.path && !pathEquals(start.path, end.path)) ||
4747
(isSingle && start.offset < end.offset);
4848

49-
Selection normalize() {
49+
Selection get normalize {
5050
if (isForward) {
51-
return Selection(start: end, end: start);
51+
return reversed;
5252
}
5353
return this;
5454
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ mixin DefaultSelectable {
3232
Selection getSelectionInRange(Offset start, Offset end) =>
3333
forward.getSelectionInRange(start, end);
3434

35-
Offset localToGlobal(Offset offset) => forward.localToGlobal(offset);
35+
Offset localToGlobal(Offset offset) =>
36+
forward.localToGlobal(offset) - baseOffset;
3637

3738
Selection? getWorldBoundaryInOffset(Offset offset) =>
3839
forward.getWorldBoundaryInOffset(offset);

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,24 @@ class _FlowyRichTextState extends State<FlowyRichText> with Selectable {
132132

133133
@override
134134
List<Rect> getRectsInSelection(Selection selection) {
135-
assert(pathEquals(selection.start.path, selection.end.path) &&
135+
assert(selection.isSingle &&
136136
pathEquals(selection.start.path, widget.textNode.path));
137137

138138
final textSelection = TextSelection(
139139
baseOffset: selection.start.offset,
140140
extentOffset: selection.end.offset,
141141
);
142-
return _renderParagraph
142+
final rects = _renderParagraph
143143
.getBoxesForSelection(textSelection, boxHeightStyle: BoxHeightStyle.max)
144144
.map((box) => box.toRect())
145-
.toList();
145+
.toList(growable: false);
146+
if (rects.isEmpty) {
147+
// If the rich text widget does not contain any text,
148+
// there will be no selection boxes,
149+
// so we need to return to the default selection.
150+
return [Rect.fromLTWH(0, 0, 0, _renderParagraph.size.height)];
151+
}
152+
return rects;
146153
}
147154

148155
@override

0 commit comments

Comments
 (0)