fix clear format button

pull/295/head
li3317 4 years ago
parent 6ce51a6600
commit 6751472a55
  1. 8
      lib/src/models/documents/document.dart
  2. 33
      lib/src/models/documents/nodes/line.dart
  3. 8
      lib/src/widgets/controller.dart
  4. 2
      lib/src/widgets/toolbar/clear_format_button.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) {

@ -368,4 +368,37 @@ class Line extends Container<Leaf?> {
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;
}
}

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

@ -34,7 +34,7 @@ class _ClearFormatButtonState extends State<ClearFormatButton> {
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));
}
});

Loading…
Cancel
Save