From 6751472a55b2cc7d1aade08e253259b44e1ffbe2 Mon Sep 17 00:00:00 2001 From: li3317 Date: Tue, 13 Jul 2021 23:19:43 +0800 Subject: [PATCH] fix clear format button --- lib/src/models/documents/document.dart | 8 +++-- lib/src/models/documents/nodes/line.dart | 33 +++++++++++++++++++ lib/src/widgets/controller.dart | 8 ++++- .../widgets/toolbar/clear_format_button.dart | 2 +- 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/lib/src/models/documents/document.dart b/lib/src/models/documents/document.dart index 346da93b..010df462 100644 --- a/lib/src/models/documents/document.dart +++ b/lib/src/models/documents/document.dart @@ -119,9 +119,13 @@ class Document { return delta; } - Style collectStyle(int index, int len) { + Style collectStyle(int index, int len, bool collectAll) { final res = queryChild(index); - return (res.node as Line).collectStyle(res.offset, len); + if (collectAll) { + return (res.node as Line).collectAllStyle(res.offset, len); + } else { + return (res.node as Line).collectStyle(res.offset, len); + } } ChildQuery queryChild(int offset) { diff --git a/lib/src/models/documents/nodes/line.dart b/lib/src/models/documents/nodes/line.dart index fabfad4d..a67a17da 100644 --- a/lib/src/models/documents/nodes/line.dart +++ b/lib/src/models/documents/nodes/line.dart @@ -368,4 +368,37 @@ class Line extends Container { return result; } + + /// Returns all style for any character within the specified text range. + Style collectAllStyle(int offset, int len) { + final local = math.min(length - offset, len); + var result = Style(); + + final data = queryChild(offset, true); + var node = data.node as Leaf?; + if (node != null) { + result = result.mergeAll(node.style); + var pos = node.length - data.offset; + while (!node!.isLast && pos < local) { + node = node.next as Leaf?; + result = result.mergeAll(node!.style); + pos += node.length; + } + } + + result = result.mergeAll(style); + if (parent is Block) { + final block = parent as Block; + result = result.mergeAll(block.style); + } + + final remaining = len - local; + if (remaining > 0) { + final rest = nextLine!.collectAllStyle(0, remaining); + result = result.mergeAll(rest); + } + + return result; + } + } diff --git a/lib/src/widgets/controller.dart b/lib/src/widgets/controller.dart index 7de1a122..9e2e01b0 100644 --- a/lib/src/widgets/controller.dart +++ b/lib/src/widgets/controller.dart @@ -57,7 +57,13 @@ class QuillController extends ChangeNotifier { Style getSelectionStyle() { return document - .collectStyle(selection.start, selection.end - selection.start) + .collectStyle(selection.start, selection.end - selection.start, false) + .mergeAll(toggledStyle); + } + + Style getAllSelectionStyle() { + return document + .collectStyle(selection.start, selection.end - selection.start, true) .mergeAll(toggledStyle); } diff --git a/lib/src/widgets/toolbar/clear_format_button.dart b/lib/src/widgets/toolbar/clear_format_button.dart index d55c21df..ee049b41 100644 --- a/lib/src/widgets/toolbar/clear_format_button.dart +++ b/lib/src/widgets/toolbar/clear_format_button.dart @@ -34,7 +34,7 @@ class _ClearFormatButtonState extends State { fillColor: fillColor, onPressed: () { for (final k - in widget.controller.getSelectionStyle().attributes.values) { + in widget.controller.getAllSelectionStyle().attributes.values) { widget.controller.formatSelection(Attribute.clone(k, null)); } });