Skip to content

Commit

Permalink
chore: refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 committed Sep 20, 2023
1 parent f434858 commit a389e7f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,9 @@ TextDirection calculateNodeDirection({

// if the value is auto and the text isn't null or empty,
// calculate the text direction by the text
final direction = _determineTextDirection(text);
if (direction != null) {
return direction;
}

return defaultTextDirection?.toTextDirection() ?? layoutDirection;
return _determineTextDirection(text) ??
defaultTextDirection?.toTextDirection() ??
layoutDirection;
}

TextDirection? _getDirectionFromPreviousOrParentNode(
Expand All @@ -109,34 +106,31 @@ TextDirection? _getDirectionFromPreviousOrParentNode(
) {
TextDirection? prevOrParentNodeDirection;
if (node.previous != null) {
prevOrParentNodeDirection =
_getDirectionFromNode(node.previous!, defaultTextDirection);
prevOrParentNodeDirection = _getDirectionFromNode(
node.previous!,
defaultTextDirection,
);
}
if (node.parent != null && prevOrParentNodeDirection == null) {
prevOrParentNodeDirection =
_getDirectionFromNode(node.parent!, defaultTextDirection);
prevOrParentNodeDirection = _getDirectionFromNode(
node.parent!,
defaultTextDirection,
);
}

return prevOrParentNodeDirection;
}

TextDirection? _getDirectionFromNode(Node node, String? defaultTextDirection) {
String? nodeDirection;
if (defaultTextDirection == blockComponentTextDirectionAuto) {
nodeDirection = blockComponentTextDirectionAuto;
}

nodeDirection = node.direction(nodeDirection);

if (nodeDirection != null) {
if (nodeDirection == blockComponentTextDirectionAuto) {
return node.selectable?.textDirection();
} else {
return nodeDirection.toTextDirection();
}
final nodeDirection = node.direction(
defaultTextDirection == blockComponentTextDirectionAuto
? blockComponentTextDirectionAuto
: null,
);
if (nodeDirection == blockComponentTextDirectionAuto) {
return node.selectable?.textDirection();
} else {
return nodeDirection?.toTextDirection();
}

return null;
}

TextDirection? _determineTextDirection(String text) {
Expand All @@ -152,10 +146,8 @@ TextDirection? _determineTextDirection(String text) {
}

extension on Node {
String? direction(String? defaultDirection) {
return (attributes[blockComponentTextDirection] as String?) ??
defaultDirection;
}
String? direction(String? defaultDirection) =>
attributes[blockComponentTextDirection] as String? ?? defaultDirection;
}

extension on String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,18 @@ mixin NestedBlockComponentStatefulWidgetMixin<
TextDirection direction =
Directionality.maybeOf(context) ?? TextDirection.ltr;
if (node.children.isNotEmpty) {
TextDirection? lastDirection;
if (node.children.first.key.currentState != null) {
lastDirection = (node.children.first.key.currentState
as BlockComponentTextDirectionMixin)
.lastDirection;
final firstChild = node.children.first;
final currentState =
firstChild.key.currentState as BlockComponentTextDirectionMixin?;
if (currentState != null) {
final lastDirection = currentState.lastDirection;
direction = calculateNodeDirection(
node: firstChild,
layoutDirection: direction,
defaultTextDirection: editorState.editorStyle.defaultTextDirection,
lastDirection: lastDirection,
);
}

direction = calculateNodeDirection(
node: node.children.first,
layoutDirection: direction,
defaultTextDirection: editorState.editorStyle.defaultTextDirection,
lastDirection: lastDirection,
);
}
return configuration.indentPadding(node, direction);
}
Expand Down

0 comments on commit a389e7f

Please sign in to comment.