Repository Public
1// SP Notes iOS2// File: Features/Editor/Blocks/ReferenceBlockSheets.swift3// Description: Sheets of the research blocks: an inline `citation` (label, locator, choice among the references of the local4// copy — also used to insert a new one) and the `bibliography` (title, style, the references cited in the page).5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SPNotesCore9import SPNotesDesign10import SwiftUI1112struct CitationSheet: View {13 let isNew: Bool14 let app: AppModel15 let isEditable: Bool16 let onDone: (CitationInline) -> Void17 var onDelete: (() -> Void)?18 let onCancel: () -> Void19 @State private var citation: CitationInline20 @State private var references: [ResearchReference] = []21 @State private var search = ""22 @State private var isLoaded = false2324 init(citation: CitationInline?, isNew: Bool, app: AppModel, isEditable: Bool = true, onDone: @escaping (CitationInline) -> Void, onDelete: (() -> Void)? = nil, onCancel: @escaping () -> Void) {25 self.isNew = isNew26 self.app = app27 self.isEditable = isEditable28 self.onDone = onDone29 self.onDelete = onDelete30 self.onCancel = onCancel31 _citation = State(initialValue: citation ?? CitationInline(refId: "", label: "", locator: ""))32 }3334 private var filtered: [ResearchReference] {35 let needle = EditorCommand.fold(search)36 guard !needle.isEmpty else { return references }37 return references.filter { EditorCommand.fold($0.title + " " + ($0.authors ?? []).joined(separator: " ") + " " + $0.citeKey + " " + ($0.year.map(String.init) ?? "")).contains(needle) }38 }3940 var body: some View {41 BlockSheetScaffold(title: isNew ? String(localized: "Citer une référence") : String(localized: "Citation"), isEditable: isEditable, onCancel: onCancel,42 onDone: citation.refId == nil && isNew ? nil : { onDone(citation) }, onDelete: isNew ? nil : onDelete) {43 Form {44 Section("Citation") {45 TextField("Libellé (Auteur Année)", text: Binding(get: { citation.label }, set: { if $0 != citation.label { citation.label = $0 } })).accessibilityIdentifier("citation-label")46 TextField("Repère (p. 12, chap. 3…)", text: Binding(get: { citation.locator }, set: { if $0 != citation.locator { citation.locator = $0 } })).accessibilityIdentifier("citation-locator")47 LabeledContent("Aperçu", value: "[" + citation.displayText + "]")48 }49 .disabled(!isEditable)50 Section {51 if !isLoaded {52 ProgressView()53 } else if references.isEmpty {54 NVEmptyState(systemImage: "books.vertical", title: String(localized: "Aucune référence sur cet appareil"),55 message: String(localized: "Les références s’ajoutent dans l’espace Recherche ; elles apparaissent ici après la synchronisation."))56 } else {57 TextField("Rechercher une référence", text: $search).textInputAutocapitalization(.never).autocorrectionDisabled().accessibilityIdentifier("citation-search")58 ForEach(filtered) { reference in59 Button {60 citation.refId = reference.id61 citation.label = ReferenceFormat.label(reference)62 } label: {63 HStack(alignment: .firstTextBaseline) {64 VStack(alignment: .leading, spacing: 2) {65 Text(reference.title.isEmpty ? reference.citeKey : reference.title).nvFont(.body).foregroundStyle(NVColor.fg).multilineTextAlignment(.leading)66 Text(ReferenceFormat.label(reference)).nvFont(.ui).foregroundStyle(NVColor.fgMuted)67 }68 Spacer(minLength: 8)69 if citation.refId == reference.id { Image(systemName: "checkmark").foregroundStyle(NVColor.accent).accessibilityLabel("Sélectionnée") }70 }71 }72 .accessibilityIdentifier("citation-reference-\(reference.citeKey)")73 }74 }75 } header: { Text("Référence") }76 .disabled(!isEditable)77 }78 }79 .task {80 references = (try? await app.repositories.research.localReferences()) ?? []81 isLoaded = true82 }83 }84}8586struct BibliographySheet: View {87 let original: BibliographyBlock88 let context: BlockDetailContext89 @State private var block: BibliographyBlock90 @State private var entries: [String] = []91 @State private var missing = 092 @State private var isLoaded = false9394 static let styles = ["apa", "mla", "chicago", "ieee", "harvard"]9596 init(block: BibliographyBlock, context: BlockDetailContext) {97 original = block98 self.context = context99 _block = State(initialValue: block)100 }101102 private var styles: [String] { Self.styles.contains(block.style) ? Self.styles : [block.style] + Self.styles }103104 var body: some View {105 BlockSheetScaffold(title: String(localized: "Bibliographie"), isEditable: context.isEditable, onCancel: context.dismiss,106 onDone: { context.finish(block.node(), original: original.node()) }, onDelete: context.delete) {107 Form {108 Section {109 TextField("Titre", text: Binding(get: { block.title }, set: { if $0 != block.title { block.title = $0 } })).accessibilityIdentifier("bibliography-title")110 Picker("Style", selection: Binding(get: { block.style }, set: { if $0 != block.style { block.style = $0 } })) {111 ForEach(styles, id: \.self) { Text($0.uppercased()).tag($0) }112 }113 .accessibilityIdentifier("bibliography-style")114 } footer: { Text("Le style s’applique aux exports faits par le Web. Ici, les références sont listées sous une forme neutre auteur–année.") }115 .disabled(!context.isEditable)116 Section("Références citées dans la page") {117 if !isLoaded { ProgressView() }118 else if entries.isEmpty && missing == 0 { Text("Aucune référence citée dans cette page").foregroundStyle(NVColor.fgMuted) }119 ForEach(Array(entries.enumerated()), id: \.offset) { _, entry in Text(entry).nvFont(.body).foregroundStyle(NVColor.fg).textSelection(.enabled) }120 if missing > 0 {121 BlockNotice(text: missing == 1 ? String(localized: "1 référence citée n’est pas sur cet appareil") : String(localized: "\(missing) références citées ne sont pas sur cet appareil"), symbol: "icloud.slash")122 }123 }124 }125 }126 .task {127 // the page as last saved (the editor saves a few hundred milliseconds after each change)128 let cited = ((try? await context.app.store.page(id: context.noteId)).flatMap { $0 }.flatMap { try? NoteDocument(json: $0.content) })?.citedReferences() ?? []129 let found = (try? await context.app.repositories.research.resolveLocally(ids: cited)) ?? [:]130 entries = cited.compactMap { found[$0].map(ReferenceFormat.entry) }131 missing = cited.count - entries.count132 isLoaded = true133 }134 }135}136