Skip to content

Commit

Permalink
fix: cursor height is inconsistent when the font size is smaller than…
Browse files Browse the repository at this point in the history
… the default text size (#958)

* fix: cursor height is inconsistent when the font size is smaller than the default text size

* fix: widget tests
  • Loading branch information
LucasXu0 authored Nov 1, 2024
1 parent 4081fb7 commit eccb244
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
75 changes: 44 additions & 31 deletions lib/src/editor/block_component/rich_text/appflowy_rich_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,12 @@ class _AppFlowyRichTextState extends State<AppFlowyRichText>

@override
Widget build(BuildContext context) {
Widget child = _buildRichText(context);

final delta = widget.node.delta;
if (delta == null || delta.isEmpty) {
child = Stack(
children: [
_buildPlaceholderText(context),
_buildRichText(context),
],
);
}
Widget child = Stack(
children: [
_buildPlaceholderText(context),
_buildRichText(context),
],
);

if (enableAutoComplete) {
final autoCompleteText = _buildAutoCompleteRichText();
Expand Down Expand Up @@ -184,27 +179,37 @@ class _AppFlowyRichTextState extends State<AppFlowyRichText>
}

final textPosition = TextPosition(offset: position.offset);
var cursorHeight = _renderParagraph?.getFullHeightForCaret(textPosition);
var cursorOffset =
_renderParagraph?.getOffsetForCaret(textPosition, Rect.zero) ??
double? placeholderCursorHeight =
_placeholderRenderParagraph?.getFullHeightForCaret(textPosition);
Offset? placeholderCursorOffset =
_placeholderRenderParagraph?.getOffsetForCaret(
textPosition,
Rect.zero,
) ??
Offset.zero;
if (delta?.isEmpty == true || cursorHeight == null || cursorHeight == 0) {
cursorHeight =
_placeholderRenderParagraph?.getFullHeightForCaret(textPosition);
cursorOffset = _placeholderRenderParagraph?.getOffsetForCaret(
textPosition,
Rect.zero,
) ??
Offset.zero;
if (textDirection() == TextDirection.rtl) {
if (widget.placeholderText.trim().isNotEmpty) {
cursorOffset = cursorOffset.translate(
_placeholderRenderParagraph?.size.width ?? 0,
0,
);
}
if (textDirection() == TextDirection.rtl) {
if (widget.placeholderText.trim().isNotEmpty) {
placeholderCursorOffset = placeholderCursorOffset.translate(
_placeholderRenderParagraph?.size.width ?? 0,
0,
);
}
}

double? cursorHeight =
_renderParagraph?.getFullHeightForCaret(textPosition);
Offset? cursorOffset =
_renderParagraph?.getOffsetForCaret(textPosition, Rect.zero) ??
Offset.zero;

if (placeholderCursorHeight != null) {
cursorHeight = max(cursorHeight ?? 0, placeholderCursorHeight);
}

if (delta?.isEmpty == true) {
cursorOffset = placeholderCursorOffset;
}

if (widget.cursorHeight != null && cursorHeight != null) {
cursorOffset = Offset(
cursorOffset.dx,
Expand Down Expand Up @@ -340,6 +345,12 @@ class _AppFlowyRichTextState extends State<AppFlowyRichText>
textSpan = widget.placeholderTextSpanDecorator!(textSpan);
}
textSpan = adjustTextSpan(textSpan);
final delta = widget.node.delta;
if (delta != null && delta.isNotEmpty) {
textSpan = textSpan.updateTextStyle(
const TextStyle(color: Colors.transparent),
);
}
return RichText(
key: placeholderTextKey,
textHeightBehavior: TextHeightBehavior(
Expand All @@ -351,8 +362,10 @@ class _AppFlowyRichTextState extends State<AppFlowyRichText>
),
text: textSpan,
textDirection: textDirection(),
textScaler:
TextScaler.linear(widget.editorState.editorStyle.textScaleFactor),
textScaler: TextScaler.linear(
widget.editorState.editorStyle.textScaleFactor,
),
overflow: TextOverflow.ellipsis,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ void main() async {
});

group('table_view.dart', () {
// TODO(zoli)
// testWidgets('resize column', (tester) async {});

testWidgets('row height changing base on cell height', (tester) async {
final tableNode = TableNode.fromList([
['', ''],
Expand Down

0 comments on commit eccb244

Please sign in to comment.