collectAllStyles should return List<Style>

pull/295/head
Xin Yao 4 years ago
parent bbabf4e25d
commit dad5c676cb
  1. 2
      lib/src/models/documents/document.dart
  2. 14
      lib/src/models/documents/nodes/line.dart
  3. 9
      lib/src/widgets/controller.dart
  4. 9
      lib/src/widgets/toolbar/clear_format_button.dart

@ -127,7 +127,7 @@ class Document {
} }
/// Returns all style for any character within the specified text range. /// Returns all style for any character within the specified text range.
Style collectAllStyles(int index, int len) { List<Style> collectAllStyles(int index, int len) {
final res = queryChild(index); final res = queryChild(index);
return (res.node as Line).collectAllStyles(res.offset, len); return (res.node as Line).collectAllStyles(res.offset, len);
} }

@ -370,32 +370,32 @@ class Line extends Container<Leaf?> {
} }
/// Returns all style for any character within the specified text range. /// Returns all style for any character within the specified text range.
Style collectAllStyles(int offset, int len) { List<Style> collectAllStyles(int offset, int len) {
final local = math.min(length - offset, len); final local = math.min(length - offset, len);
var result = Style(); final result = <Style>[];
final data = queryChild(offset, true); final data = queryChild(offset, true);
var node = data.node as Leaf?; var node = data.node as Leaf?;
if (node != null) { if (node != null) {
result = result.mergeAll(node.style); result.add(node.style);
var pos = node.length - data.offset; var pos = node.length - data.offset;
while (!node!.isLast && pos < local) { while (!node!.isLast && pos < local) {
node = node.next as Leaf?; node = node.next as Leaf?;
result = result.mergeAll(node!.style); result.add(node!.style);
pos += node.length; pos += node.length;
} }
} }
result = result.mergeAll(style); result.add(style);
if (parent is Block) { if (parent is Block) {
final block = parent as Block; final block = parent as Block;
result = result.mergeAll(block.style); result.add(block.style);
} }
final remaining = len - local; final remaining = len - local;
if (remaining > 0) { if (remaining > 0) {
final rest = nextLine!.collectAllStyles(0, remaining); final rest = nextLine!.collectAllStyles(0, remaining);
result = result.mergeAll(rest); result.addAll(rest);
} }
return result; return result;

@ -64,10 +64,11 @@ class QuillController extends ChangeNotifier {
} }
/// Returns all style for any character within the specified text range. /// Returns all style for any character within the specified text range.
Style getAllSelectionStyles() { List<Style> getAllSelectionStyles() {
return document final styles = document.collectAllStyles(
.collectAllStyles(selection.start, selection.end - selection.start) selection.start, selection.end - selection.start)
.mergeAll(toggledStyle); ..add(toggledStyle);
return styles;
} }
void undo() { void undo() {

@ -33,8 +33,13 @@ class _ClearFormatButtonState extends State<ClearFormatButton> {
icon: Icon(widget.icon, size: widget.iconSize, color: iconColor), icon: Icon(widget.icon, size: widget.iconSize, color: iconColor),
fillColor: fillColor, fillColor: fillColor,
onPressed: () { onPressed: () {
for (final k final keys = <Attribute>{};
in widget.controller.getAllSelectionStyles().attributes.values) { for (final style in widget.controller.getAllSelectionStyles()) {
for (final k in style.attributes.values) {
keys.add(k);
}
}
for (final k in keys) {
widget.controller.formatSelection(Attribute.clone(k, null)); widget.controller.formatSelection(Attribute.clone(k, null));
} }
}); });

Loading…
Cancel
Save