Repository Public
1// SP Notes Core2// File: Sources/SPNotesFeatures/Blocks/BlockRefView.swift3// Description: A cross-reference (`blockRef`): `embed` shows the source block read-only with its page as a header,4// `link` a compact line; a missing target shows « Référence introuvable ». A tap opens the source page at5// the block through the router. Usable inline in an editor or as a detail.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import SPNotesDesign10import SPNotesDocument11import SPNotesSync12import SwiftUI1314public struct BlockRefView: View {15 @State private var model: BlockRefModel1617 public init(model: BlockRefModel) { _model = State(initialValue: model) }1819 public init(repositories: NotesRepositories, router: (any FeatureRouter)? = nil, block: BlockRefBlock) {20 self.init(model: BlockRefModel(repositories: repositories, router: router, block: block))21 }2223 public var body: some View {24 Group {25 switch model.resolution {26 case .loading:27 HStack(spacing: NVSpace.sm) { NVProgressDots(); Text(L("Chargement de la référence…")).nvFont(.ui).foregroundStyle(NVColor.fgMuted) }28 .padding(NVSpace.md)29 .frame(maxWidth: .infinity, alignment: .leading)30 case .missing:31 HStack(spacing: NVSpace.sm) {32 Image(systemName: "link.badge.plus").symbolRenderingMode(.hierarchical).foregroundStyle(NVColor.fgMuted).accessibilityHidden(true)33 Text(L("Référence introuvable")).nvFont(.ui).foregroundStyle(NVColor.fgMuted)34 Spacer(minLength: 0)35 }36 .padding(NVSpace.md)37 .frame(maxWidth: .infinity, alignment: .leading)38 .background(NVColor.bgSunken, in: NVRadius.shape(NVRadius.md))39 .overlay(NVRadius.shape(NVRadius.md).strokeBorder(NVColor.line, style: StrokeStyle(lineWidth: 1, dash: [4, 3])))40 .accessibilityElement(children: .combine)41 case .found(let target):42 if model.isLink { link(target) } else { embed(target) }43 }44 }45 .task { if model.resolution == .loading { await model.load() } }46 }4748 private func source(_ target: BlockRefModel.Target) -> some View {49 HStack(spacing: NVSpace.xs) {50 if let icon = target.pageIcon, !icon.isEmpty { Text(icon).accessibilityHidden(true) } else {51 Image(systemName: "doc.text").foregroundStyle(NVColor.accent).accessibilityHidden(true)52 }53 Text(target.pageTitle).nvFont(.uiStrong).foregroundStyle(NVColor.accent).lineLimit(1)54 Image(systemName: "arrow.up.right").font(.system(size: 10, weight: .semibold)).foregroundStyle(NVColor.accent).accessibilityHidden(true)55 }56 }5758 private func link(_ target: BlockRefModel.Target) -> some View {59 Button { model.open() } label: {60 HStack(alignment: .firstTextBaseline, spacing: NVSpace.sm) {61 source(target)62 if let first = target.items.first(where: { !$0.plainText.isEmpty }) {63 Text(first.plainText).nvFont(.ui).foregroundStyle(NVColor.fgMuted).lineLimit(1)64 }65 Spacer(minLength: 0)66 }67 .padding(.horizontal, NVSpace.md)68 .frame(maxWidth: .infinity, minHeight: 36, alignment: .leading)69 .background(NVColor.accentSoft.opacity(0.5), in: NVRadius.shape(NVRadius.sm))70 .contentShape(Rectangle())71 }72 .buttonStyle(.plain)73 .accessibilityLabel(LF("Référence vers %@", target.pageTitle))74 .accessibilityHint(L("Ouvre la page source"))75 }7677 private func embed(_ target: BlockRefModel.Target) -> some View {78 VStack(alignment: .leading, spacing: NVSpace.sm) {79 Button { model.open() } label: {80 HStack(spacing: NVSpace.sm) {81 source(target)82 Spacer(minLength: 0)83 Text(L("Référence")).nvFont(.micro).foregroundStyle(NVColor.fgMuted)84 }85 .frame(minHeight: 28)86 .contentShape(Rectangle())87 }88 .buttonStyle(.plain)89 .accessibilityLabel(LF("Ouvrir la source : %@", target.pageTitle))90 if target.items.isEmpty {91 Text(L("Ce bloc est vide.")).nvFont(.ui).foregroundStyle(NVColor.fgMuted)92 } else {93 BlockRefContent(items: target.items)94 }95 if target.truncated {96 Text(L("Début de la page seulement.")).nvFont(.micro).foregroundStyle(NVColor.fgMuted)97 }98 }99 .padding(NVSpace.md)100 .frame(maxWidth: .infinity, alignment: .leading)101 .background(NVColor.bgElevated, in: NVRadius.shape(NVRadius.md))102 .overlay(alignment: .leading) { Rectangle().fill(NVColor.accent).frame(width: 3).clipShape(NVRadius.shape(2)).padding(.vertical, NVSpace.sm) }103 .overlay(NVRadius.shape(NVRadius.md).strokeBorder(NVColor.line, lineWidth: 1))104 }105}106107/// The read-only rendering of referenced blocks.108public struct BlockRefContent: View {109 private let items: [BlockRefItem]110 public init(items: [BlockRefItem]) { self.items = items }111112 public var body: some View {113 VStack(alignment: .leading, spacing: NVSpace.sm) {114 ForEach(items) { item in row(item) }115 }116 .textSelection(.enabled)117 }118119 @ViewBuilder120 private func row(_ item: BlockRefItem) -> some View {121 HStack(alignment: .firstTextBaseline, spacing: NVSpace.sm) {122 if let icon = item.calloutIcon { Text(icon).accessibilityHidden(true) } else if item.inCallout { Text(" ").hidden() }123 content(item)124 }125 .padding(.leading, item.quoted ? NVSpace.md : 0)126 .overlay(alignment: .leading) { if item.quoted { Rectangle().fill(NVColor.lineStrong).frame(width: 3) } }127 .padding(.leading, CGFloat(item.depth) * NVSpace.lg)128 .padding(item.inCallout ? NVSpace.xs : 0)129 .frame(maxWidth: .infinity, alignment: .leading)130 .background(item.inCallout ? NVColor.bgSunken : Color.clear, in: NVRadius.shape(NVRadius.sm))131 }132133 @ViewBuilder134 private func content(_ item: BlockRefItem) -> some View {135 switch item.kind {136 case .paragraph:137 Text(item.text).nvFont(.body).foregroundStyle(item.quoted ? NVColor.fgMuted : NVColor.fg).fixedSize(horizontal: false, vertical: true)138 case .heading(let level):139 Text(item.text).nvFont(level <= 1 ? .h2 : .h3).foregroundStyle(NVColor.fg).fixedSize(horizontal: false, vertical: true).accessibilityAddTraits(.isHeader)140 case .bullet:141 marker("•"); Text(item.text).nvFont(.body).foregroundStyle(NVColor.fg).fixedSize(horizontal: false, vertical: true)142 case .ordered(let number):143 marker("\(number)."); Text(item.text).nvFont(.body).foregroundStyle(NVColor.fg).fixedSize(horizontal: false, vertical: true)144 case .task(let checked):145 Image(systemName: checked ? "checkmark.square.fill" : "square").foregroundStyle(checked ? NVColor.accent : NVColor.fgMuted)146 .accessibilityLabel(checked ? L("Cochée") : L("Non cochée"))147 Text(item.text).nvFont(.body).foregroundStyle(checked ? NVColor.fgMuted : NVColor.fg).strikethrough(checked).fixedSize(horizontal: false, vertical: true)148 case .code(let language):149 VStack(alignment: .leading, spacing: NVSpace.xs) {150 if !language.isEmpty { Text(language).nvFont(.micro).foregroundStyle(NVColor.fgMuted) }151 Text(item.text).font(NVFont.fixed(.code)).foregroundStyle(NVColor.fg).fixedSize(horizontal: false, vertical: true)152 }153 .padding(NVSpace.sm)154 .frame(maxWidth: .infinity, alignment: .leading)155 .background(NVColor.bgSunken, in: NVRadius.shape(NVRadius.sm))156 case .atom(let type):157 HStack(spacing: NVSpace.sm) {158 Image(systemName: BlockRefContent.symbol(type)).foregroundStyle(NVColor.fgMuted).accessibilityHidden(true)159 Text(item.text).nvFont(.ui).foregroundStyle(NVColor.fgMuted).lineLimit(2)160 Spacer(minLength: 0)161 }162 .padding(NVSpace.sm)163 .frame(maxWidth: .infinity, alignment: .leading)164 .background(NVColor.bgSunken, in: NVRadius.shape(NVRadius.sm))165 .overlay(NVRadius.shape(NVRadius.sm).strokeBorder(NVColor.line, lineWidth: 1))166 case .divider:167 Rectangle().fill(NVColor.line).frame(height: 1).frame(maxWidth: .infinity)168 }169 }170171 private func marker(_ text: String) -> some View {172 Text(text).nvFont(.body).foregroundStyle(NVColor.fgMuted).frame(minWidth: 18, alignment: .trailing).accessibilityHidden(true)173 }174175 static func symbol(_ type: String) -> String {176 switch type {177 case NodeType.table: return "tablecells"178 case NodeType.image: return "photo"179 case NodeType.fileBlock, NodeType.pdf: return "doc"180 case NodeType.blockMath: return "function"181 case NodeType.youtube: return "play.rectangle"182 case NodeType.mermaid: return "point.3.connected.trianglepath.dotted"183 case NodeType.drawing: return "scribble"184 case NodeType.chart: return "chart.bar"185 case NodeType.queryView: return "list.bullet.rectangle"186 case NodeType.audio: return "waveform"187 case NodeType.runCode: return "chevron.left.forwardslash.chevron.right"188 case NodeType.canvasLink: return "rectangle.on.rectangle.angled"189 default: return "square.dashed"190 }191 }192}193