Update collectAllIndividualStyles

pull/605/head
X Code 3 years ago
parent cd035d008e
commit 15a86e24cd
  1. 18
      lib/src/models/documents/nodes/line.dart
  2. 11
      lib/src/widgets/controller.dart

@ -384,29 +384,29 @@ class Line extends Container<Leaf?> {
}
/// Returns each offset in selection with its corresponding styles as a list
List<Tuple2<int, Style>> collectAllIndividualStyles(int offset, int len) {
List<Tuple2<int, Style>> collectAllIndividualStyles(int offset, int len,
{int beg = 0}) {
final local = math.min(length - offset, len);
final result = <Tuple2<int, Style>>[];
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);
}

@ -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));

Loading…
Cancel
Save