Skip to content

Commit

Permalink
fix: incorrect cursor position after new line (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 authored Jun 11, 2024
1 parent 6ae9159 commit 3cd2aae
Showing 1 changed file with 43 additions and 54 deletions.
97 changes: 43 additions & 54 deletions lib/src/editor/block_component/rich_text/appflowy_rich_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,61 +183,42 @@ class _AppFlowyRichTextState extends State<AppFlowyRichText>
return null;
}

final selection = Selection(
start: position.copyWith(
offset: position.offset == 0 ? 0 : position.offset - 1,
),
end: position.copyWith(
offset: position.offset == 0 ? 1 : position.offset,
),
);

final rects = getRectsInSelection(selection);
var selectionRect = rects.firstOrNull;
var cursorHeight = selectionRect?.height;
var cursorOffset = _getCursorOffset(position, selectionRect);
if (cursorOffset == null ||
cursorHeight == null ||
selectionRect?.width == 0) {
selectionRect =
getRectsInSelection(selection, paragraph: _placeholderRenderParagraph)
.firstOrNull;
cursorHeight = selectionRect?.height;
cursorOffset = _getCursorOffset(position, selectionRect);
final textPosition = TextPosition(offset: position.offset);
var cursorHeight = _renderParagraph?.getFullHeightForCaret(textPosition);
var cursorOffset =
_renderParagraph?.getOffsetForCaret(textPosition, Rect.zero) ??
Offset.zero;
if (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 (cursorOffset != null &&
cursorHeight != null &&
widget.cursorHeight != null) {
cursorOffset = _adjustCursorOffset(cursorOffset, cursorHeight);
if (widget.cursorHeight != null && cursorHeight != null) {
cursorOffset = Offset(
cursorOffset.dx,
cursorOffset.dy + (cursorHeight - widget.cursorHeight!) / 2,
);
cursorHeight = widget.cursorHeight;
}

return _getCursorRect(cursorOffset, cursorHeight);
}

Offset? _getCursorOffset(Position position, Rect? selectionRect) {
return position.offset == 0
? selectionRect?.topLeft
: selectionRect?.topRight;
}

Offset _adjustCursorOffset(Offset cursorOffset, double cursorHeight) {
return Offset(
cursorOffset.dx,
cursorOffset.dy + (cursorHeight - widget.cursorHeight!) / 2,
);
}

Rect _getCursorRect(Offset? cursorOffset, double? cursorHeight) {
if (cursorOffset == null || cursorHeight == null) {
return Rect.zero;
}
return Rect.fromLTWH(
final rect = Rect.fromLTWH(
max(0, cursorOffset.dx - (widget.cursorWidth / 2.0)),
cursorOffset.dy,
widget.cursorWidth,
cursorHeight,
cursorHeight ?? 16.0,
);
return rect;
}

@override
Expand Down Expand Up @@ -370,12 +351,15 @@ class _AppFlowyRichTextState extends State<AppFlowyRichText>
textDirection: textDirection(),
textScaler:
TextScaler.linear(widget.editorState.editorStyle.textScaleFactor),
strutStyle: StrutStyle(
height: textStyleConfiguration.lineHeight,
),
);
}

Widget _buildRichText(BuildContext context) {
final textInserts = widget.node.delta!.whereType<TextInsert>();
TextSpan textSpan = getTextSpan(textInserts: textInserts);
var textSpan = getTextSpan(textInserts: textInserts);
return RichText(
key: textKey,
textAlign: widget.textAlign ?? TextAlign.start,
Expand All @@ -392,12 +376,15 @@ class _AppFlowyRichTextState extends State<AppFlowyRichText>
textDirection: textDirection(),
textScaler:
TextScaler.linear(widget.editorState.editorStyle.textScaleFactor),
strutStyle: StrutStyle(
height: textStyleConfiguration.lineHeight,
),
);
}

Widget _buildAutoCompleteRichText() {
final textInserts = widget.node.delta!.whereType<TextInsert>();
TextSpan textSpan = getTextSpan(textInserts: textInserts);
var textSpan = getTextSpan(textInserts: textInserts);
return ValueListenableBuilder(
valueListenable: widget.editorState.selectionNotifier,
builder: (_, __, ___) {
Expand Down Expand Up @@ -442,6 +429,9 @@ class _AppFlowyRichTextState extends State<AppFlowyRichText>
textDirection: textDirection(),
textScaler:
TextScaler.linear(widget.editorState.editorStyle.textScaleFactor),
strutStyle: StrutStyle(
height: textStyleConfiguration.lineHeight,
),
);
},
);
Expand All @@ -453,7 +443,7 @@ class _AppFlowyRichTextState extends State<AppFlowyRichText>
TextSpan(
text: widget.placeholderText,
style: textStyleConfiguration.text.copyWith(
height: widget.lineHeight,
height: textStyleConfiguration.lineHeight,
),
),
],
Expand Down Expand Up @@ -541,9 +531,8 @@ class _AppFlowyRichTextState extends State<AppFlowyRichText>
);
offset += textInsert.length;
}
return TextSpan(
children: textSpans,
);

return TextSpan(children: textSpans);
}

TextSelection? textSelectionFromEditorSelection(Selection? selection) {
Expand Down

0 comments on commit 3cd2aae

Please sign in to comment.