Repository Public License
1import AppKit2import PrismCore3import PrismLogicRuntime4import PrismObjectModel5import SwiftUI67/// Routes the text components of family A.8struct FamilyAText: View {9 static let types: Set<String> = [10 "prism.codeBlock", "prism.quote", "prism.markdown", "prism.link", "prism.shortcut", "prism.label", "prism.breadcrumb",11 ]12 let o: PrismObject1314 var body: some View {15 switch o.type {16 case "prism.codeBlock": CodeBlockComponent(o: o)17 case "prism.quote": QuoteComponent(o: o)18 case "prism.markdown": MarkdownComponent(o: o)19 case "prism.link": LinkComponent(o: o)20 case "prism.shortcut": ShortcutComponent(o: o)21 case "prism.label": LabelComponent(o: o)22 default: BreadcrumbComponent(o: o)23 }24 }25}2627/// Monospaced, selectable code with a copy button.28struct CodeBlockComponent: View {29 @Environment(AppRuntime.self) private var runtime30 @Environment(\.prismTheme) private var theme31 let o: PrismObject32 @State private var copied = false3334 var body: some View {35 let code = runtime.string(o.props["code"])36 let language = runtime.string(o.props["language"])37 let showCopy = o.props["showCopy"]?.boolValue != false38 let shape = RoundedRectangle(cornerRadius: theme.radius, style: .continuous)39 VStack(alignment: .leading, spacing: 0) {40 if showCopy || !language.isEmpty {41 HStack {42 Text(language).font(.caption.weight(.medium)).foregroundStyle(.secondary)43 Spacer()44 if showCopy {45 Button { copy(code) } label: { Label(copied ? "Copied" : "Copy", systemImage: copied ? "checkmark" : "doc.on.doc") }46 .buttonStyle(.borderless).font(.caption).disabled(code.isEmpty).accessibilityLabel("Copy code")47 }48 }49 .padding(.horizontal, 12).padding(.vertical, 6)50 Divider()51 }52 if o.props["wrap"]?.boolValue == true {53 text(code).fixedSize(horizontal: false, vertical: true).padding(12).frame(maxWidth: .infinity, alignment: .leading)54 } else {55 ScrollView(.horizontal) { text(code).fixedSize().padding(12) }56 }57 }58 .frame(maxWidth: .infinity, alignment: .leading)59 .background(.quaternary.opacity(0.45), in: shape)60 .overlay(shape.strokeBorder(.separator.opacity(0.6)).allowsHitTesting(false))61 }6263 private func text(_ code: String) -> some View {64 Text(code).font(.callout.monospaced()).textSelection(.enabled)65 }6667 private func copy(_ code: String) {68 NSPasteboard.general.clearContents()69 NSPasteboard.general.setString(code, forType: .string)70 runtime.fire("copy", on: o.id, value: .string(code))71 copied = true72 Task {73 try? await Task.sleep(for: .seconds(1.5))74 copied = false75 }76 }77}7879/// A quotation with an accent bar and an optional author.80struct QuoteComponent: View {81 @Environment(AppRuntime.self) private var runtime82 @Environment(\.prismTheme) private var theme83 let o: PrismObject8485 var body: some View {86 let author = runtime.string(o.props["author"])87 HStack(alignment: .top, spacing: 12) {88 RoundedRectangle(cornerRadius: 2).fill(theme.mark(FA.tone(runtime, o, default: "accent"))).frame(width: 4).accessibilityHidden(true)89 VStack(alignment: .leading, spacing: 6) {90 Text(runtime.string(o.props["text"])).font(.title3).italic().fixedSize(horizontal: false, vertical: true).textSelection(.enabled)91 if !author.isEmpty { Text("— \(author)").font(.callout).foregroundStyle(.secondary) }92 }93 }94 .fixedSize(horizontal: false, vertical: true)95 .accessibilityElement(children: .combine)96 }97}9899/// Text with inline Markdown: bold, italic, code, strikethrough and web links.100struct MarkdownComponent: View {101 @Environment(AppRuntime.self) private var runtime102 @Environment(\.prismTheme) private var theme103 let o: PrismObject104105 var body: some View {106 Text(FamilyAFormat.markdown(runtime.string(o.props["text"])))107 .font(FA.font(o.props["style"]?.stringValue))108 .foregroundStyle(theme.color(FA.tone(runtime, o)))109 .fixedSize(horizontal: false, vertical: true)110 .textSelection(.enabled)111 // Links in the text go through the same gate as `prism.link`: http and https only.112 .environment(\.openURL, OpenURLAction { url in FamilyALink.open(url.absoluteString) ? .handled : .discarded })113 }114}115116/// A link that opens an http or https address in the default browser.117struct LinkComponent: View {118 @Environment(AppRuntime.self) private var runtime119 let o: PrismObject120121 var body: some View {122 let raw = runtime.string(o.props["url"])123 let accepted = FamilyALink.sanitized(raw)124 let title = runtime.string(o.props["title"])125 let symbol = runtime.string(o.props["symbol"])126 Button {127 if FamilyALink.open(raw) { runtime.fire("open", on: o.id, value: .string(accepted?.absoluteString ?? raw)) }128 } label: {129 if symbol.isEmpty { Text(title.isEmpty ? raw : title) } else { Label(title.isEmpty ? raw : title, systemImage: SymbolName.safe(symbol)) }130 }131 .buttonStyle(.link)132 .disabled(accepted == nil)133 .help(accepted.map { $0.absoluteString } ?? "Only http and https links can be opened.")134 .accessibilityHint(accepted == nil ? "This address cannot be opened." : "Opens in your browser.")135 }136}137138/// A keyboard shortcut drawn as keycaps.139struct ShortcutComponent: View {140 @Environment(AppRuntime.self) private var runtime141 let o: PrismObject142143 var body: some View {144 let keys = FA.list(runtime, o.props["keys"]).map(\.displayString).filter { !$0.isEmpty }145 let label = runtime.string(o.props["label"])146 HStack(spacing: 8) {147 if !label.isEmpty { Text(label).font(.callout) }148 HStack(spacing: 3) {149 ForEach(Array(keys.enumerated()), id: \.offset) { _, key in150 Text(FamilyAFormat.keycap(key)).font(.system(.callout, design: .rounded).weight(.medium))151 .padding(.horizontal, 6).padding(.vertical, 2).frame(minWidth: 24)152 .background(.quaternary.opacity(0.6), in: RoundedRectangle(cornerRadius: 5))153 .overlay(RoundedRectangle(cornerRadius: 5).strokeBorder(.separator))154 }155 }156 }157 .accessibilityElement(children: .ignore)158 .accessibilityLabel(label.isEmpty ? "Keyboard shortcut" : label)159 .accessibilityValue(keys.map(FamilyAFormat.keyName).joined(separator: " "))160 }161}162163/// A symbol followed by text.164struct LabelComponent: View {165 @Environment(AppRuntime.self) private var runtime166 @Environment(\.prismTheme) private var theme167 let o: PrismObject168169 var body: some View {170 let symbol = runtime.string(o.props["symbol"])171 HStack(alignment: .firstTextBaseline, spacing: 6) {172 if !symbol.isEmpty { Image(systemName: SymbolName.safe(symbol)).accessibilityHidden(true) }173 Text(runtime.string(o.props["text"])).fixedSize(horizontal: false, vertical: true)174 }175 .font(FA.font(o.props["style"]?.stringValue))176 .foregroundStyle(theme.color(FA.tone(runtime, o)))177 }178}179180/// A path of ancestors; every item but the last is a button.181struct BreadcrumbComponent: View {182 @Environment(AppRuntime.self) private var runtime183 let o: PrismObject184185 var body: some View {186 let items = FA.list(runtime, o.props["items"])187 let connected = FA.connected(o, "click")188 HStack(spacing: 4) {189 ForEach(Array(items.enumerated()), id: \.offset) { index, item in190 let title = FA.field(runtime, item, "title", plainAs: "title")191 if index > 0 { Image(systemName: "chevron.right").font(.caption2.weight(.semibold)).foregroundStyle(.tertiary).accessibilityHidden(true) }192 if index == items.count - 1 {193 Text(title).font(.callout.weight(.semibold)).lineLimit(1).accessibilityAddTraits(.isSelected)194 } else {195 // The payload is the item's `value` when it has one, its position otherwise.196 Button(title) { runtime.fire("click", on: o.id, value: item["value"].map { runtime.resolve($0) } ?? .int(index)) }197 .buttonStyle(.link).font(.callout).lineLimit(1)198 .disabled(!connected).help(connected ? "" : "No action is connected to this breadcrumb yet.")199 }200 }201 }202 .accessibilityElement(children: .contain)203 .accessibilityLabel("Breadcrumb")204 }205}206