Repository navigation
Conversation
… paragraphs TextAlignment.LEFT mapped to Layout.Alignment.ALIGN_NORMAL and RIGHT to ALIGN_OPPOSITE, which are relative to the paragraph's direction, so in a Hebrew or Arabic paragraph or heading `left` rendered on the right and `right` on the left. iOS (NSTextAlignmentLeft/Right) and web (CSS text-align) both treat them as physical sides. Port the per-paragraph resolution from packages/enriched-markdown-android (#872): TextAlignment.layoutAlignment(isRtl) and applyTextAlignment, which resolves each paragraph's direction from its first strong character, falling back to the layout direction. auto, center and justify are unchanged.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What/Why?
On Android,
markdownStyletextAlign: 'left'/'right'onparagraphandh1–h6flipped in right-to-left paragraphs: a Hebrew or Arabic paragraph withtextAlign: 'left'rendered right-aligned, and one with'right'rendered left-aligned.The cause:
TextAlignment.LEFTmapped toLayout.Alignment.ALIGN_NORMAL(andRIGHTtoALIGN_OPPOSITE). Android's layout alignments are relative to the paragraph's direction, so in an RTL paragraphALIGN_NORMALis the right side. iOS mapsleft/righttoNSTextAlignmentLeft/Right(physical sides;ios/utils/ParagraphStyleUtils.mtextAlignmentFromString), and web passes them to CSStext-align(src/web/styles.ts), so on those platforms they are physical sides too.The fix is ported from the standalone library
packages/enriched-markdown-android(#872):styles/TextAlignment.kt:layoutAlignment(isRtl)andisAbsolutereplace the fixedlayoutAlignment/needsAlignmentSpan.utils/text/span/TextAlignmentSpans.kt(new, taken from the library as is):applyTextAlignmentresolvesleft/rightper text paragraph from its first strong character, falling back to the layout direction when there is none. That is how the TextView resolves each paragraph's direction, so the span always lands on the requested physical side.ParagraphRendererandHeadingRenderercallapplyTextAlignment, as in the library.The JS-facing values are unchanged (
auto,left,right,center,justify). The library'sSTART/ENDvalues were not ported, because the JS API has nostart/endvalues.auto,centerandjustifybehave as before:autoandjustifyadd no span (justify is stilljustificationModeon the TextView), andcenteraddsALIGN_CENTER.writingDirectionis a no-op on Android (each paragraph is already resolved first-strong), so it does not change anything here.Tables are not touched. Table cells still inherit the paragraph's
textAlignwhen the column has no alignment of its own, as before (and as on iOS). So withparagraph.textAlign: 'left', an RTL cell in a column with no alignment is now left-aligned, like a paragraph would be.Testing
On the Playground (Android emulator, API 36), with a temporary toggle (not committed) that sets
paragraphandh1textAlign, I set this markdown:I checked it in both flavors: "Reference links: Off" renders GitHub and "CommonMark" renders CommonMark.
left: before, the Hebrew heading and paragraph stayed on the right. After, all four blocks are on the left.right: before, the Hebrew blocks were on the left. After, all four blocks are on the right.auto: Hebrew is on the right and English on the left, both before and after (unchanged).Unit tests added:
styles/TextAlignmentTest: alignment × direction →Layout.Alignment,isAbsolute, and parsing the JS values.TextAlignmentRendererTest(Robolectric, ported from the library's test): a rendered RTL or LTR paragraph and heading get the expectedAlignmentSpan; a paragraph is resolved line by line; text with no strong character follows the layout direction (arlocale).:react-native-enriched-markdown:testDebugUnitTestpasses (54 tests) and:react-native-enriched-markdown:ktlintCheckis clean.Shots below use the GitHub flavor; preview box at the bottom.
textAlign: 'left'textAlign: 'right'PR Checklist