Add getPlainText method

pull/605/head
X Code 3 years ago
parent b39ee617d5
commit ef7d17f2d8
  1. 6
      lib/src/models/documents/document.dart
  2. 30
      lib/src/models/documents/nodes/line.dart
  3. 7
      lib/src/widgets/controller.dart

@ -169,6 +169,12 @@ class Document {
return (res.node as Line).collectAllStyles(res.offset, len);
}
/// Returns plain text within the specified text range.
String getPlainText(int index, int len) {
final res = queryChild(index);
return (res.node as Line).getPlainText(res.offset, len);
}
/// Returns [Line] located at specified character [offset].
ChildQuery queryChild(int offset) {
// TODO: prevent user from moving caret after last line-break.

@ -384,7 +384,7 @@ class Line extends Container<Leaf?> {
}
/// Returns each node segment's length in selection
/// with its corresponding styles as a list
/// with its corresponding style as a list
List<Tuple2<int, Style>> collectAllIndividualStyles(int offset, int len) {
final local = math.min(length - offset, len);
final result = <Tuple2<int, Style>>[];
@ -401,7 +401,7 @@ class Line extends Container<Leaf?> {
}
}
// TODO: add current line's style and parent block style as well
// Need to add current line's style or parent block's style?
final remaining = len - local;
if (remaining > 0) {
@ -444,4 +444,30 @@ class Line extends Container<Leaf?> {
return result;
}
/// Returns plain text within the specified text range.
String getPlainText(int offset, int len) {
final local = math.min(length - offset, len);
final result = <String>[];
final data = queryChild(offset, true);
var node = data.node as Leaf?;
if (node != null) {
result.add(node.toPlainText());
var pos = node.length - data.offset;
while (!node!.isLast && pos < local) {
node = node.next as Leaf;
result.add(node.toPlainText());
pos += node.length;
}
}
final remaining = len - local;
if (remaining > 0) {
final rest = nextLine!.getPlainText(0, remaining);
result.add(rest);
}
return result.join();
}
}

@ -91,6 +91,13 @@ class QuillController extends ChangeNotifier {
return styles;
}
/// Returns plain text for each node within selection
String getPlainText() {
final text =
document.getPlainText(selection.start, selection.end - selection.start);
return text;
}
/// Returns all styles for any character within the specified text range.
List<Style> getAllSelectionStyles() {
final styles = document.collectAllStyles(

Loading…
Cancel
Save