Skip to content

Commit

Permalink
Fix RTL detection & add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amantoux committed Dec 4, 2023
1 parent cd8841e commit 60eaf07
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
16 changes: 8 additions & 8 deletions packages/fleather/lib/src/widgets/autoformats.dart
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,13 @@ class _AutoTextDirection extends AutoFormat {
return data == null || (data is String ? data.endsWith('\n') : false);
}

bool _isBeforeEmptyLine(Operation next) {
final data = next.data;
return data is String ? data.startsWith('\n') : false;
bool _isBeforeEmptyLine(Operation next, String data) {
final nextData = next.data;
return nextData is String ? nextData.startsWith('$data\n') : false;
}

bool _isInEmptyLine(Operation? previous, Operation next) =>
_isAfterEmptyLine(previous) && _isBeforeEmptyLine(next);
bool _isInEmptyLine(Operation? previous, Operation next, String data) =>
_isAfterEmptyLine(previous) && _isBeforeEmptyLine(next, data);

@override
AutoFormatResult? apply(
Expand All @@ -370,7 +370,7 @@ class _AutoTextDirection extends AutoFormat {
final previous = iter.skip(position);
final next = iter.next();

if (!_isInEmptyLine(previous, next)) return null;
if (!_isInEmptyLine(previous, next, data)) return null;

final Map<String, dynamic> attributes;
if (_isRTL(data)) {
Expand All @@ -386,10 +386,10 @@ class _AutoTextDirection extends AutoFormat {
}

final change = Delta()
..retain(position)
..insert(data)
..retain(position + data.length) //
..retain(1, attributes);
final undo = change.invert(documentDelta);
document.compose(change, ChangeSource.local);
return AutoFormatResult(
selection: TextSelection.collapsed(offset: position + data.length),
change: change,
Expand Down
15 changes: 15 additions & 0 deletions packages/fleather/test/widgets/autoformats_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,19 @@ void main() {
expect(autoformats.hasActiveSuggestion, isFalse);
});
});

group('RTL detection', () {
test('Detection of RTL', () {
final document = ParchmentDocument.fromJson([
{'insert': 'some ltr text\nש\n'}
]);
final selection = autoformats.run(document, 14, 'ש');
expect(selection, const TextSelection.collapsed(offset: 15));
final attributes = document.toDelta().toList()[1].attributes;
expect(attributes, isNotNull);
expect(attributes!.containsKey(ParchmentAttribute.direction.key), isTrue);
expect(attributes[ParchmentAttribute.direction.key],
ParchmentAttribute.direction.rtl.value);
});
});
}

0 comments on commit 60eaf07

Please sign in to comment.