From 55cb35d9e77868c6a53b9f1b35055dce2c56752c Mon Sep 17 00:00:00 2001 From: Douglas Ward Date: Wed, 24 Apr 2024 11:38:03 -0600 Subject: [PATCH] Fix: collectAllIndividualStylesAndEmbed for result span --- lib/src/models/documents/nodes/line.dart | 60 +++++++-------------- lib/src/widgets/quill/quill_controller.dart | 2 +- 2 files changed, 21 insertions(+), 41 deletions(-) diff --git a/lib/src/models/documents/nodes/line.dart b/lib/src/models/documents/nodes/line.dart index 86f2b499..935babda 100644 --- a/lib/src/models/documents/nodes/line.dart +++ b/lib/src/models/documents/nodes/line.dart @@ -43,9 +43,7 @@ base class Line extends QuillContainer { if (parent!.isLast) { return null; } - return parent!.next is Block - ? (parent!.next as Block).first as Line? - : parent!.next as Line?; + return parent!.next is Block ? (parent!.next as Block).first as Line? : parent!.next as Line?; } @override @@ -53,9 +51,7 @@ base class Line extends QuillContainer { @override Delta toDelta() { - final delta = children - .map((child) => child.toDelta()) - .fold(Delta(), (dynamic a, b) => a.concat(b)); + final delta = children.map((child) => child.toDelta()).fold(Delta(), (dynamic a, b) => a.concat(b)); var attributes = style; if (parent is Block) { final block = parent as Block; @@ -134,17 +130,11 @@ base class Line extends QuillContainer { final isLineFormat = (index + local == thisLength) && local == 1; if (isLineFormat) { - assert( - style.values.every((attr) => - attr.scope == AttributeScope.block || - attr.scope == AttributeScope.ignore), - 'It is not allowed to apply inline attributes to line itself.'); + assert(style.values.every((attr) => attr.scope == AttributeScope.block || attr.scope == AttributeScope.ignore), 'It is not allowed to apply inline attributes to line itself.'); _format(style); } else { // Otherwise forward to children as it's an inline format update. - assert(style.values.every((attr) => - attr.scope == AttributeScope.inline || - attr.scope == AttributeScope.ignore)); + assert(style.values.every((attr) => attr.scope == AttributeScope.inline || attr.scope == AttributeScope.ignore)); assert(index + local != thisLength); super.retain(index, local, style); } @@ -215,21 +205,15 @@ base class Line extends QuillContainer { // Ensure that we're only unwrapping the block only if we unset a single // block format in the `parentStyle` and there are no more block formats // left to unset. - if (blockStyle.value == null && - parentStyle.containsKey(blockStyle.key) && - parentStyle.length == 1) { + if (blockStyle.value == null && parentStyle.containsKey(blockStyle.key) && parentStyle.length == 1) { _unwrap(); - } else if (!const MapEquality() - .equals(newStyle.getBlocksExceptHeader(), parentStyle)) { + } else if (!const MapEquality().equals(newStyle.getBlocksExceptHeader(), parentStyle)) { _unwrap(); // Block style now can contain multiple attributes - if (newStyle.attributes.keys - .any(Attribute.exclusiveBlockKeys.contains)) { - parentStyle.removeWhere( - (key, attr) => Attribute.exclusiveBlockKeys.contains(key)); + if (newStyle.attributes.keys.any(Attribute.exclusiveBlockKeys.contains)) { + parentStyle.removeWhere((key, attr) => Attribute.exclusiveBlockKeys.contains(key)); } - parentStyle.removeWhere( - (key, attr) => newStyle?.attributes.keys.contains(key) ?? false); + parentStyle.removeWhere((key, attr) => newStyle?.attributes.keys.contains(key) ?? false); final parentStyleToMerge = Style.attr(parentStyle); newStyle = newStyle.mergeAll(parentStyleToMerge); _applyBlockStyles(newStyle); @@ -361,8 +345,7 @@ base class Line extends QuillContainer { void handle(Style style) { for (final attr in result.values) { - if (!style.containsKey(attr.key) || - (style.attributes[attr.key]?.value != attr.value)) { + if (!style.containsKey(attr.key) || (style.attributes[attr.key]?.value != attr.value)) { excluded.add(attr); } } @@ -399,28 +382,27 @@ base class Line extends QuillContainer { /// Returns each node segment's offset in selection /// with its corresponding style or embed as a list - List collectAllIndividualStylesAndEmbed(int offset, int len, - {int beg = 0}) { + List collectAllIndividualStylesAndEmbed(int offset, int len, {int beg = 0}) { final local = math.min(length - offset, len); final result = []; final data = queryChild(offset, true); var node = data.node as Leaf?; if (node != null) { - var pos = 0; - pos = node.length - data.offset; + var pos = math.min(local, node.length - data.offset); if (node is QuillText && node.style.isNotEmpty) { - result.add(OffsetValue(beg, node.style, node.length)); + result.add(OffsetValue(beg, node.style, pos)); } else if (node.value is Embeddable) { - result.add(OffsetValue(beg, node.value as Embeddable, node.length)); + result.add(OffsetValue(beg, node.value as Embeddable, pos)); } + while (!node!.isLast && pos < local) { node = node.next as Leaf; + final span = math.min(local - pos, node.length); if (node is QuillText && node.style.isNotEmpty) { - result.add(OffsetValue(pos + beg, node.style, node.length)); + result.add(OffsetValue(pos + beg, node.style, span)); } else if (node.value is Embeddable) { - result.add( - OffsetValue(pos + beg, node.value as Embeddable, node.length)); + result.add(OffsetValue(pos + beg, node.value as Embeddable, span)); } pos += node.length; } @@ -432,8 +414,7 @@ base class Line extends QuillContainer { final remaining = len - local; if (remaining > 0 && nextLine != null) { - final rest = nextLine! - .collectAllIndividualStylesAndEmbed(0, remaining, beg: local + beg); + final rest = nextLine!.collectAllIndividualStylesAndEmbed(0, remaining, beg: local + beg); result.addAll(rest); } @@ -503,8 +484,7 @@ base class Line extends QuillContainer { final remaining = len - local; if (remaining > 0 && nextLine != null) { - final rest = - nextLine!.collectAllStylesWithOffsets(0, remaining, beg: local); + final rest = nextLine!.collectAllStylesWithOffsets(0, remaining, beg: local); result.addAll(rest); } diff --git a/lib/src/widgets/quill/quill_controller.dart b/lib/src/widgets/quill/quill_controller.dart index 4f07f139..56bbcd29 100644 --- a/lib/src/widgets/quill/quill_controller.dart +++ b/lib/src/widgets/quill/quill_controller.dart @@ -485,7 +485,7 @@ class QuillController extends ChangeNotifier { final sel = selection; replaceText(sel.start, sel.end - sel.start, '', TextSelection.collapsed(offset: sel.start)); } - return true; + return true; } return false; }