Esc: make the panel key when shown (closeWithEscape, default on) without activating the app; the source app stays frontmost and Replace still targets it. Handle Esc with SwiftUI's exit command, which also works from the popover and after clicking controls. Add TranslationPanel.canBecomeKey and amend ADR-009.\n\nMarkdown: add MarkdownPresentation in SprachmittlerText (Foundation inline parser plus a conservative block parser for headings, lists, quotes, fenced code and rules), a SwiftUI MarkdownTranslationView, and a renderMarkdown setting (default on). JSON, HTML and code sources are never rendered; only http/https links open; images become alt text; Copy and Replace keep the raw Markdown (ADR-021).\n\nVerified: make verify green; synthetic Esc closes the panel and closes an open language popover first; a live Markdown translation rendered heading, bold/italic, inline code, list, quote, code block and link; Copy returned the raw Markdown; Replace still changed the TextEdit selection with the panel key.
137 lines
5.0 KiB
Swift
137 lines
5.0 KiB
Swift
import AppKit
|
|
import SprachmittlerText
|
|
import SwiftUI
|
|
|
|
/// Renders a translation as formatted Markdown. Copy and Replace still use the raw translation.
|
|
struct MarkdownTranslationView: View {
|
|
let markdown: String
|
|
let fontSize: Int
|
|
let isRTL: Bool
|
|
|
|
private let blocks: [MarkdownBlock]
|
|
|
|
init(markdown: String, fontSize: Int, isRTL: Bool) {
|
|
self.markdown = markdown
|
|
self.fontSize = fontSize
|
|
self.isRTL = isRTL
|
|
blocks = MarkdownPresentation.blocks(in: markdown)
|
|
}
|
|
|
|
var body: some View {
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
ForEach(blocks.indices, id: \.self) { index in
|
|
block(blocks[index])
|
|
}
|
|
}
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
.environment(\.layoutDirection, isRTL ? .rightToLeft : .leftToRight)
|
|
.environment(\.openURL, OpenURLAction { url in
|
|
guard MarkdownPresentation.isAllowedLink(url) else { return .discarded }
|
|
NSWorkspace.shared.open(url)
|
|
return .handled
|
|
})
|
|
.accessibilityElement(children: .contain)
|
|
.accessibilityLabel(String(localized: "Translation"))
|
|
}
|
|
|
|
private var baseSize: CGFloat {
|
|
CGFloat(fontSize)
|
|
}
|
|
|
|
@ViewBuilder private func block(_ block: MarkdownBlock) -> some View {
|
|
switch block {
|
|
case let .paragraph(text):
|
|
inlineText(text, size: baseSize)
|
|
case let .heading(level, text):
|
|
inlineText(text, size: baseSize * headingScale(level), weight: .bold)
|
|
.accessibilityAddTraits(.isHeader)
|
|
case let .listItem(ordered, number, indent, text):
|
|
HStack(alignment: .firstTextBaseline, spacing: 6) {
|
|
Text(ordered ? "\(number)." : "•")
|
|
.font(.system(size: baseSize, weight: ordered ? .regular : .bold))
|
|
.foregroundStyle(.secondary)
|
|
.frame(minWidth: ordered ? 18 : 12, alignment: .trailing)
|
|
.accessibilityHidden(true)
|
|
inlineText(text, size: baseSize)
|
|
}
|
|
.padding(.leading, CGFloat(indent) * 14)
|
|
case let .quote(text):
|
|
HStack(alignment: .top, spacing: 8) {
|
|
Rectangle()
|
|
.fill(.secondary.opacity(0.35))
|
|
.frame(width: 3)
|
|
.accessibilityHidden(true)
|
|
inlineText(text, size: baseSize)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
case let .codeBlock(_, text):
|
|
ScrollView(.horizontal, showsIndicators: false) {
|
|
Text(verbatim: text)
|
|
.font(.system(size: baseSize * 0.95, design: .monospaced))
|
|
.textSelection(.enabled)
|
|
.fixedSize(horizontal: true, vertical: false)
|
|
.padding(10)
|
|
}
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
.background(.primary.opacity(0.06), in: RoundedRectangle(cornerRadius: 8))
|
|
.environment(\.layoutDirection, .leftToRight)
|
|
case .rule:
|
|
Divider()
|
|
.padding(.vertical, 2)
|
|
}
|
|
}
|
|
|
|
private func inlineText(_ text: String, size: CGFloat, weight: Font.Weight = .regular) -> some View {
|
|
Text(styled(text, size: size))
|
|
.font(.system(size: size, weight: weight))
|
|
.textSelection(.enabled)
|
|
.fixedSize(horizontal: false, vertical: true)
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
}
|
|
|
|
private func styled(_ text: String, size: CGFloat) -> AttributedString {
|
|
var attributed = MarkdownPresentation.inline(text)
|
|
let runs = attributed.runs.compactMap { run -> (Range<AttributedString.Index>, InlinePresentationIntent)? in
|
|
guard let intent = run.inlinePresentationIntent else { return nil }
|
|
let relevant = intent.contains(.stronglyEmphasized)
|
|
|| intent.contains(.emphasized)
|
|
|| intent.contains(.code)
|
|
return relevant ? (run.range, intent) : nil
|
|
}
|
|
|
|
for (range, intent) in runs {
|
|
if intent.contains(.code) {
|
|
var font = Font.system(size: size * 0.95, design: .monospaced)
|
|
if intent.contains(.stronglyEmphasized) {
|
|
font = font.bold()
|
|
}
|
|
if intent.contains(.emphasized) {
|
|
font = font.italic()
|
|
}
|
|
attributed[range].font = font
|
|
} else {
|
|
var font = Font.system(
|
|
size: size,
|
|
weight: intent.contains(.stronglyEmphasized) ? .bold : .regular
|
|
)
|
|
if intent.contains(.emphasized) {
|
|
font = font.italic()
|
|
}
|
|
attributed[range].font = font
|
|
}
|
|
}
|
|
return attributed
|
|
}
|
|
|
|
private func headingScale(_ level: Int) -> CGFloat {
|
|
switch level {
|
|
case 1: 1.6
|
|
case 2: 1.45
|
|
case 3: 1.3
|
|
case 4: 1.2
|
|
case 5: 1.12
|
|
default: 1.06
|
|
}
|
|
}
|
|
}
|