From 14b6f12f39696b1f51c2834a045f20f5ade1d8af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ph=E1=BA=A1m=20Ho=C3=A0ng=20Sang?= Date: Fri, 25 Aug 2023 14:13:18 +0700 Subject: [PATCH] Fixbug Indent, style status of button --- lib/src/models/documents/document.dart | 14 +++++++++++++- lib/src/widgets/controller.dart | 7 +++++-- lib/src/widgets/editor.dart | 10 +++++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/src/models/documents/document.dart b/lib/src/models/documents/document.dart index b62299d1..f89a7a19 100644 --- a/lib/src/models/documents/document.dart +++ b/lib/src/models/documents/document.dart @@ -156,7 +156,19 @@ class Document { /// included in the result. Style collectStyle(int index, int len) { final res = queryChild(index); - return (res.node as Line).collectStyle(res.offset, len); + // -1 because the cursor is at the part of the line that is not visible + // Bug: When the caret is in the middle of the paragraph + // and at the end of the format string, it will display the wrong state + // of the format button + final isNotLinkStyle = + res.node?.style.attributes[Attribute.link.key]?.value == false; + // In this case, we have an exception, this is a link. + // When node is a link we will not -1 + return (res.node as Line).collectStyle( + len == 0 && res.node != null && isNotLinkStyle + ? res.offset - 1 + : res.offset, + len); } /// Returns all styles and Embed for each node within selection diff --git a/lib/src/widgets/controller.dart b/lib/src/widgets/controller.dart index a3732449..2826e3f6 100644 --- a/lib/src/widgets/controller.dart +++ b/lib/src/widgets/controller.dart @@ -122,11 +122,14 @@ class QuillController extends ChangeNotifier { formatSelection(Attribute.clone(Attribute.indentL1, null)); return; } - if (isIncrease) { + if (isIncrease && indent.value < 5) { formatSelection(Attribute.getIndentLevel(indent.value + 1)); return; } - formatSelection(Attribute.getIndentLevel(indent.value - 1)); + if (indent.value > 0) { + formatSelection(Attribute.getIndentLevel(indent.value - 1)); + return; + } } void _indentSelectionEachLine(bool isIncrease) { diff --git a/lib/src/widgets/editor.dart b/lib/src/widgets/editor.dart index fb1e495f..896b6ae4 100644 --- a/lib/src/widgets/editor.dart +++ b/lib/src/widgets/editor.dart @@ -192,6 +192,7 @@ class QuillEditor extends StatefulWidget { this.dialogTheme, this.contentInsertionConfiguration, this.contextMenuBuilder, + this.editorKey, Key? key, }) : super(key: key); @@ -205,6 +206,7 @@ class QuillEditor extends StatefulWidget { bool expands = false, FocusNode? focusNode, String? placeholder, + GlobalKey? editorKey, /// The locale to use for the editor toolbar, defaults to system locale /// More at https://github.com/singerdmx/flutter-quill#translation @@ -223,6 +225,7 @@ class QuillEditor extends StatefulWidget { locale: locale, embedBuilders: embedBuilders, placeholder: placeholder, + editorKey: editorKey, ); } @@ -448,19 +451,24 @@ class QuillEditor extends StatefulWidget { /// See [https://api.flutter.dev/flutter/widgets/EditableText/contentInsertionConfiguration.html] final ContentInsertionConfiguration? contentInsertionConfiguration; + /// Using the editorKey for get getLocalRectForCaret + /// editorKey.currentState?.renderEditor.getLocalRectForCaret + final GlobalKey? editorKey; + @override QuillEditorState createState() => QuillEditorState(); } class QuillEditorState extends State implements EditorTextSelectionGestureDetectorBuilderDelegate { - final GlobalKey _editorKey = GlobalKey(); + late GlobalKey _editorKey; late EditorTextSelectionGestureDetectorBuilder _selectionGestureDetectorBuilder; @override void initState() { super.initState(); + _editorKey = widget.editorKey ?? GlobalKey(); _selectionGestureDetectorBuilder = _QuillEditorSelectionGestureDetectorBuilder( this, widget.detectWordBoundary);