Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: RTL issues #2060

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/src/common/utils/directionality.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

bool isRTLLanguage(Locale locale) {
final rtlLanguages = [
'ar', // Arabic
'dv', // Divehi (Maldivian)
'fa', // Persian (Farsi)
'ha', // Hausa (in Ajami script)
'he', // Hebrew
'khw', // Khowar (in Arabic script)
'ks', // Kashmiri (in Arabic script)
'ku', // Kurdish (Sorani)
'ps', // Pashto
'ur', // Urdu
'yi', // Yiddish
'sd', // Sindhi
'ug', // Uyghur
];
return rtlLanguages.contains(locale.languageCode);
}

bool isRTL(BuildContext context) {
return isRTLLanguage(Localizations.localeOf(context));
}
EchoEllet marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 12 additions & 4 deletions lib/src/editor/raw_editor/raw_editor_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import '../../common/structs/horizontal_spacing.dart';
import '../../common/structs/offset_value.dart';
import '../../common/structs/vertical_spacing.dart';
import '../../common/utils/cast.dart';
import '../../common/utils/directionality.dart';
import '../../common/utils/platform.dart';
import '../../controller/quill_controller.dart';
import '../../delta/delta_diff.dart';
Expand Down Expand Up @@ -975,16 +976,23 @@ class QuillRawEditorState extends EditorState
}

prevNodeOl = attrs[Attribute.list.key] == Attribute.ol;

var nodeTextDirection = getDirectionOfNode(node);
// verify if the direction from nodeTextDirection is the default direction
// and watch if the system language is a RTL language and avoid putting
// to the edge of the left side any checkbox or list point/number if is a
// RTL language
if (nodeTextDirection == TextDirection.ltr && isRTL(context)) {
nodeTextDirection = TextDirection.rtl;
}
if (node is Line) {
final editableTextLine = _getEditableTextLineFromNode(node, context);
result.add(Directionality(
textDirection: getDirectionOfNode(node), child: editableTextLine));
textDirection: nodeTextDirection, child: editableTextLine));
} else if (node is Block) {
final editableTextBlock = EditableTextBlock(
block: node,
controller: controller,
textDirection: getDirectionOfNode(node),
textDirection: nodeTextDirection,
scrollBottomInset: widget.configurations.scrollBottomInset,
horizontalSpacing: _getHorizontalSpacingForBlock(node, _styles),
verticalSpacing: _getVerticalSpacingForBlock(node, _styles),
Expand All @@ -1011,7 +1019,7 @@ class QuillRawEditorState extends EditorState
);
result.add(
Directionality(
textDirection: getDirectionOfNode(node),
textDirection: nodeTextDirection,
child: editableTextBlock,
),
);
Expand Down
19 changes: 18 additions & 1 deletion lib/src/editor/widgets/text/text_block.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/rendering.dart';

import '../../../common/structs/horizontal_spacing.dart';
import '../../../common/structs/vertical_spacing.dart';
import '../../../common/utils/directionality.dart';
import '../../../common/utils/font.dart';
import '../../../controller/quill_controller.dart';
import '../../../delta/delta_diff.dart';
Expand Down Expand Up @@ -134,6 +135,15 @@ class EditableTextBlock extends StatelessWidget {
Block node, DefaultStyles? defaultStyles) {
final attrs = block.style.attributes;
if (attrs.containsKey(Attribute.blockQuote.key)) {
// Verify if the direction is RTL and avoid passing the decoration
// to the left when need to be on right side
if (textDirection == TextDirection.rtl) {
return defaultStyles!.quote!.decoration?.copyWith(
border: Border(
right: BorderSide(width: 4, color: Colors.grey.shade300),
),
);
}
return defaultStyles!.quote!.decoration;
}
if (attrs.containsKey(Attribute.codeBlock.key)) {
Expand Down Expand Up @@ -184,7 +194,14 @@ class EditableTextBlock extends StatelessWidget {
MediaQuery.devicePixelRatioOf(context),
cursorCont,
);
final nodeTextDirection = getDirectionOfNode(line);
var nodeTextDirection = getDirectionOfNode(line);
// verify if the direction from nodeTextDirection is the default direction
// and watch if the system language is a RTL language and avoid putting
// to the edge of the left side any checkbox or list point/number if is a
// RTL language
if (nodeTextDirection == TextDirection.ltr && isRTL(context)) {
nodeTextDirection = TextDirection.rtl;
}
children.add(
Directionality(
textDirection: nodeTextDirection,
Expand Down