From a37d0590260c30b175239117cb770bec7436e250 Mon Sep 17 00:00:00 2001 From: orenro Date: Sun, 22 Jan 2023 19:17:27 +0900 Subject: [PATCH] Display statuses with appropriate alignment for right-to-left languages (#256) --- .../Sources/DesignSystem/Views/EmojiText.swift | 11 ++++++++++- .../Status/Sources/Status/Row/StatusRowView.swift | 4 ++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Packages/DesignSystem/Sources/DesignSystem/Views/EmojiText.swift b/Packages/DesignSystem/Sources/DesignSystem/Views/EmojiText.swift index 65b75f6f..2d494da0 100644 --- a/Packages/DesignSystem/Sources/DesignSystem/Views/EmojiText.swift +++ b/Packages/DesignSystem/Sources/DesignSystem/Views/EmojiText.swift @@ -7,11 +7,13 @@ import SwiftUI public struct EmojiTextApp: View { private let markdown: HTMLString private let emojis: [any CustomEmoji] + private let language: String? private let append: (() -> Text)? - public init(_ markdown: HTMLString, emojis: [Emoji], append: (() -> Text)? = nil) { + public init(_ markdown: HTMLString, emojis: [Emoji], language: String? = nil, append: (() -> Text)? = nil) { self.markdown = markdown self.emojis = emojis.map { RemoteEmoji(shortcode: $0.shortcode, url: $0.url) } + self.language = language self.append = append } @@ -23,8 +25,15 @@ public struct EmojiTextApp: View { } } else if emojis.isEmpty { Text(markdown.asSafeMarkdownAttributedString) + .environment(\.layoutDirection, isRTL() ? .rightToLeft : .leftToRight) } else { EmojiText(markdown: markdown.asMarkdown, emojis: emojis) + .environment(\.layoutDirection, isRTL() ? .rightToLeft : .leftToRight) } } + + private func isRTL() -> Bool { + // Arabic, Hebrew, Persian, Urdu, Kurdish, Azeri, Dhivehi + return ["ar", "he", "fa", "ur", "ku", "az", "dv"].contains(self.language) + } } diff --git a/Packages/Status/Sources/Status/Row/StatusRowView.swift b/Packages/Status/Sources/Status/Row/StatusRowView.swift index 0fb7e6b5..98dc1253 100644 --- a/Packages/Status/Sources/Status/Row/StatusRowView.swift +++ b/Packages/Status/Sources/Status/Row/StatusRowView.swift @@ -215,7 +215,7 @@ public struct StatusRowView: View { private func makeStatusContentView(status: AnyStatus) -> some View { Group { if !status.spoilerText.asRawText.isEmpty { - EmojiTextApp(status.spoilerText, emojis: status.emojis) + EmojiTextApp(status.spoilerText, emojis: status.emojis, language: status.language) .font(.scaledBody) Button { withAnimation { @@ -230,7 +230,7 @@ public struct StatusRowView: View { if !viewModel.displaySpoiler { HStack { - EmojiTextApp(status.content, emojis: status.emojis) + EmojiTextApp(status.content, emojis: status.emojis, language: status.language) .font(.scaledBody) .environment(\.openURL, OpenURLAction { url in routerPath.handleStatus(status: status, url: url)