SPB Git

Repository Public

CaptionSheet.swift 4.9 KB 96 lines swift

1// ORIX Docs2// File: App/Sources/Sheets/CaptionSheet.swift3// Description: Inserts a numbered caption (Figure, Table, Equation or a custom label) followed by its text.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import SwiftUI8import DocModel910struct CaptionSheet: View {11    enum Label: String, CaseIterable, Identifiable {12        case figure, table, equation, custom13        var id: String { rawValue }14        /// Name of the numbering sequence (also what cross-references and the lists of figures and tables look for).15        var sequence: String { switch self { case .figure: return "Figure"; case .table: return "Table"; case .equation: return "Equation"; case .custom: return "" } }16        var title: LocalizedStringKey { switch self { case .figure: return "Figure"; case .table: return "Table"; case .equation: return "Equation"; case .custom: return "Custom" } }17    }1819    let session: DocumentSession20    @State private var label: Label21    @State private var custom = ""22    @State private var text = ""23    @FocusState private var textFocused: Bool2425    init(session: DocumentSession) {26        self.session = session27        // A caption placed right under a table is most likely a table caption.28        let block = session.selection.head.path.block29        let body = session.doc.body30        let nearTable = session.selection.story == .body && ((block > 0 && body.indices.contains(block - 1) && body[block - 1].table != nil) || session.selection.head.path.cell != nil)31        _label = State(initialValue: nearTable ? .table : .figure)32    }3334    private var french: Bool { session.doc.meta.language.lowercased().hasPrefix("fr") }3536    /// The word printed before the number, in the document's language.37    private var printedLabel: String {38        switch label {39        case .figure: return "Figure"40        case .table: return french ? "Tableau" : "Table"41        case .equation: return french ? "Équation" : "Equation"42        case .custom: return custom.trimmingCharacters(in: .whitespaces)43        }44    }4546    private var sequence: String { label == .custom ? custom.trimmingCharacters(in: .whitespaces) : label.sequence }4748    private var nextNumber: Int {49        let block = session.selection.story == .body ? session.selection.head.path.block : Int.max50        return session.numbering.captions.values.filter { $0.label == sequence && $0.blockIndex <= block }.count + 151    }5253    private func close() { session.activeSheet = nil }5455    private func commit() {56        guard !sequence.isEmpty else { return }57        session.insertCaption(sequence: sequence, label: printedLabel)58        let body = text.trimmingCharacters(in: .whitespacesAndNewlines)59        if !body.isEmpty { session.perform(String(localized: "Caption Text")) { $0.insertText(body) } }60        close()61    }6263    var body: some View {64        SheetScaffold(title: "Insert Caption", symbol: "text.below.photo",65                      subtitle: "Captions are numbered automatically and can be the target of cross-references.", width: 480,66                      primaryTitle: "Insert", primaryEnabled: !sequence.isEmpty, onPrimary: commit, onCancel: close) {67            VStack(alignment: .leading, spacing: ORIX.Space.m) {68                Picker("Label", selection: $label) {69                    ForEach(Label.allCases) { Text($0.title).tag($0) }70                }71                .pickerStyle(.segmented).labelsHidden().accessibilityLabel(Text("Label")).accessibilityIdentifier("caption-label")72                if label == .custom {73                    LabeledField(label: "Custom label") {74                        TextField("", text: $custom, prompt: Text(verbatim: "Listing, Map, Scheme…")).textFieldStyle(.roundedBorder)75                            .accessibilityLabel(Text("Custom label")).accessibilityIdentifier("caption-custom")76                    }77                }78                LabeledField(label: "Caption text") {79                    TextField("", text: $text, prompt: Text("Describe the figure or table")).textFieldStyle(.roundedBorder).focused($textFocused)80                        .accessibilityLabel(Text("Caption text")).accessibilityIdentifier("caption-text")81                }82                VStack(alignment: .leading, spacing: 4) {83                    FieldLabel("Result")84                    (Text(verbatim: sequence.isEmpty ? "—" : "\(printedLabel)\u{00A0}\(nextNumber). ").bold() + Text(verbatim: text))85                        .font(.system(size: 12.5, design: .serif)).foregroundStyle(ORIX.Palette.text)86                        .frame(maxWidth: .infinity, alignment: .leading).padding(10)87                        .background(ORIX.Palette.surfaceRaised).well()88                        .accessibilityIdentifier("caption-preview")89                }90                Text("The caption is added as a new paragraph after the current one.").font(ORIX.Font.caption).foregroundStyle(ORIX.Palette.textTertiary)91            }92        }93        .onAppear { textFocused = true }94    }95}96
Files main
Menu

Theme