Inkspring / app · inkspring.ai / native-mac / Sources / Inkspring / NewsletterPreview.swift
| 1 | import SwiftUI |
| 2 | |
| 3 | struct NewsletterPreview: View { |
| 4 | let title: String |
| 5 | let subtitle: String |
| 6 | let markdown: String |
| 7 | let cover: URL? |
| 8 | @State private var blocks: [[String: Any]] = [] |
| 9 | @State private var error: String? |
| 10 | |
| 11 | var body: some View { |
| 12 | VStack(alignment: .leading, spacing: 12) { |
| 13 | Text("Preview").font(.caption).foregroundStyle(.secondary) |
| 14 | AttachedMedia(url: cover) |
| 15 | Text(title.isEmpty ? "Your newsletter title" : title) |
| 16 | .font(InkspringTypography.editorial(32, relativeTo: .title)) |
| 17 | .accessibilityAddTraits(.isHeader) |
| 18 | if !subtitle.isEmpty { |
| 19 | Text(subtitle) |
| 20 | .font(InkspringTypography.interface(19)) |
| 21 | .lineSpacing(5) |
| 22 | .foregroundStyle(.secondary) |
| 23 | } |
| 24 | if let error { |
| 25 | Text(error).foregroundStyle(.secondary) |
| 26 | } else { |
| 27 | ForEach(Array(blocks.enumerated()), id: \.offset) { _, block in |
| 28 | NewsletterPreviewBlock(node: block) |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | .textSelection(.enabled) |
| 33 | .frame(maxWidth: .infinity, alignment: .leading) |
| 34 | .padding(14) |
| 35 | .studioSurface() |
| 36 | .task(id: markdown) { |
| 37 | do { |
| 38 | try await Task.sleep(for: .milliseconds(180)) |
| 39 | blocks = try NewsletterDocument.parse(markdown)["content"] as? [[String: Any]] ?? [] |
| 40 | error = nil |
| 41 | } catch is CancellationError { |
| 42 | } catch let failure { |
| 43 | error = failure.localizedDescription |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | private struct NewsletterPreviewBlock: View { |
| 50 | @Environment(\.studioPalette) private var palette |
| 51 | let node: [String: Any] |
| 52 | |
| 53 | private var children: [[String: Any]] { node["content"] as? [[String: Any]] ?? [] } |
| 54 | private var attributes: [String: Any] { node["attrs"] as? [String: Any] ?? [:] } |
| 55 | |
| 56 | var body: some View { |
| 57 | switch node["type"] as? String { |
| 58 | case "heading": |
| 59 | let level = attributes["level"] as? Int ?? 1 |
| 60 | Text(inline) |
| 61 | .font(InkspringTypography.editorial(level == 1 ? 28 : level == 2 ? 23 : 19, relativeTo: .headline)) |
| 62 | .accessibilityAddTraits(.isHeader) |
| 63 | case "paragraph": |
| 64 | Text(inline) |
| 65 | .font(InkspringTypography.interface(17)) |
| 66 | .lineSpacing(5) |
| 67 | case "blockquote": |
| 68 | blockChildren |
| 69 | .padding(.leading, 12) |
| 70 | .overlay(alignment: .leading) { Rectangle().fill(palette.line).frame(width: 2) } |
| 71 | case "bulletList", "orderedList": |
| 72 | let start = attributes["start"] as? Int |
| 73 | VStack(alignment: .leading, spacing: 5) { |
| 74 | ForEach(Array(children.enumerated()), id: \.offset) { index, child in |
| 75 | HStack(alignment: .firstTextBaseline, spacing: 8) { |
| 76 | Text(start.map { "\($0 + index)." } ?? "•").foregroundStyle(.secondary) |
| 77 | NewsletterPreviewBlock(node: child) |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | case "listItem": |
| 82 | blockChildren |
| 83 | case "code_block": |
| 84 | VStack(alignment: .leading, spacing: 6) { |
| 85 | Text(attributes["language"] as? String ?? "Code").font(.caption) |
| 86 | ScrollView(.horizontal) { |
| 87 | Text(inline).font(.system(.body, design: .monospaced)).fixedSize(horizontal: true, vertical: false) |
| 88 | } |
| 89 | } |
| 90 | .padding(10) |
| 91 | .frame(maxWidth: .infinity, alignment: .leading) |
| 92 | .studioSurface(StudioRadius.control, fill: palette.well) |
| 93 | case "horizontal_rule": |
| 94 | Divider() |
| 95 | case "captionedImage": |
| 96 | blockChildren |
| 97 | case "image2": |
| 98 | if let src = attributes["src"] as? String, let url = URL(string: src), url.scheme == "https" { |
| 99 | Link(destination: url) { |
| 100 | StudioLinkOutLabel { Label(attributes["alt"] as? String ?? "Newsletter image", systemImage: "photo") } |
| 101 | } |
| 102 | } |
| 103 | default: |
| 104 | EmptyView() |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | private var blockChildren: some View { |
| 109 | VStack(alignment: .leading, spacing: 7) { |
| 110 | ForEach(Array(children.enumerated()), id: \.offset) { _, child in |
| 111 | NewsletterPreviewBlock(node: child) |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | private var inline: AttributedString { |
| 117 | var result = AttributedString() |
| 118 | for node in children { |
| 119 | if node["type"] as? String == "hard_break" { |
| 120 | result.append(AttributedString("\n")) |
| 121 | continue |
| 122 | } |
| 123 | guard let text = node["text"] as? String else { continue } |
| 124 | var span = AttributedString(text) |
| 125 | var intent: InlinePresentationIntent = [] |
| 126 | for mark in node["marks"] as? [[String: Any]] ?? [] { |
| 127 | switch mark["type"] as? String { |
| 128 | case "strong": intent.insert(.stronglyEmphasized) |
| 129 | case "em": intent.insert(.emphasized) |
| 130 | case "strikethrough": intent.insert(.strikethrough) |
| 131 | case "code": span.font = .system(.body, design: .monospaced) |
| 132 | case "link": |
| 133 | if let attributes = mark["attrs"] as? [String: Any], let href = attributes["href"] as? String { |
| 134 | span.link = URL(string: href) |
| 135 | } |
| 136 | default: break |
| 137 | } |
| 138 | } |
| 139 | if !intent.isEmpty { span.inlinePresentationIntent = intent } |
| 140 | result.append(span) |
| 141 | } |
| 142 | return result |
| 143 | } |
| 144 | } |