From 2045d45e882e7b830b41a2e92aa0669ec13c2070 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:47:01 +0700 Subject: [PATCH] =?UTF-8?q?[Ho=C3=A0ng=20Sang]=20Fix=20some=20bugs=20(#137?= =?UTF-8?q?2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/src/models/documents/document.dart | 14 +++++++++++++- lib/src/widgets/controller.dart | 8 ++++++-- lib/src/widgets/editor.dart | 10 +++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/src/models/documents/document.dart b/lib/src/models/documents/document.dart index b62299d1..6266b42b 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 isLinkStyle = + res.node?.style.attributes[Attribute.link.key]?.value == true; + // 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 && !isLinkStyle + ? 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..16e919e8 100644 --- a/lib/src/widgets/controller.dart +++ b/lib/src/widgets/controller.dart @@ -123,7 +123,9 @@ class QuillController extends ChangeNotifier { return; } if (isIncrease) { - formatSelection(Attribute.getIndentLevel(indent.value + 1)); + if (indent.value < 5) { + formatSelection(Attribute.getIndentLevel(indent.value + 1)); + } return; } formatSelection(Attribute.getIndentLevel(indent.value - 1)); @@ -150,7 +152,9 @@ class QuillController extends ChangeNotifier { } else if (indent.value == 1 && !isIncrease) { formatAttribute = Attribute.clone(Attribute.indentL1, null); } else if (isIncrease) { - formatAttribute = Attribute.getIndentLevel(indent.value + 1); + if (indent.value < 5) { + formatAttribute = Attribute.getIndentLevel(indent.value + 1); + } } else { formatAttribute = Attribute.getIndentLevel(indent.value - 1); } 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);