From 15e6bfd0181695644bbce7ee0c2bb0caa196eccb Mon Sep 17 00:00:00 2001 From: Alspb <> Date: Mon, 2 Oct 2023 00:45:16 +0100 Subject: [PATCH 1/2] Fixed style recognition when caret is at the beginning/end of style --- lib/src/models/documents/document.dart | 35 ++++++++++++++++---------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/lib/src/models/documents/document.dart b/lib/src/models/documents/document.dart index 6266b42b0..55617865b 100644 --- a/lib/src/models/documents/document.dart +++ b/lib/src/models/documents/document.dart @@ -156,19 +156,28 @@ class Document { /// included in the result. Style collectStyle(int index, int len) { final res = queryChild(index); - // -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); + Style rangeStyle; + if (len > 0) { + return (res.node as Line).collectStyle(res.offset, len); + } + if (res.offset == 0) { + rangeStyle = (res.node as Line).collectStyle(res.offset, len); + return rangeStyle.removeAll({ + for (final attr in rangeStyle.values) + if (attr.isInline) attr + }); + } + rangeStyle = (res.node as Line).collectStyle(res.offset - 1, len); + final linkAttribute = rangeStyle.attributes[Attribute.link.key]; + if ((linkAttribute != null) && + (linkAttribute.value != + (res.node as Line) + .collectStyle(res.offset, len) + .attributes[Attribute.link.key] + ?.value)) { + return rangeStyle.removeAll({linkAttribute}); + } + return rangeStyle; } /// Returns all styles and Embed for each node within selection From 48835b90f8479c67e60d797e1862e74fc29c0c22 Mon Sep 17 00:00:00 2001 From: Alspb <> Date: Mon, 2 Oct 2023 00:47:59 +0100 Subject: [PATCH 2/2] Fixed range style: symmetric difference is replaced with intersection of each individual segment styles --- lib/src/models/documents/nodes/line.dart | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/src/models/documents/nodes/line.dart b/lib/src/models/documents/nodes/line.dart index b9e7191d5..539f3c5d1 100644 --- a/lib/src/models/documents/nodes/line.dart +++ b/lib/src/models/documents/nodes/line.dart @@ -352,18 +352,13 @@ class Line extends Container { final excluded = {}; void _handle(Style style) { - if (result.isEmpty) { - excluded.addAll(style.values); - } else { - for (final attr in result.values) { - if (!style.containsKey(attr.key)) { - excluded.add(attr); - } + for (final attr in result.values) { + if (!style.containsKey(attr.key) || + (style.attributes[attr.key] != attr.value)) { + excluded.add(attr); } } - final remaining = style.removeAll(excluded); result = result.removeAll(excluded); - result = result.mergeAll(remaining); } final data = queryChild(offset, true);