Refactor code for collectAllStyles

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

@ -119,13 +119,17 @@ class Document {
return delta; return delta;
} }
Style collectStyle(int index, int len, bool collectAll) { /// Only attributes applied to all characters within this range are
/// included in the result.
Style collectStyle(int index, int len) {
final res = queryChild(index); final res = queryChild(index);
if (collectAll) { return (res.node as Line).collectStyle(res.offset, len);
return (res.node as Line).collectAllStyle(res.offset, len); }
} else {
return (res.node as Line).collectStyle(res.offset, len); /// Returns all style for any character within the specified text range.
} Style collectAllStyles(int index, int len) {
final res = queryChild(index);
return (res.node as Line).collectAllStyles(res.offset, len);
} }
ChildQuery queryChild(int offset) { ChildQuery queryChild(int offset) {

@ -370,7 +370,7 @@ 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 collectAllStyle(int offset, int len) { Style collectAllStyles(int offset, int len) {
final local = math.min(length - offset, len); final local = math.min(length - offset, len);
var result = Style(); var result = Style();
@ -394,11 +394,10 @@ class Line extends Container<Leaf?> {
final remaining = len - local; final remaining = len - local;
if (remaining > 0) { if (remaining > 0) {
final rest = nextLine!.collectAllStyle(0, remaining); final rest = nextLine!.collectAllStyles(0, remaining);
result = result.mergeAll(rest); result = result.mergeAll(rest);
} }
return result; return result;
} }
} }

@ -55,15 +55,18 @@ class QuillController extends ChangeNotifier {
selection: selection, selection: selection,
); );
/// Only attributes applied to all characters within this range are
/// included in the result.
Style getSelectionStyle() { Style getSelectionStyle() {
return document return document
.collectStyle(selection.start, selection.end - selection.start, false) .collectStyle(selection.start, selection.end - selection.start)
.mergeAll(toggledStyle); .mergeAll(toggledStyle);
} }
Style getAllSelectionStyle() { /// Returns all style for any character within the specified text range.
Style getAllSelectionStyles() {
return document return document
.collectStyle(selection.start, selection.end - selection.start, true) .collectAllStyles(selection.start, selection.end - selection.start)
.mergeAll(toggledStyle); .mergeAll(toggledStyle);
} }

@ -34,7 +34,7 @@ class _ClearFormatButtonState extends State<ClearFormatButton> {
fillColor: fillColor, fillColor: fillColor,
onPressed: () { onPressed: () {
for (final k for (final k
in widget.controller.getAllSelectionStyle().attributes.values) { in widget.controller.getAllSelectionStyles().attributes.values) {
widget.controller.formatSelection(Attribute.clone(k, null)); widget.controller.formatSelection(Attribute.clone(k, null));
} }
}); });

Loading…
Cancel
Save