diff --git a/lib/src/models/documents/nodes/line.dart b/lib/src/models/documents/nodes/line.dart index b8d25edc..39ad970e 100644 --- a/lib/src/models/documents/nodes/line.dart +++ b/lib/src/models/documents/nodes/line.dart @@ -384,29 +384,29 @@ class Line extends Container { } /// Returns each offset in selection with its corresponding styles as a list - List> collectAllIndividualStyles(int offset, int len) { + List> collectAllIndividualStyles(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) { - result.add(Tuple2(0, node.style)); - var pos = node.length - data.offset; // 2 + result.add(Tuple2(beg, node.style)); + var pos = node.length - data.offset; while (!node!.isLast && pos < local) { node = node.next as Leaf; - result.add(Tuple2(pos, node.style)); // 2 - pos += node.length; // 2 + 1 = 3 + result.add(Tuple2(pos + beg, node.style)); + pos += node.length; } } - // Add line style - // result.add(style); - // TODO: add block style as well + // TODO: add current line's style and parent block style as well final remaining = len - local; if (remaining > 0) { - final rest = nextLine!.collectAllIndividualStyles(0, remaining); + final rest = + nextLine!.collectAllIndividualStyles(0, remaining, beg: local); result.addAll(rest); } diff --git a/lib/src/widgets/controller.dart b/lib/src/widgets/controller.dart index 58343eb1..1ef15a91 100644 --- a/lib/src/widgets/controller.dart +++ b/lib/src/widgets/controller.dart @@ -215,14 +215,25 @@ class QuillController extends ChangeNotifier { void handleDelete(int cursorPosition, bool forward) => onDelete?.call(cursorPosition, forward); + void formatTextStyle(int index, int len, Style style) { + style.attributes.forEach((key, attr) { + formatText(index, len, attr); + }); + } + void formatText(int index, int len, Attribute? attribute) { if (len == 0 && attribute!.isInline && attribute.key != Attribute.link.key) { + // Add the attribute to our toggledStyle. + // It will be used later upon insertion. toggledStyle = toggledStyle.put(attribute); } final change = document.format(index, len, attribute); + // Transform selection against the composed change and give priority to + // the change. This is needed in cases when format operation actually + // inserts data into the document (e.g. embeds). final adjustedSelection = selection.copyWith( baseOffset: change.transformPosition(selection.baseOffset), extentOffset: change.transformPosition(selection.extentOffset));