Display statuses with appropriate alignment for right-to-left languages (#256)

This commit is contained in:
orenro 2023-01-22 19:17:27 +09:00 committed by GitHub
parent 45ec990384
commit a37d059026
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View file

@ -7,11 +7,13 @@ import SwiftUI
public struct EmojiTextApp: View { public struct EmojiTextApp: View {
private let markdown: HTMLString private let markdown: HTMLString
private let emojis: [any CustomEmoji] private let emojis: [any CustomEmoji]
private let language: String?
private let append: (() -> Text)? 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.markdown = markdown
self.emojis = emojis.map { RemoteEmoji(shortcode: $0.shortcode, url: $0.url) } self.emojis = emojis.map { RemoteEmoji(shortcode: $0.shortcode, url: $0.url) }
self.language = language
self.append = append self.append = append
} }
@ -23,8 +25,15 @@ public struct EmojiTextApp: View {
} }
} else if emojis.isEmpty { } else if emojis.isEmpty {
Text(markdown.asSafeMarkdownAttributedString) Text(markdown.asSafeMarkdownAttributedString)
.environment(\.layoutDirection, isRTL() ? .rightToLeft : .leftToRight)
} else { } else {
EmojiText(markdown: markdown.asMarkdown, emojis: emojis) 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)
}
} }

View file

@ -215,7 +215,7 @@ public struct StatusRowView: View {
private func makeStatusContentView(status: AnyStatus) -> some View { private func makeStatusContentView(status: AnyStatus) -> some View {
Group { Group {
if !status.spoilerText.asRawText.isEmpty { if !status.spoilerText.asRawText.isEmpty {
EmojiTextApp(status.spoilerText, emojis: status.emojis) EmojiTextApp(status.spoilerText, emojis: status.emojis, language: status.language)
.font(.scaledBody) .font(.scaledBody)
Button { Button {
withAnimation { withAnimation {
@ -230,7 +230,7 @@ public struct StatusRowView: View {
if !viewModel.displaySpoiler { if !viewModel.displaySpoiler {
HStack { HStack {
EmojiTextApp(status.content, emojis: status.emojis) EmojiTextApp(status.content, emojis: status.emojis, language: status.language)
.font(.scaledBody) .font(.scaledBody)
.environment(\.openURL, OpenURLAction { url in .environment(\.openURL, OpenURLAction { url in
routerPath.handleStatus(status: status, url: url) routerPath.handleStatus(status: status, url: url)