mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-11-10 10:18:57 +03:00
feat: compute string indexes
This commit is contained in:
parent
45556ae015
commit
bee1a04329
@ -260,6 +260,7 @@ TextOperation? _textOperationFromJson(Map<String, dynamic> json) {
|
||||
class Delta extends Iterable<TextOperation> {
|
||||
final List<TextOperation> _operations;
|
||||
String? _rawString;
|
||||
List<int>? _runeIndexes;
|
||||
|
||||
factory Delta.fromJson(List<dynamic> list) {
|
||||
final operations = <TextOperation>[];
|
||||
@ -477,9 +478,23 @@ class Delta extends Iterable<TextOperation> {
|
||||
String toRawString() {
|
||||
_rawString ??=
|
||||
_operations.whereType<TextInsert>().map((op) => op.content).join();
|
||||
_runeIndexes ??= stringIndexes(_rawString!);
|
||||
return _rawString!;
|
||||
}
|
||||
|
||||
@override
|
||||
Iterator<TextOperation> get iterator => _operations.iterator;
|
||||
}
|
||||
|
||||
List<int> stringIndexes(String content) {
|
||||
final indexes = List<int>.filled(content.length, 0);
|
||||
final iterator = content.runes.iterator;
|
||||
|
||||
while (iterator.moveNext()) {
|
||||
for (var i = 0; i < iterator.currentSize; i++) {
|
||||
indexes[iterator.rawIndex + i] = iterator.rawIndex;
|
||||
}
|
||||
}
|
||||
|
||||
return indexes;
|
||||
}
|
||||
|
@ -279,4 +279,9 @@ void main() {
|
||||
expect(delta, expected);
|
||||
});
|
||||
});
|
||||
test("stringIndexes", () {
|
||||
final indexes = stringIndexes('😊');
|
||||
expect(indexes[0], 0);
|
||||
expect(indexes[1], 0);
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user