Repository Public
1// SP Notes iOS2// File: Features/Editor/Blocks/BlockPickers.swift3// Description: The choices made before a block is inserted — the block a `blockRef` points at (a note, then one of its blocks),4// the canvas a `canvasLink` opens (an existing canvas page or a new one) — and the due date of a task item.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SPNotesCore9import SPNotesDesign10import SwiftUI1112struct BlockRefPicker: View {13 let excluding: String14 let onPick: (_ pageId: String, _ blockId: String?) -> Void15 let onCancel: () -> Void16 @Environment(AppModel.self) private var app17 @State private var notes: [PageSummary] = []18 @State private var search = ""19 @State private var chosen: PageSummary?20 @State private var blocks: [(id: String, type: String, text: String)] = []2122 private var filtered: [PageSummary] {23 let needle = EditorCommand.fold(search)24 return needle.isEmpty ? notes : notes.filter { EditorCommand.fold($0.title).contains(needle) }25 }2627 var body: some View {28 NavigationStack {29 List {30 if let chosen {31 Section(AppModel.displayTitle(chosen.title)) {32 Button { onPick(chosen.id, nil) } label: { Label("Toute la note (début)", systemImage: "doc.text") }.accessibilityIdentifier("blockref-pick-page")33 ForEach(blocks, id: \.id) { block in34 Button { onPick(chosen.id, block.id) } label: {35 Text(block.text.isEmpty ? "« \(block.type) »" : block.text).lineLimit(3).foregroundStyle(NVColor.fg).multilineTextAlignment(.leading)36 }37 .accessibilityIdentifier("blockref-pick-\(block.id)")38 }39 }40 } else {41 ForEach(filtered) { note in42 Button { choose(note) } label: { Text(AppModel.displayTitle(note.title)).foregroundStyle(NVColor.fg) }43 }44 }45 }46 .searchable(text: $search, prompt: "Rechercher une note")47 .navigationTitle(chosen == nil ? String(localized: "Référencer un bloc") : String(localized: "Choisir le bloc"))48 .navigationBarTitleDisplayMode(.inline)49 .toolbar {50 ToolbarItem(placement: .cancellationAction) { Button("Annuler", action: onCancel) }51 if chosen != nil { ToolbarItem(placement: .topBarLeading) { Button("Notes") { chosen = nil } } }52 }53 }54 .task {55 notes = ((try? await app.store.summaries(list: .all)) ?? []).filter { $0.id != excluding && $0.type != "database" && !$0.isFolder }56 }57 .accessibilityIdentifier("blockref-picker")58 }5960 private func choose(_ note: PageSummary) {61 Task {62 let document = ((try? await app.store.page(id: note.id)).flatMap { $0 }).flatMap { try? NoteDocument(json: $0.content) }63 blocks = (document?.referencableBlocks() ?? []).filter { !$0.text.isEmpty || $0.type != NodeType.paragraph }64 chosen = note65 }66 }67}6869struct CanvasLinkPicker: View {70 let onPick: (String) -> Void71 let onCancel: () -> Void72 @Environment(AppModel.self) private var app73 @State private var canvases: [PageSummary] = []74 @State private var title = ""75 @State private var failure: String?7677 var body: some View {78 NavigationStack {79 Form {80 Section("Nouveau canvas") {81 TextField("Titre", text: $title).accessibilityIdentifier("canvas-new-title")82 Button { create() } label: { Label("Créer et lier", systemImage: "plus.rectangle.on.rectangle") }.accessibilityIdentifier("canvas-create")83 if let failure { BlockNotice(text: failure, symbol: "exclamationmark.triangle") }84 }85 Section("Canvas existants") {86 if canvases.isEmpty { Text("Aucun canvas pour l’instant").foregroundStyle(NVColor.fgMuted) }87 ForEach(canvases) { canvas in88 Button { onPick(canvas.id) } label: { Label(AppModel.displayTitle(canvas.title), systemImage: "rectangle.3.group").foregroundStyle(NVColor.fg) }89 }90 }91 }92 .scrollContentBackground(.hidden)93 .background(NVColor.bg)94 .navigationTitle("Lier un canvas")95 .navigationBarTitleDisplayMode(.inline)96 .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Annuler", action: onCancel) } }97 }98 .task { canvases = ((try? await app.store.summaries(list: .all)) ?? []).filter { $0.type == "canvas" } }99 .accessibilityIdentifier("canvas-picker")100 }101102 private func create() {103 Task {104 do {105 let name = title.trimmingCharacters(in: .whitespacesAndNewlines)106 onPick(try await app.repositories.canvases.create(title: name.isEmpty ? "Canvas" : name))107 } catch { failure = String(localized: "Le canvas n’a pas pu être créé : \(error.localizedDescription)") }108 }109 }110}111112/// Due date of a task item: a day (stored as `YYYY-MM-DD`), or none.113struct TaskDueSheet: View {114 let current: String?115 let onSet: (String?) -> Void116 let onCancel: () -> Void117 @State private var date: Date118119 init(current: String?, onSet: @escaping (String?) -> Void, onCancel: @escaping () -> Void) {120 self.current = current121 self.onSet = onSet122 self.onCancel = onCancel123 _date = State(initialValue: Self.initialDate(current))124 }125126 /// The stored day shown in the device's calendar (a bare day is a UTC date: its components are taken as they are).127 static func initialDate(_ due: String?, now: Date = Date()) -> Date {128 guard let due, let parsed = TaskItemDetail.date(from: due) else { return now }129 guard due.utf8.count == 10 else { return parsed }130 var utc = Calendar(identifier: .gregorian)131 utc.timeZone = TimeZone(identifier: "UTC") ?? .current132 let parts = utc.dateComponents([.year, .month, .day], from: parsed)133 return Calendar.current.date(from: parts) ?? parsed134 }135136 var body: some View {137 NavigationStack {138 Form {139 DatePicker("Échéance", selection: $date, displayedComponents: .date).datePickerStyle(.graphical).accessibilityIdentifier("task-due-picker")140 if current != nil {141 Button(role: .destructive) { onSet(nil) } label: { Label("Retirer l’échéance", systemImage: "calendar.badge.minus") }.accessibilityIdentifier("task-due-clear")142 }143 }144 .scrollContentBackground(.hidden)145 .background(NVColor.bg)146 .navigationTitle("Échéance")147 .navigationBarTitleDisplayMode(.inline)148 .toolbar {149 ToolbarItem(placement: .cancellationAction) { Button("Annuler", action: onCancel) }150 ToolbarItem(placement: .confirmationAction) { Button("OK") { onSet(TaskItemDetail.dayString(date)) }.fontWeight(.semibold).accessibilityIdentifier("task-due-done") }151 }152 }153 .presentationDetents([.medium, .large])154 }155}156