Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Features/Hosting/FeatureBlockDetails.swift3// Description: The detail sheets of the 3.0 blocks, built from the shared block views of SPNotesFeatures (Mermaid, chart with live data, living view with its native result, block reference, citation picker, bibliography, PDF with annotations, runnable code, voice note with recording and transcription, drawing with the mouse / trackpad ink editor). The views hand back an edited block; this file writes the node into the note (one undo step) — the views never touch the document.4import SwiftUI5import SPNotesCore6import SPNotesDocument7import SPNotesStore8import SPNotesSync9import SPNotesDesign10import SPNotesFeatures1112struct FeaturesBlockDetails: BlockDetailProviding {13 func detail(for node: PMNode, context c: BlockDetailContext) -> AnyView? {14 let repos = c.env.repositories, router = c.router, pageId = c.session.id15 let loader = c.env.attachmentLoader(pageId: pageId)16 switch node.typed {17 case .mermaid(let b):18 return AnyView(FeatureBlockSheet(title: "Diagramme Mermaid", symbol: "point.3.connected.trianglepath.dotted", node: node, context: c) { edit in19 MermaidBlockView(block: b, imageLoader: { id in try await loader.data(id: id) }, onChange: { edit($0.node()) })20 })21 case .chart(let b):22 return AnyView(FeatureBlockSheet(title: "Graphique", symbol: "chart.bar.xaxis", width: 760, node: node, context: c) { edit in23 ChartBlockView(block: b, repositories: repos, onChange: { edit($0.node()) })24 })25 case .runCode(let b):26 return AnyView(FeatureBlockSheet(title: "Code exécutable", symbol: "play.rectangle", node: node, context: c) { edit in RunCodeBlockView(block: b, onChange: { edit($0.node()) }) })27 case .queryView(let b):28 return AnyView(FeatureBlockSheet(title: "Vue dynamique", symbol: "rectangle.stack.badge.play", width: 760, node: node, context: c) { edit in29 QueryViewBlockView(repositories: repos, router: router, block: b, hostPageId: pageId, onChange: { edit($0.node()) })30 })31 case .blockRef(let b): return AnyView(BlockRefDetail(block: b, context: c))32 case .citation(let b): return AnyView(Self.citationPicker(existing: b, context: c))33 case .bibliography(let b):34 return AnyView(FeatureBlockSheet(title: "Bibliographie", symbol: "books.vertical", node: node, context: c) { _ in35 BibliographyView(repositories: repos, router: router, citedIds: c.session.editor.document().citedReferences(), style: b.style, title: b.title)36 })37 case .pdf(let b):38 return AnyView(FeatureBlockSheet(title: b.name.isEmpty ? "PDF" : b.name, symbol: "doc.richtext", width: 860, height: 640, node: node, context: c) { edit in39 PDFBlockView(block: b, onChange: { edit($0.node()) }).featureAttachmentLoader(loader)40 })41 case .audio(let b): return AnyView(AudioDetail(block: b, node: node, context: c))42 case .drawing(let b): return AnyView(DrawingDetail(block: b, context: c))43 case .canvasLink(let b): return AnyView(CanvasLinkSheet(block: b, context: c))44 case nil: return nil45 }46 }4748 /// Reference picker of the library, for a new citation or an existing one (`existing`).49 @MainActor static func citationPicker(existing: CitationInline?, context c: BlockDetailContext) -> some View {50 let current = existing.flatMap { e in e.refId.map { CitationChoice(refId: $0, label: e.label, locator: e.locator) } }51 return VStack(spacing: 0) {52 CitationPickerView(repositories: c.env.repositories, pageId: c.session.id, current: current) { choice in53 if var e = existing { e.refId = choice.refId; e.label = choice.label; e.locator = choice.locator; c.apply(e.node()) }54 else { c.apply(CitationInline(refId: choice.refId, label: choice.label, locator: choice.locator).node()) }55 }56 Divider().overlay(NVColor.line)57 HStack { if existing != nil, c.editable { Button("Retirer de la note", role: .destructive, action: c.remove) }; Spacer(); Button("Annuler", role: .cancel, action: c.dismiss).keyboardShortcut(.cancelAction) }.padding(NVSpace.md)58 }59 .frame(width: 560, height: 520).accessibilityIdentifier("citation-picker")60 }61}6263/// Frame of a block sheet around a library view: the view reports edits, « Terminé » writes the last one into the note.64struct FeatureBlockSheet<Content: View>: View {65 let title: String, symbol: String66 var width: CGFloat = 66067 var height: CGFloat = 56068 let node: PMNode69 let context: BlockDetailContext70 @ViewBuilder var content: (@escaping (PMNode) -> Void) -> Content71 @State private var edited: PMNode?72 var body: some View {73 VStack(spacing: 0) {74 content { edited = $0 }.frame(maxWidth: .infinity, maxHeight: .infinity)75 Divider().overlay(NVColor.line)76 HStack {77 if context.editable { Button("Retirer de la note", role: .destructive, action: context.remove) }78 Spacer()79 Button(edited == nil ? "Fermer" : "Annuler", role: .cancel, action: context.dismiss).keyboardShortcut(.cancelAction)80 if context.editable { Button("Terminé") { context.apply(edited ?? node) }.buttonStyle(.borderedProminent).keyboardShortcut(.defaultAction).disabled(edited == nil).accessibilityIdentifier("block-done") }81 }.padding(NVSpace.md)82 }83 .frame(width: width, height: height).background(NVColor.bg)84 .accessibilityIdentifier("block-sheet-" + node.type)85 }86}8788/// A referenced block: its content read from the local copy (library view), and this app's picker to point elsewhere.89private struct BlockRefDetail: View {90 let block: BlockRefBlock91 let context: BlockDetailContext92 @State private var choosing = false93 var body: some View {94 if choosing || block.pageId == nil { BlockRefSheet(block: block, context: context) }95 else {96 VStack(spacing: 0) {97 BlockRefView(repositories: context.env.repositories, router: context.router, block: block).frame(maxWidth: .infinity, maxHeight: .infinity)98 Divider().overlay(NVColor.line)99 HStack {100 if context.editable { Button("Retirer de la note", role: .destructive, action: context.remove); Button("Choisir un autre bloc…") { choosing = true } }101 Spacer(); Button("Fermer", role: .cancel, action: context.dismiss).keyboardShortcut(.cancelAction)102 }.padding(NVSpace.md)103 }.frame(width: 640, height: 480).background(NVColor.bg).accessibilityIdentifier("block-sheet-blockRef")104 }105 }106}107108/// Audio files of a note: played from this Mac; a fresh recording is stored here first and uploaded by the synchronisation.109struct NoteAudioFiles: AudioFileProvider {110 let env: AppEnvironment111 let pageId: String112 func localFileURL(attachmentId: String) async throws -> URL { try await AttachmentService(env: env).localURL(attachmentId: attachmentId) }113 func adopt(recording: URL, pageId: String?) async throws -> String? { try await AttachmentService(env: env).importFile(recording, pageId: pageId ?? self.pageId).id }114}115116/// Voice note: player, transcript with seek, recording (microphone permission asked at the first recording) and the117/// explicit, paid transcription request — all in the library view. Every change of the block is written into the note.118struct AudioDetail: View {119 let block: AudioBlock?120 let node: PMNode?121 let context: BlockDetailContext122 @State private var model: AudioBlockModel?123 @State private var latest: PMNode?124 var body: some View {125 VStack(spacing: 0) {126 Group { if let model { AudioBlockView(model: model) } else { ProgressView() } }.frame(maxWidth: .infinity, maxHeight: .infinity)127 Divider().overlay(NVColor.line)128 HStack {129 if context.editable, node != nil { Button("Retirer de la note", role: .destructive, action: context.remove) }130 Spacer()131 Button(latest == nil ? "Fermer" : "Annuler", role: .cancel, action: context.dismiss).keyboardShortcut(.cancelAction)132 if context.editable { Button(node == nil ? "Insérer" : "Terminé") { if let latest { context.apply(latest) } else { context.dismiss() } }.buttonStyle(.borderedProminent).keyboardShortcut(.defaultAction).disabled(latest == nil).accessibilityIdentifier("block-done") }133 }.padding(NVSpace.md)134 }135 .frame(width: 620, height: 560).background(NVColor.bg)136 .onAppear { if model == nil { model = AudioBlockModel(repositories: context.env.repositories, block: block, pageId: context.session.id, files: NoteAudioFiles(env: context.env, pageId: context.session.id), onChange: { latest = $0.node() }) } }137 .accessibilityIdentifier("block-sheet-audio")138 }139}140141/// Drawing: the ink as it was traced, and the library's mouse / trackpad editor. Saving writes the ink (`spn-ink-1`) and142/// its PNG preview as attachments of the note, clears `pk` (the ink changed without PencilKit) and updates the node.143struct DrawingDetail: View {144 let block: DrawingBlock145 let context: BlockDetailContext146 @State private var editing: InkDocument?147 @State private var failure: String?148 @State private var saving = false149 var body: some View {150 let loader = context.env.attachmentLoader(pageId: context.session.id)151 VStack(spacing: 0) {152 if let doc = editing {153 InkEditorView(document: doc, background: block.background, canSave: !saving, onCancel: { editing = nil }, onSave: { ink, _ in save(ink, loader: loader) })154 } else {155 DrawingBlockViewer(block: block, onEdit: context.editable ? { editing = $0 } : nil).featureAttachmentLoader(loader).frame(maxWidth: .infinity, maxHeight: .infinity)156 if let failure { Label(failure, systemImage: "exclamationmark.triangle").nvFont(.ui).foregroundStyle(NVColor.dangerText).padding(NVSpace.sm) }157 Divider().overlay(NVColor.line)158 HStack {159 if context.editable { Button("Retirer de la note", role: .destructive, action: context.remove); Button("Dessiner…") { editing = InkDocument(width: block.width > 0 ? block.width : 800, height: block.height > 0 ? block.height : 500) }.accessibilityIdentifier("drawing-edit") }160 Spacer(); Button("Fermer", role: .cancel, action: context.dismiss).keyboardShortcut(.cancelAction)161 }.padding(NVSpace.md)162 }163 }164 .frame(width: 820, height: 620).background(NVColor.bg)165 .accessibilityIdentifier("block-sheet-drawing")166 }167 private func save(_ ink: InkDocument, loader: FeatureAttachmentLoader) {168 saving = true169 Task {170 defer { saving = false }171 do { let updated = try await InkDrawingExport.store(ink, into: block, pageId: context.session.id, loader: loader); context.apply(updated.node()) }172 catch { failure = FeatureFailure(error).message; editing = nil }173 }174 }175}176