SPB Git

Repository Public

Catalog+Media.swift 5.7 KB 120 lines swift

1// SP Notes Core2// File: Sources/SPNotesFeaturesCatalog/Catalog+Media.swift3// Description: Catalogue entries of the « Media » area: the canvas (sample, empty, with a sync conflict), the ink editor,4//              the drawing viewer, the PDF block and the chart block in its five kinds and fed by a database.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SPNotesDesign9import SPNotesDocument10import SPNotesFeatures11import SwiftUI1213extension FeatureCatalog {14    @MainActor static var mediaEntries: [FeatureCatalogEntry] {15        let area = "Canevas et médias"16        var entries: [FeatureCatalogEntry] = [17            FeatureCatalogEntry("canvas", title: "Canevas", area: area) { world, router in18                CanvasScreen(repositories: world.repositories, pageId: FeatureSamples.canvasPageId, router: router, attachments: FeatureSamples.attachmentLoader(world))19            },20            FeatureCatalogEntry("canvas-empty", title: "Canevas vide", area: area) { world, router in21                CatalogPreparedCanvas(world: world, router: router) { world in try await world.repositories.canvases.create(title: "Nouveau canevas") }22            },23            FeatureCatalogEntry("canvas-conflict", title: "Canevas — conflit", area: area) { world, router in24                CatalogPreparedCanvas(world: world, router: router) { world in try await FeatureSamples.makeCanvasConflict(world) }25            },26            FeatureCatalogEntry("ink-editor", title: "Éditeur d'encre", area: area) { _, _ in27                InkEditorView(document: FeatureSamples.sampleInk(), onCancel: {}, onSave: { _, _ in })28            },29            FeatureCatalogEntry("drawing-viewer", title: "Bloc dessin", area: area) { world, _ in30                CatalogDrawingBlock().featureAttachmentLoader(FeatureSamples.attachmentLoader(world))31            },32            FeatureCatalogEntry("pdf-block", title: "Bloc PDF", area: area) { world, _ in33                PDFBlockView(block: FeatureSamples.samplePDFBlock()).featureAttachmentLoader(FeatureSamples.attachmentLoader(world))34            },35            FeatureCatalogEntry("chart-live", title: "Graphique — base en direct", area: area) { world, _ in36                ChartBlockView(block: FeatureSamples.sampleLiveChart(), repositories: world.repositories)37            },38        ]39        for kind in ChartKind.known {40            entries.append(FeatureCatalogEntry("chart-\(kind.rawValue)", title: "Graphique — \(kind.rawValue)", area: area) { _, _ in41                ChartBlockView(block: FeatureSamples.sampleChart(kind))42            })43        }44        return entries45    }46}4748/// A canvas that needs some preparation in the sample world before it can be shown (creation, conflict).49struct CatalogPreparedCanvas: View {50    let world: FeatureWorld51    let router: any FeatureRouter52    let prepare: @Sendable (FeatureWorld) async throws -> String53    @State private var pageId: String?54    @State private var failure: String?5556    var body: some View {57        Group {58            if let pageId {59                CanvasScreen(repositories: world.repositories, pageId: pageId, router: router, attachments: FeatureSamples.attachmentLoader(world))60            } else if let failure {61                NVEmptyState(systemImage: "exclamationmark.triangle", title: "Préparation impossible", message: failure)62            } else {63                NVProgressDots()64            }65        }66        .frame(maxWidth: .infinity, maxHeight: .infinity)67        .task {68            guard pageId == nil else { return }69            do { pageId = try await prepare(world) } catch { failure = String(describing: error) }70        }71    }72}7374/// The drawing viewer with its « Modifier » control opening the built-in ink editor (what the macOS editor's atom sheet does).75struct CatalogDrawingBlock: View {76    @State private var editing: InkDocument?77    @State private var saved: InkDocument?7879    var body: some View {80        var block = DrawingBlock(width: 520, height: 300)81        block.ink = FeatureSamples.canvasInkId82        return Group {83            if let editing {84                InkEditorView(document: editing, onCancel: { self.editing = nil }) { ink, _ in85                    saved = ink86                    self.editing = nil87                }88            } else if let saved {89                DrawingBlockViewer(block: block, ink: saved) { editing = $0 }.padding(NVSpace.xl)90            } else {91                DrawingBlockViewer(block: block) { editing = $0 }.padding(NVSpace.xl)92            }93        }94        .frame(maxWidth: .infinity, maxHeight: .infinity)95        .background(NVColor.bg)96    }97}9899extension FeatureSamples {100    /// A second canvas of the sample world edited both on this device and on the server (same card, a card deleted on101    /// one side and edited on the other): returns its page id, with the conflict waiting in the store.102    public static func makeCanvasConflict(_ world: FeatureWorld) async throws -> String {103        let id = world.fake.webCreateCanvas(title: "Canevas en conflit")104        world.fake.webSaveCanvas(id, data: sampleCanvas().jsonData())105        await world.sync()106        world.setOffline(true)107        var local = sampleCanvas()108        local.updateNode(id: "n-text") { $0.text = "Question de recherche — version de cet appareil" }109        local.updateNode(id: "n-ellipse") { $0.text = "Données 2005–2025" }110        try await world.repositories.canvases.save(pageId: id, local, editedFrom: sampleCanvas())111        var remote = sampleCanvas()112        remote.updateNode(id: "n-text") { $0.text = "Question de recherche (reformulée sur le Web)" }113        remote.removeNode(id: "n-ellipse")114        world.fake.webSaveCanvas(id, data: remote.jsonData())115        world.setOffline(false)116        await world.sync()117        return id118    }119}120
Files main
Menu

Theme