diff --git a/lib/src/models/documents/document.dart b/lib/src/models/documents/document.dart index 0bc42c4f..0283a011 100644 --- a/lib/src/models/documents/document.dart +++ b/lib/src/models/documents/document.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. diff --git a/lib/src/models/documents/nodes/line.dart b/lib/src/models/documents/nodes/line.dart index 163d284e..70470901 100644 --- a/lib/src/models/documents/nodes/line.dart +++ b/lib/src/models/documents/nodes/line.dart @@ -384,7 +384,7 @@ class Line extends Container { } /// Returns each node segment's length in selection - /// with its corresponding styles as a list + /// with its corresponding style as a list List> collectAllIndividualStyles(int offset, int len) { final local = math.min(length - offset, len); final result = >[]; @@ -401,7 +401,7 @@ class Line extends Container { } } - // 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 { 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 = []; + + 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(); + } } diff --git a/lib/src/widgets/controller.dart b/lib/src/widgets/controller.dart index 1ef15a91..d3937027 100644 --- a/lib/src/widgets/controller.dart +++ b/lib/src/widgets/controller.dart @@ -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