SPB Git

Repository Public

FeaturesBlockDetails.swift 5.2 KB 120 lines swift

1// SP Notes iOS2// File: Features/Editor/Blocks/FeaturesBlockDetails.swift3// Description: Block detail screens taken from the shared `SPNotesFeatures` library (SPEC §8), registered above the minimal4//              in-app sheets: chart (live data of a database source), living view (`queryView`), block reference,5//              bibliography (APA, from the research copy) and executable code (read-only output). The editor still writes the6//              node: each view returns the edited block and `BlockDetailContext.commit` saves it. Drawing, PDF annotation and7//              voice notes stay with this app (PencilKit, Pencil, on-device speech).8//9// Author: Simon-Pierre Boucher10// Contact: contact@spboucher.ai11import SPNotesCore12import SPNotesDesign13import SPNotesFeatures14import SwiftUI1516@MainActor17struct FeaturesBlockDetails: BlockDetailProviding {18    /// The block types served by the shared library.19    static let types: Set<String> = [NodeType.chart, NodeType.queryView, NodeType.blockRef, NodeType.bibliography, NodeType.runCode]2021    func canPresent(_ node: PMNode) -> Bool { Self.types.contains(node.type) && node.typed != nil }22    func style(for node: PMNode) -> BlockDetailStyle { .sheet }2324    func view(for node: PMNode, context: BlockDetailContext) -> AnyView {25        AnyView(FeaturesBlockSheet(node: node, context: context))26    }27}2829/// Routes of a block view opened from a note: a page (with its block) goes through the app's routes.30@MainActor31final class BlockFeatureRouter: FeatureRouter {32    let context: BlockDetailContext33    init(context: BlockDetailContext) { self.context = context }34    func openPage(id: String, blockId: String?) { context.dismiss(); context.open(.note(id, blockId: blockId)) }35    func openDatabase(id: String) { context.dismiss(); context.open(.note(id)) }36    func openCanvas(pageId: String) { context.dismiss(); context.open(.note(pageId)) }37    func openURL(_ url: URL) { UIApplication.shared.open(url) }38    func presentError(_ error: Error) { context.app.banner = error.localizedDescription }39}4041private struct FeaturesBlockSheet: View {42    let node: PMNode43    let context: BlockDetailContext44    @State private var router: BlockFeatureRouter45    @State private var edited: PMNode?46    @State private var confirmsDelete = false4748    init(node: PMNode, context: BlockDetailContext) {49        self.node = node50        self.context = context51        _router = State(initialValue: BlockFeatureRouter(context: context))52    }5354    var body: some View {55        NavigationStack {56            content57                .background(NVColor.bg)58                .navigationBarTitleDisplayMode(.inline)59                .toolbar {60                    ToolbarItem(placement: .confirmationAction) {61                        Button("OK") {62                            if let edited, edited != node, context.isEditable { context.commit(edited) }63                            context.dismiss()64                        }65                        .fontWeight(.semibold).accessibilityIdentifier("block-sheet-done")66                    }67                    if context.isEditable {68                        ToolbarItem(placement: .bottomBar) {69                            Button(role: .destructive) { confirmsDelete = true } label: { Label("Supprimer le bloc", systemImage: "trash") }70                                .accessibilityIdentifier("block-sheet-delete")71                        }72                    }73                }74                .confirmationDialog("Supprimer ce bloc ?", isPresented: $confirmsDelete, titleVisibility: .visible) {75                    Button("Supprimer", role: .destructive) { context.commit(nil); context.dismiss() }76                }77        }78        .accessibilityIdentifier("features-block-\(node.type)")79    }8081    @ViewBuilder private var content: some View {82        let repositories = context.app.repositories83        switch node.typed {84        case .chart(let block)?:85            ChartBlockView(block: block, repositories: repositories, onChange: { edited = $0.node() })86        case .queryView(let block)?:87            QueryViewBlockView(repositories: repositories, router: router, block: block, hostPageId: context.noteId)88        case .blockRef(let block)?:89            BlockRefView(repositories: repositories, router: router, block: block)90        case .bibliography(let block)?:91            BibliographyHost(block: block, context: context, router: router)92        case .runCode(let block)?:93            RunCodeBlockView(block: block, onChange: { edited = $0.node() })94        default:95            EmptyView()96        }97    }98}99100private struct BibliographyHost: View {101    let block: BibliographyBlock102    let context: BlockDetailContext103    let router: BlockFeatureRouter104    @State private var cited: [String]?105106    var body: some View {107        Group {108            if let cited {109                BibliographyView(repositories: context.app.repositories, router: router, citedIds: cited, style: block.style, title: block.title)110            } else {111                ProgressView()112            }113        }114        .task {115            let page = try? await context.app.store.page(id: context.noteId)116            cited = page.flatMap { try? NoteDocument(json: $0.content) }?.citedReferences() ?? []117        }118    }119}120
Files main
Menu

Theme