SPB Git

Repository Public

CanvasNodeViews.swift 9.8 KB 209 lines swift

1// SP Notes Core2// File: Sources/SPNotesFeatures/Canvas/CanvasNodeViews.swift3// Description: The cards of the canvas, one native rendering per node kind of `spn-canvas-1` (text, page, image, pdf,4//              link, drawing, shape, reference, math, table) and a neutral read-only card for kinds this build does not5//              know. Drawn at canvas scale (the surface scales them with the zoom).6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import SPNotesDesign10import SPNotesDocument11import SwiftUI1213enum CanvasPaint {14    /// The colour of a card (`#rrggbb`), nil when it has none or it does not parse.15    static func color(_ hex: String?) -> Color? { hex.flatMap(NVRGB.init(hexString:))?.color }16}1718/// One card, sized by its caller to the node's `w` × `h`.19struct CanvasNodeCard: View {20    let node: CanvasNode21    let title: String?22    let media: CanvasMedia?2324    var body: some View {25        Group {26            switch node.kind {27            case .shape: shape28            case .text: framed { text }29            case .page: framed { linked(symbol: "doc.text", caption: L("Page"), title: title ?? CanvasNodeLabel.label(node)) }30            case .reference: framed { linked(symbol: "quote.opening", caption: L("Référence"), title: title ?? CanvasNodeLabel.label(node)) }31            case .link: framed { link }32            case .image: framed(padding: 0) { picture(placeholder: "photo", caption: L("Image")) }33            case .pdf: framed(padding: 0) { picture(placeholder: "doc.richtext", caption: L("PDF")) }34            case .drawing: framed(padding: 0) { drawing }35            case .math: framed { math }36            case .table: framed(padding: 0) { table }37            default: framed { unknown }38            }39        }40        .accessibilityElement(children: .ignore)41        .accessibilityLabel(CanvasNodeLabel.kindName(node.kind) + " : " + (title ?? CanvasNodeLabel.label(node)))42    }4344    private var tint: Color? { CanvasPaint.color(node.color) }4546    private func framed<Content: View>(padding: CGFloat = NVSpace.md, @ViewBuilder _ content: () -> Content) -> some View {47        content()48            .padding(padding)49            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)50            .background(NVColor.bgElevated)51            .overlay(alignment: .top) { if let tint { Rectangle().fill(tint).frame(height: 4) } }52            .clipShape(NVRadius.shape(NVRadius.md))53            .overlay(NVRadius.shape(NVRadius.md).strokeBorder(tint?.opacity(0.7) ?? NVColor.lineStrong, lineWidth: 1))54            .shadow(color: .black.opacity(0.06), radius: 2, y: 1)55    }5657    private var text: some View {58        Text(node.text?.isEmpty == false ? node.text! : L("Texte"))59            .font(NVFont.fixed(.ui))60            .foregroundStyle(node.text?.isEmpty == false ? NVColor.fg : NVColor.fgFaint)61            .multilineTextAlignment(.leading)62    }6364    private func linked(symbol: String, caption: String, title: String) -> some View {65        VStack(alignment: .leading, spacing: NVSpace.xs) {66            HStack(spacing: NVSpace.xs) {67                Image(systemName: symbol).font(.system(size: 11, weight: .semibold))68                Text(caption.uppercased()).font(NVFont.fixed(.micro)).kerning(0.6)69            }70            .foregroundStyle(tint ?? NVColor.accent)71            Text(title).font(NVFont.font(.h3, size: 16)).foregroundStyle(NVColor.fg).lineLimit(3)72            if let text = node.text, !text.isEmpty, text != title {73                Text(text).font(NVFont.fixed(.micro)).foregroundStyle(NVColor.fgMuted).lineLimit(2)74            }75        }76    }7778    private var link: some View {79        VStack(alignment: .leading, spacing: NVSpace.xs) {80            HStack(spacing: NVSpace.xs) {81                Image(systemName: "link").font(.system(size: 11, weight: .semibold))82                Text((URL(string: node.url ?? "")?.host ?? L("Lien")).uppercased()).font(NVFont.fixed(.micro)).kerning(0.6).lineLimit(1)83            }84            .foregroundStyle(tint ?? NVColor.accent)85            Text(CanvasNodeLabel.label(node)).font(NVFont.fixed(.uiStrong)).foregroundStyle(NVColor.fg).lineLimit(2)86            if let url = node.url, !url.isEmpty {87                Text(url).font(NVFont.fixed(.micro)).foregroundStyle(NVColor.fgMuted).lineLimit(1).truncationMode(.middle)88            }89        }90    }9192    @ViewBuilder93    private func picture(placeholder: String, caption: String) -> some View {94        if case .image(let image)? = media {95            Image(decorative: image, scale: 1).resizable().scaledToFill()96                .frame(maxWidth: .infinity, maxHeight: .infinity)97                .clipped()98                .overlay(alignment: .bottomLeading) {99                    if let text = node.text, !text.isEmpty {100                        Text(text).font(NVFont.fixed(.micro)).foregroundStyle(NVColor.fg).lineLimit(1)101                            .padding(.horizontal, NVSpace.sm).padding(.vertical, NVSpace.xxs)102                            .background(NVColor.bgElevated.opacity(0.9), in: NVRadius.shape(NVRadius.xs))103                            .padding(NVSpace.xs)104                    }105                }106        } else {107            VStack(spacing: NVSpace.sm) {108                Image(systemName: placeholder).font(.system(size: 28, weight: .light)).foregroundStyle(NVColor.fgFaint)109                Text(node.text?.isEmpty == false ? node.text! : caption).font(NVFont.fixed(.micro)).foregroundStyle(NVColor.fgMuted).lineLimit(2)110                if case .unavailable? = media { Text(L("Fichier non disponible hors ligne")).font(NVFont.fixed(.micro)).foregroundStyle(NVColor.fgMuted) }111            }112            .padding(NVSpace.md)113            .frame(maxWidth: .infinity, maxHeight: .infinity)114            .background(NVColor.bgSunken)115        }116    }117118    @ViewBuilder119    private var drawing: some View {120        if case .ink(let ink)? = media {121            InkCanvasView(document: ink).padding(NVSpace.xs)122        } else {123            picture(placeholder: "scribble.variable", caption: L("Dessin"))124        }125    }126127    private var math: some View {128        VStack(alignment: .leading, spacing: NVSpace.xs) {129            Text("LATEX").font(NVFont.fixed(.micro)).kerning(0.6).foregroundStyle(NVColor.fgMuted)130            Text(node.latex?.isEmpty == false ? node.latex! : (node.text ?? ""))131                .font(NVFont.fixed(.code))132                .foregroundStyle(NVColor.fg)133                .frame(maxWidth: .infinity, alignment: .leading)134        }135    }136137    private var table: some View {138        let cells = CanvasTable.parse(node.text)139        return Group {140            if cells.isEmpty {141                Text(L("Tableau vide")).font(NVFont.fixed(.micro)).foregroundStyle(NVColor.fgMuted).padding(NVSpace.md)142            } else {143                Grid(alignment: .leading, horizontalSpacing: 0, verticalSpacing: 0) {144                    ForEach(Array(cells.enumerated()), id: \.offset) { rowIndex, row in145                        GridRow {146                            ForEach(Array(row.enumerated()), id: \.offset) { _, cell in147                                Text(cell.isEmpty ? " " : cell)148                                    .font(NVFont.fixed(rowIndex == 0 ? .uiStrong : .ui))149                                    .foregroundStyle(NVColor.fg)150                                    .lineLimit(2)151                                    .padding(.horizontal, NVSpace.sm).padding(.vertical, NVSpace.xs)152                                    .frame(maxWidth: .infinity, alignment: .leading)153                                    .background(rowIndex == 0 ? NVColor.bgSunken : .clear)154                                    .overlay(alignment: .bottom) { Rectangle().fill(NVColor.line).frame(height: 1) }155                                    .overlay(alignment: .trailing) { Rectangle().fill(NVColor.line).frame(width: 1) }156                            }157                        }158                    }159                }160            }161        }162        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)163    }164165    private var unknown: some View {166        VStack(alignment: .leading, spacing: NVSpace.xs) {167            HStack(spacing: NVSpace.xs) {168                Image(systemName: "questionmark.square.dashed")169                Text(LF("Carte (%@)", node.kind.rawValue)).font(NVFont.fixed(.uiStrong))170            }171            .foregroundStyle(NVColor.fgMuted)172            if let text = node.text, !text.isEmpty { Text(text).font(NVFont.fixed(.ui)).foregroundStyle(NVColor.fg).lineLimit(3) }173            Text(L("Type inconnu de cette version : conservé tel quel, en lecture seule.")).font(NVFont.fixed(.micro)).foregroundStyle(NVColor.fgMuted)174        }175    }176177    private var shape: some View {178        let stroke = tint ?? NVColor.accent179        return ZStack {180            CanvasOutline(shape: node.shape ?? .rect).fill(stroke.opacity(0.12))181            CanvasOutline(shape: node.shape ?? .rect).stroke(stroke, lineWidth: 1.5)182            if let text = node.text, !text.isEmpty {183                Text(text).font(NVFont.fixed(.uiStrong)).foregroundStyle(NVColor.fg).multilineTextAlignment(.center)184                    .padding(node.shape == .diamond ? NVSpace.xl : NVSpace.md)185            }186        }187    }188}189190/// The outline of a `shape` node (unknown shapes draw as rectangles).191struct CanvasOutline: Shape {192    let shape: CanvasShape193194    func path(in rect: CGRect) -> Path {195        switch shape {196        case .ellipse: return Path(ellipseIn: rect)197        case .diamond:198            var path = Path()199            path.move(to: CGPoint(x: rect.midX, y: rect.minY))200            path.addLine(to: CGPoint(x: rect.maxX, y: rect.midY))201            path.addLine(to: CGPoint(x: rect.midX, y: rect.maxY))202            path.addLine(to: CGPoint(x: rect.minX, y: rect.midY))203            path.closeSubpath()204            return path205        default: return Path(roundedRect: rect, cornerRadius: NVRadius.sm)206        }207    }208}209
Files main
Menu

Theme