Skip to content

Commit

Permalink
Fix RenderEditableTextLine's getPositionAbove
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir-P committed Jan 5, 2024
1 parent e043d4f commit 7713fe5
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion packages/fleather/lib/src/rendering/editable_text_line.dart
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,24 @@ class RenderEditableTextLine extends RenderEditableBox {
return body!.getPositionForOffset(shiftedOffset);
}

// Computes the box height for the position.
// To get the correct box, it extends position backward if position
// is bigger than 0 and extends it forward if it's text position is 0.
double _getBoxHeightForPosition(TextPosition position) {

Check warning on line 288 in packages/fleather/lib/src/rendering/editable_text_line.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/rendering/editable_text_line.dart#L288

Added line #L288 was not covered by tests
final int baseOffset;
if (position.offset > 0) {
baseOffset = position.offset - 1;

Check warning on line 291 in packages/fleather/lib/src/rendering/editable_text_line.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/rendering/editable_text_line.dart#L290-L291

Added lines #L290 - L291 were not covered by tests
} else {
baseOffset = position.offset;

Check warning on line 293 in packages/fleather/lib/src/rendering/editable_text_line.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/rendering/editable_text_line.dart#L293

Added line #L293 was not covered by tests
}
final boxes = body!.getBoxesForSelection(TextSelection(

Check warning on line 295 in packages/fleather/lib/src/rendering/editable_text_line.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/rendering/editable_text_line.dart#L295

Added line #L295 was not covered by tests
baseOffset: baseOffset,
extentOffset: baseOffset + 1,
affinity: position.affinity,

Check warning on line 298 in packages/fleather/lib/src/rendering/editable_text_line.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/rendering/editable_text_line.dart#L297-L298

Added lines #L297 - L298 were not covered by tests
));
return boxes.firstOrNull?.toRect().height ?? 0;

Check warning on line 300 in packages/fleather/lib/src/rendering/editable_text_line.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/rendering/editable_text_line.dart#L300

Added line #L300 was not covered by tests
}

@override
TextPosition? getPositionAbove(TextPosition position) {
assert(position.offset < node.length);
Expand All @@ -291,7 +309,8 @@ class RenderEditableTextLine extends RenderEditableBox {
// the caret so the middle of the line above is a half line above that
// point.
final caretOffset = getOffsetForCaret(position);
final dy = -0.5 * preferredLineHeight(position);
final dy = -_getBoxHeightForPosition(position) +
0.5 * preferredLineHeight(position);

Check warning on line 313 in packages/fleather/lib/src/rendering/editable_text_line.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/rendering/editable_text_line.dart#L312-L313

Added lines #L312 - L313 were not covered by tests
final abovePositionOffset = caretOffset.translate(0, dy);
if (!body!.size.contains(abovePositionOffset - parentData.offset)) {
// We're outside of the body so there is no text above to check.
Expand Down

0 comments on commit 7713fe5

Please sign in to comment.