mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: compute string indexes
This commit is contained in:
@ -260,6 +260,7 @@ TextOperation? _textOperationFromJson(Map<String, dynamic> json) {
|
|||||||
class Delta extends Iterable<TextOperation> {
|
class Delta extends Iterable<TextOperation> {
|
||||||
final List<TextOperation> _operations;
|
final List<TextOperation> _operations;
|
||||||
String? _rawString;
|
String? _rawString;
|
||||||
|
List<int>? _runeIndexes;
|
||||||
|
|
||||||
factory Delta.fromJson(List<dynamic> list) {
|
factory Delta.fromJson(List<dynamic> list) {
|
||||||
final operations = <TextOperation>[];
|
final operations = <TextOperation>[];
|
||||||
@ -477,9 +478,23 @@ class Delta extends Iterable<TextOperation> {
|
|||||||
String toRawString() {
|
String toRawString() {
|
||||||
_rawString ??=
|
_rawString ??=
|
||||||
_operations.whereType<TextInsert>().map((op) => op.content).join();
|
_operations.whereType<TextInsert>().map((op) => op.content).join();
|
||||||
|
_runeIndexes ??= stringIndexes(_rawString!);
|
||||||
return _rawString!;
|
return _rawString!;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Iterator<TextOperation> get iterator => _operations.iterator;
|
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);
|
expect(delta, expected);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
test("stringIndexes", () {
|
||||||
|
final indexes = stringIndexes('😊');
|
||||||
|
expect(indexes[0], 0);
|
||||||
|
expect(indexes[1], 0);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user