Repository Public
1// ORIX DJ2// File: App/Sources/Features/Docs/DocsCodeBlock.swift3// Description: Fenced code block of the documentation. ORIX Script blocks are coloured by the real lexer and can be4// copied, opened in the editor or run in the simulator; "mistake" blocks show the compiler's own message.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import ORIXDJCore9import SwiftUI1011struct DocsCodeBlock: View {12 enum Kind { case script, mistake, plain }1314 @Environment(AppModel.self) private var model15 var language: String16 var source: String17 var style: MarkdownStyle1819 @State private var copied = false20 private var cache: ScriptRunCache { ScriptRunCache.shared }2122 private var kind: Kind {23 switch language {24 case "orixdj", "orix", "orixscript": .script25 case "orixdj-error": .mistake26 default: .plain27 }28 }2930 private var mistakeKey: String { "mistake\u{0}" + source }3132 var body: some View {33 VStack(alignment: .leading, spacing: 0) {34 header35 Rectangle().fill(ORIX.Palette.hairline).frame(height: 1)36 ScrollView(.horizontal, showsIndicators: false) {37 Group {38 if kind == .plain {39 Text(verbatim: source).font(.system(size: 12 * style.scale, design: .monospaced)).foregroundStyle(ORIX.Palette.textPrimary)40 } else {41 Text(ScriptHighlighter.attributed(source, size: 12 * style.scale))42 }43 }44 .lineSpacing(2.5 * style.scale)45 .textSelection(.enabled)46 .fixedSize(horizontal: true, vertical: true)47 .padding(.horizontal, 12).padding(.vertical, 10)48 }49 .accessibilityLabel(kind == .plain ? Text("Text block") : Text("ORIX Script code"))50 .accessibilityValue(Text(verbatim: source))51 footer52 }53 .background(ORIX.Palette.well.opacity(0.55), in: RoundedRectangle(cornerRadius: ORIX.Radius.control, style: .continuous))54 .overlay(RoundedRectangle(cornerRadius: ORIX.Radius.control, style: .continuous).strokeBorder(kind == .mistake ? ORIX.Palette.danger.opacity(0.55) : ORIX.Palette.hairline, lineWidth: 1))55 .frame(maxWidth: 900 * style.scale, alignment: .leading)56 .task(id: source) {57 if kind == .mistake, cache.outcome(for: mistakeKey) == nil { await compileMistake() }58 }59 .accessibilityElement(children: .contain)60 .accessibilityIdentifier(kind == .script ? "docs-code-script" : (kind == .mistake ? "docs-code-mistake" : "docs-code-plain"))61 }6263 private var header: some View {64 HStack(spacing: 8) {65 switch kind {66 case .script: Label("ORIX Script", systemImage: "chevron.left.forwardslash.chevron.right").foregroundStyle(ORIX.Palette.textSecondary)67 case .mistake: Label("Example of a mistake", systemImage: "exclamationmark.triangle.fill").foregroundStyle(ORIX.Palette.danger)68 case .plain: Text(verbatim: language.isEmpty ? "TEXT" : language.uppercased()).foregroundStyle(ORIX.Palette.textTertiary)69 }70 Spacer(minLength: 8)71 if kind == .script, style.scriptActions {72 Button { cache.run(source) } label: { Label("Run in Simulator", systemImage: "play.fill") }73 .disabled(cache.isRunning(source))74 .help("Compiles this example and runs it in the offline simulator. Nothing reaches the decks.")75 .accessibilityIdentifier("docs-code-run")76 Button { model.openInEditor(name: automationName, source: ScriptOutcome.complete(source)) } label: { Label("Open in Editor", systemImage: "square.and.pencil") }77 .help("Opens this example in a new tab of the Automation editor.")78 .accessibilityIdentifier("docs-code-open")79 }80 Button {81 copyToPasteboard(source); copied = true82 Task { try? await Task.sleep(nanoseconds: 1_600_000_000); copied = false }83 } label: { Label(copied ? "Copied" : "Copy", systemImage: copied ? "checkmark" : "doc.on.doc") }84 .accessibilityIdentifier("docs-code-copy")85 }86 .font(.system(size: 11, weight: .medium))87 .labelStyle(.titleAndIcon)88 .buttonStyle(.borderless)89 .controlSize(.small)90 .padding(.horizontal, 12).frame(minHeight: 30)91 }9293 private var automationName: String {94 if let r = source.range(of: #"automation\s+"([^"]*)""#, options: .regularExpression) {95 let match = source[r]96 if let open = match.firstIndex(of: "\""), let close = match.lastIndex(of: "\""), open < close { return String(match[match.index(after: open) ..< close]) }97 }98 return "Example"99 }100101 @ViewBuilder private var footer: some View {102 if kind == .script {103 if cache.isRunning(source) {104 result { HStack(spacing: 8) { ProgressView().controlSize(.small); Text("Compiling and simulating…").font(.system(size: 11 * style.scale)).foregroundStyle(ORIX.Palette.textSecondary) } }105 } else if let outcome = cache.outcome(for: source) {106 result {107 VStack(alignment: .leading, spacing: 6) {108 HStack {109 SectionLabel("Simulator result")110 if outcome.wrapped { Text("Run inside automation \"Example\"").font(.system(size: 10.5 * style.scale)).foregroundStyle(ORIX.Palette.textTertiary) }111 Spacer()112 Button("Dismiss") { cache.clear(source) }.buttonStyle(.borderless).controlSize(.small).font(.system(size: 11))113 .accessibilityIdentifier("docs-code-result-dismiss")114 }115 if !outcome.errors.isEmpty {116 Label("Does not compile", systemImage: "xmark.octagon.fill").font(.system(size: 12 * style.scale, weight: .semibold)).foregroundStyle(ORIX.Palette.danger)117 DiagnosticsListView(diagnostics: outcome.errors, scale: style.scale)118 }119 if let sim = outcome.simulation { SimulationSummaryView(simulation: sim, scale: style.scale) }120 if !outcome.warnings.isEmpty { DiagnosticsListView(diagnostics: outcome.warnings, scale: style.scale) }121 }122 }123 .accessibilityIdentifier("docs-code-result")124 }125 } else if kind == .mistake {126 result {127 VStack(alignment: .leading, spacing: 6) {128 SectionLabel("What the compiler says")129 if let outcome = cache.outcome(for: mistakeKey) {130 if !outcome.errors.isEmpty {131 DiagnosticsListView(diagnostics: outcome.errors, scale: style.scale)132 } else if let e = outcome.simulation?.error {133 Text("It compiles, and fails when it runs:").font(.system(size: 11 * style.scale)).foregroundStyle(ORIX.Palette.textSecondary)134 Text(verbatim: e.description).font(.system(size: 12 * style.scale)).foregroundStyle(ORIX.Palette.danger).textSelection(.enabled)135 } else if !outcome.warnings.isEmpty {136 DiagnosticsListView(diagnostics: outcome.warnings, scale: style.scale)137 } else {138 Text("This version of the compiler accepts the block, and it ran in the simulator without an error.")139 .font(.system(size: 12 * style.scale)).foregroundStyle(ORIX.Palette.textSecondary)140 }141 } else {142 ProgressView().controlSize(.small)143 }144 }145 }146 .accessibilityIdentifier("docs-code-mistake-message")147 }148 }149150 private func result<C: View>(@ViewBuilder _ content: () -> C) -> some View {151 VStack(alignment: .leading, spacing: 0) {152 Rectangle().fill(ORIX.Palette.hairline).frame(height: 1)153 content().padding(.horizontal, 12).padding(.vertical, 9).frame(maxWidth: .infinity, alignment: .leading)154 }155 .background(ORIX.Palette.surface.opacity(0.7))156 }157158 private func compileMistake() async {159 let text = source160 let outcome = await Task.detached(priority: .utility) { ScriptOutcome.evaluate(text) }.value161 cache.store(outcome, for: mistakeKey)162 }163}164