SPB Git

Repository Public

BlockShared.swift 8.7 KB 207 lines swift

1// SP Notes Core2// File: Sources/SPNotesFeatures/Blocks/BlockShared.swift3// Description: What the block detail views share (they are opened by the editors' atom cards in a sheet, an inspector or4//              a window): the detail container with its title row, the monospaced source editor, a section label, the5//              image loader of attachments and the decoding of image data on both platforms.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import SPNotesDesign10import SwiftUI11#if canImport(AppKit)12import AppKit13#elseif canImport(UIKit)14import UIKit15#endif1617/// Gives the bytes of an attachment (`/api/files/<id>`): the host answers from its attachment files, downloading when18/// needed. Throwing a `RepositoryError.offline` (or any error) shows the calm unavailable state.19public typealias BlockImageLoader = @Sendable (_ attachmentId: String) async throws -> Data2021/// Container of a block detail: icon + title + trailing accessory, then the scrolling content at reading width.22public struct BlockDetailContainer<Accessory: View, Content: View>: View {23    private let title: String24    private let systemImage: String25    private let subtitle: String?26    private let accessory: () -> Accessory27    private let content: (FeatureSizeClass) -> Content2829    public init(title: String, systemImage: String, subtitle: String? = nil, @ViewBuilder accessory: @escaping () -> Accessory,30                @ViewBuilder content: @escaping (FeatureSizeClass) -> Content) {31        self.title = title32        self.systemImage = systemImage33        self.subtitle = subtitle34        self.accessory = accessory35        self.content = content36    }3738    public var body: some View {39        FeatureLayoutReader { sizeClass in40            let inset = sizeClass.isCompact ? NVSpace.lg : NVSpace.xl41            VStack(alignment: .leading, spacing: 0) {42                HStack(alignment: .center, spacing: NVSpace.md) {43                    Image(systemName: systemImage)44                        .font(.system(size: 17, weight: .medium))45                        .foregroundStyle(NVColor.accent)46                        .frame(width: 36, height: 36)47                        .background(NVColor.accentSoft, in: NVRadius.shape(NVRadius.md))48                        .accessibilityHidden(true)49                    VStack(alignment: .leading, spacing: NVSpace.xxs) {50                        Text(title).nvFont(.h3).foregroundStyle(NVColor.fg).lineLimit(2).accessibilityAddTraits(.isHeader)51                        if let subtitle, !subtitle.isEmpty {52                            Text(subtitle).nvFont(.ui).foregroundStyle(NVColor.fgMuted).lineLimit(2)53                        }54                    }55                    Spacer(minLength: NVSpace.sm)56                    accessory()57                }58                .padding(.horizontal, inset)59                .padding(.top, inset)60                .padding(.bottom, NVSpace.md)61                Rectangle().fill(NVColor.line).frame(height: 1)62                ScrollView {63                    content(sizeClass)64                        .padding(inset)65                        .frame(maxWidth: .infinity, alignment: .topLeading)66                }67            }68            .background(NVColor.bg)69        }70    }71}7273public extension BlockDetailContainer where Accessory == EmptyView {74    init(title: String, systemImage: String, subtitle: String? = nil, @ViewBuilder content: @escaping (FeatureSizeClass) -> Content) {75        self.init(title: title, systemImage: systemImage, subtitle: subtitle, accessory: { EmptyView() }, content: content)76    }77}7879/// Small uppercase-free section label of a block detail.80struct BlockSectionLabel: View {81    let text: String82    var body: some View {83        Text(text).nvFont(.uiStrong).foregroundStyle(NVColor.fgMuted).accessibilityAddTraits(.isHeader)84    }85}8687/// A calm inline notice (information, not an alarm).88struct BlockNotice: View {89    let systemImage: String90    let text: String91    var tone: NVTone = .neutral9293    var body: some View {94        HStack(alignment: .firstTextBaseline, spacing: NVSpace.sm) {95            Image(systemName: systemImage).foregroundStyle(tone == .neutral ? NVColor.fgMuted : tone.foreground).accessibilityHidden(true)96            Text(text).nvFont(.ui).foregroundStyle(NVColor.fgMuted).fixedSize(horizontal: false, vertical: true)97            Spacer(minLength: 0)98        }99        .padding(NVSpace.md)100        .background(NVColor.bgSunken, in: NVRadius.shape(NVRadius.md))101        .overlay(NVRadius.shape(NVRadius.md).strokeBorder(NVColor.line, lineWidth: 1))102        .accessibilityElement(children: .combine)103    }104}105106/// A failure of a user action inside a block detail, dismissible.107struct BlockActionFailure: View {108    let failure: FeatureFailure109    let dismiss: () -> Void110111    var body: some View {112        HStack(alignment: .firstTextBaseline, spacing: NVSpace.sm) {113            Image(systemName: failure.isOffline ? "wifi.slash" : "exclamationmark.triangle")114                .foregroundStyle(failure.isOffline ? NVColor.fgMuted : NVColor.danger)115                .accessibilityHidden(true)116            Text(failure.message).nvFont(.ui).foregroundStyle(failure.isOffline ? NVColor.fgMuted : NVColor.dangerText)117                .fixedSize(horizontal: false, vertical: true)118            Spacer(minLength: NVSpace.sm)119            Button(action: dismiss) { Image(systemName: "xmark").foregroundStyle(NVColor.fgMuted).frame(minWidth: 28, minHeight: 28).contentShape(Rectangle()) }120                .buttonStyle(.plain)121                .accessibilityLabel(L("Fermer le message"))122        }123        .padding(NVSpace.md)124        .background(NVColor.bgSunken, in: NVRadius.shape(NVRadius.md))125        .overlay(NVRadius.shape(NVRadius.md).strokeBorder(NVColor.line, lineWidth: 1))126    }127}128129/// Monospaced multi-line source editor (code of a diagram, of a runnable block).130struct BlockCodeEditor: View {131    @Binding var text: String132    let label: String133    var minHeight: CGFloat = 140134135    var body: some View {136        TextEditor(text: $text)137            .font(NVFont.fixed(.code))138            .foregroundStyle(NVColor.fg)139            .scrollContentBackground(.hidden)140            .autocorrectionDisabled()141            #if os(iOS)142            .textInputAutocapitalization(.never)143            .keyboardType(.asciiCapable)144            #endif145            .padding(NVSpace.sm)146            .frame(minHeight: minHeight)147            .background(NVColor.bgSunken, in: NVRadius.shape(NVRadius.md))148            .overlay(NVRadius.shape(NVRadius.md).strokeBorder(NVColor.line, lineWidth: 1))149            .accessibilityLabel(label)150    }151}152153/// Read-only monospaced text (an output, a source that is not being edited), selectable.154struct BlockCodeText: View {155    let text: String156    var tone: NVTone = .neutral157158    var body: some View {159        Text(text.isEmpty ? " " : text)160            .font(NVFont.fixed(.code))161            .foregroundStyle(tone == .danger ? NVColor.dangerText : NVColor.fg)162            .textSelection(.enabled)163            .frame(maxWidth: .infinity, alignment: .leading)164            .padding(NVSpace.md)165            .background(NVColor.bgSunken, in: NVRadius.shape(NVRadius.md))166            .overlay(NVRadius.shape(NVRadius.md).strokeBorder(tone == .danger ? NVColor.danger.opacity(0.4) : NVColor.line, lineWidth: 1))167    }168}169170/// A single-line text field in the visual system.171struct BlockTextField: View {172    let placeholder: String173    @Binding var text: String174    var onSubmit: () -> Void = {}175176    var body: some View {177        TextField(placeholder, text: $text)178            .textFieldStyle(.plain)179            .nvFont(.body)180            .foregroundStyle(NVColor.fg)181            .padding(.horizontal, NVSpace.md)182            .frame(minHeight: 36)183            .background(NVColor.bgElevated, in: NVRadius.shape(NVRadius.sm))184            .overlay(NVRadius.shape(NVRadius.sm).strokeBorder(NVColor.lineStrong, lineWidth: 1))185            .onSubmit(onSubmit)186    }187}188189enum BlockImage {190    /// Decodes PNG / JPEG / … data; `scale` is the pixel density the data was rendered at (2 for the Mermaid previews).191    static func image(from data: Data, scale: CGFloat = 1) -> (image: Image, size: CGSize)? {192        #if canImport(AppKit)193        guard let rep = NSBitmapImageRep(data: data) else { return nil }194        let size = CGSize(width: CGFloat(rep.pixelsWide) / scale, height: CGFloat(rep.pixelsHigh) / scale)195        guard size.width > 0, size.height > 0 else { return nil }196        let image = NSImage(size: size)197        image.addRepresentation(rep)198        return (Image(nsImage: image), size)199        #elseif canImport(UIKit)200        guard let image = UIImage(data: data, scale: scale), image.size.width > 0 else { return nil }201        return (Image(uiImage: image), image.size)202        #else203        return nil204        #endif205    }206}207
Files main
Menu

Theme