Repository Public
1// SP Notes iOS2// File: Features/Editor/Blocks/TextBlockSheets.swift3// Description: Sheets of the two blocks that are source text: `mermaid` (edit the code; the render made by the Web is shown while4// it matches the code, a notice otherwise) and `runCode` (edit language and code, read the last output; never run).5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SPNotesCore9import SPNotesDesign10import SwiftUI1112struct MermaidBlockSheet: View {13 let original: MermaidBlock14 let context: BlockDetailContext15 @State private var block: MermaidBlock16 @State private var preview: UIImage?1718 init(block: MermaidBlock, context: BlockDetailContext) {19 original = block20 self.context = context21 _block = State(initialValue: block)22 }2324 var body: some View {25 BlockSheetScaffold(title: String(localized: "Diagramme Mermaid"), isEditable: context.isEditable, onCancel: context.dismiss,26 onDone: { context.finish(block.node(), original: original.node()) }, onDelete: context.delete) {27 Form {28 Section("Rendu") {29 if block.isPreviewCurrent, let preview {30 Image(uiImage: preview).resizable().scaledToFit().frame(maxWidth: .infinity).accessibilityLabel("Rendu du diagramme")31 } else if block.isPreviewCurrent, block.preview != nil {32 BlockNotice(text: String(localized: "Le rendu n’est pas encore sur cet appareil."), symbol: "arrow.down.circle")33 } else {34 BlockNotice(text: String(localized: "Rendu à mettre à jour depuis le Web"), symbol: "arrow.triangle.2.circlepath")35 .accessibilityIdentifier("mermaid-stale")36 }37 }38 Section("Code") {39 TextEditor(text: Binding(get: { block.code }, set: { if $0 != block.code { block.code = $0 } }))40 .font(.system(.footnote, design: .monospaced))41 .frame(minHeight: 220)42 .autocorrectionDisabled()43 .textInputAutocapitalization(.never)44 .disabled(!context.isEditable)45 .accessibilityIdentifier("mermaid-code")46 }47 }48 }49 .task {50 guard let id = original.preview, await context.app.resolveImage(source: "/api/files/" + id) else { return }51 preview = context.app.image(forSource: "/api/files/" + id)52 }53 }54}5556struct RunCodeBlockSheet: View {57 let original: RunCodeBlock58 let context: BlockDetailContext59 @State private var block: RunCodeBlock6061 init(block: RunCodeBlock, context: BlockDetailContext) {62 original = block63 self.context = context64 _block = State(initialValue: block)65 }6667 private var languages: [String] {68 let known = ["javascript", "typescript", "python", "sql", "bash"]69 return known.contains(block.language) ? known : [block.language] + known70 }7172 var body: some View {73 BlockSheetScaffold(title: String(localized: "Code exécutable"), isEditable: context.isEditable, onCancel: context.dismiss,74 onDone: { context.finish(block.node(), original: original.node()) }, onDelete: context.delete) {75 Form {76 Section {77 Picker("Langage", selection: Binding(get: { block.language }, set: { if $0 != block.language { block.language = $0 } })) {78 ForEach(languages, id: \.self) { Text($0).tag($0) }79 }80 .disabled(!context.isEditable)81 .accessibilityIdentifier("runcode-language")82 TextEditor(text: Binding(get: { block.code }, set: { if $0 != block.code { block.code = $0 } }))83 .font(.system(.footnote, design: .monospaced))84 .frame(minHeight: 200)85 .autocorrectionDisabled()86 .textInputAutocapitalization(.never)87 .disabled(!context.isEditable)88 .accessibilityIdentifier("runcode-code")89 } header: { Text("Code") } footer: {90 Text("Ce code s’exécute sur le Web uniquement, dans un bac à sable. Ici, tu peux le lire et le modifier.")91 }92 if let output = block.output {93 Section("Dernière sortie") {94 Text(output.text.isEmpty ? String(localized: "(vide)") : output.text)95 .font(.system(.footnote, design: .monospaced))96 .foregroundStyle(output.isError ? NVColor.dangerText : NVColor.fg)97 .textSelection(.enabled)98 .accessibilityIdentifier("runcode-output")99 if output.isError { Label("La dernière exécution a échoué", systemImage: "exclamationmark.triangle").nvFont(.ui).foregroundStyle(NVColor.dangerText) }100 if let ms = output.milliseconds { LabeledContent("Durée", value: "\(Int(ms.rounded())) ms") }101 if let date = BlockLabels.date(output.at) { LabeledContent("Exécuté le", value: date) }102 if block.code != original.code {103 BlockNotice(text: String(localized: "Le code a changé depuis cette sortie."), symbol: "exclamationmark.circle")104 }105 }106 }107 }108 }109 }110}111