From 307b0a004cff994967fe266e2e58f7247e2b7ed9 Mon Sep 17 00:00:00 2001 From: xuyang Date: Wed, 17 Jul 2024 15:46:18 +0800 Subject: [PATCH] [feature] : empty line case exception --- lib/src/widgets/editor/editor.dart | 26 ++++++++++++++++--- lib/src/widgets/quill/text_line.dart | 4 +++ .../widgets/raw_editor/raw_editor_state.dart | 2 -- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/src/widgets/editor/editor.dart b/lib/src/widgets/editor/editor.dart index 82c67a4f..e74d2872 100644 --- a/lib/src/widgets/editor/editor.dart +++ b/lib/src/widgets/editor/editor.dart @@ -2,7 +2,8 @@ import 'dart:math' as math; import 'package:flutter/cupertino.dart' show CupertinoTheme, cupertinoTextSelectionControls; -import 'package:flutter/foundation.dart' show ValueListenable; +import 'package:flutter/foundation.dart' + show ValueListenable, defaultTargetPlatform; import 'package:flutter/gestures.dart' show PointerDeviceKind; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; @@ -559,7 +560,8 @@ class _QuillEditorSelectionGestureDetectorBuilder Feedback.forLongPress(_state.context); } } - editor?.showMagnifier(details.globalPosition); + + _showMagnifierIfSupportedByPlatform(details.globalPosition); } @override @@ -578,9 +580,27 @@ class _QuillEditorSelectionGestureDetectorBuilder } } } - editor?.hideMagnifier(); + _hideMagnifierIfSupportedByPlatform(); super.onSingleLongTapEnd(details); } + + void _showMagnifierIfSupportedByPlatform(Offset positionToShow) { + switch (defaultTargetPlatform) { + case TargetPlatform.android: + case TargetPlatform.iOS: + editor?.showMagnifier(positionToShow); + default: + } + } + + void _hideMagnifierIfSupportedByPlatform() { + switch (defaultTargetPlatform) { + case TargetPlatform.android: + case TargetPlatform.iOS: + editor?.hideMagnifier(); + default: + } + } } /// Signature for the callback that reports when the user changes the selection diff --git a/lib/src/widgets/quill/text_line.dart b/lib/src/widgets/quill/text_line.dart index e03e5ae8..553e5fc8 100644 --- a/lib/src/widgets/quill/text_line.dart +++ b/lib/src/widgets/quill/text_line.dart @@ -839,6 +839,10 @@ class RenderEditableTextLine extends RenderEditableBox { _getBoxes(TextSelection(baseOffset: 0, extentOffset: line.length - 1)) .where((element) => element.top < lineDy && element.bottom > lineDy) .toList(growable: false); + if (lineBoxes.isEmpty) { + // Empty line, line box is empty + return TextRange.collapsed(position.offset); + } return TextRange( start: getPositionForOffset( Offset(lineBoxes.first.left, lineDy), diff --git a/lib/src/widgets/raw_editor/raw_editor_state.dart b/lib/src/widgets/raw_editor/raw_editor_state.dart index 4d16145d..9920bf09 100644 --- a/lib/src/widgets/raw_editor/raw_editor_state.dart +++ b/lib/src/widgets/raw_editor/raw_editor_state.dart @@ -1753,8 +1753,6 @@ class QuillRawEditorState extends EditorState @override void showMagnifier(ui.Offset positionToShow) { - if (defaultTargetPlatform != TargetPlatform.iOS && - defaultTargetPlatform != TargetPlatform.android) return; if (_selectionOverlay == null) return; final position = renderEditor.getPositionForOffset(positionToShow); _selectionOverlay?.showMagnifier(position, positionToShow, renderEditor);