SPB Git

Repository Public

InsertCatalog.swift 9.9 KB 140 lines swift

1// SP Notes iOS2// File: Editor/InsertCatalog.swift3// Description: The one list of what can be inserted or applied from the Insert menu of the formatting bar and from the slash4//              menu: every entry is an `EditorAction` with its French title, symbol, group and search keywords.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SPNotesCore9import UIKit1011struct EditorCommand: Identifiable, Hashable {12    enum Group: Int, CaseIterable, Hashable {13        case style, media, structure, data, references1415        var title: String {16            switch self {17            case .style: String(localized: "Texte")18            case .media: String(localized: "Média")19            case .structure: String(localized: "Structure")20            case .data: String(localized: "Données")21            case .references: String(localized: "Références")22            }23        }24    }2526    let id: String27    let title: String28    let symbol: String29    let group: Group30    let action: EditorAction31    /// Extra words the slash menu matches (English names, synonyms), lower-case without accents.32    var keywords: [String] = []3334    /// Case- and accent-insensitive match on the title, the id and the keywords. An empty query matches everything.35    func matches(_ query: String) -> Bool {36        let needle = Self.fold(query)37        guard !needle.isEmpty else { return true }38        if Self.fold(title).contains(needle) || id.contains(needle) { return true }39        return keywords.contains { $0.contains(needle) }40    }4142    /// Rank for sorting results: a title that starts with the query comes first.43    func rank(_ query: String) -> Int {44        let needle = Self.fold(query)45        if needle.isEmpty { return 1 }46        if Self.fold(title).hasPrefix(needle) || id.hasPrefix(needle) { return 0 }47        if Self.fold(title).split(separator: " ").contains(where: { $0.hasPrefix(needle) }) || keywords.contains(where: { $0.hasPrefix(needle) }) { return 1 }48        return 249    }5051    static func fold(_ text: String) -> String {52        text.folding(options: [.diacriticInsensitive, .caseInsensitive], locale: Locale(identifier: "fr")).lowercased().trimmingCharacters(in: .whitespaces)53    }54}5556enum InsertCatalog {57    /// What the Insert menu offers, by group (Média · Structure · Données · Références).58    static let insertions: [EditorCommand] = [59        EditorCommand(id: "photo", title: String(localized: "Photo de la photothèque"), symbol: "photo.on.rectangle", group: .media, action: .insertPhoto, keywords: ["image", "picture"]),60        EditorCommand(id: "camera", title: String(localized: "Prendre une photo"), symbol: "camera", group: .media, action: .insertCamera, keywords: ["appareil", "camera"]),61        EditorCommand(id: "scan", title: String(localized: "Numériser un document"), symbol: "doc.viewfinder", group: .media, action: .insertScan, keywords: ["scanner", "scan"]),62        EditorCommand(id: "file", title: String(localized: "Fichier"), symbol: "paperclip", group: .media, action: .insertFile, keywords: ["piece jointe", "attachment"]),63        EditorCommand(id: "drawing", title: String(localized: "Dessin"), symbol: "pencil.tip.crop.circle", group: .media, action: .insertDrawing, keywords: ["croquis", "sketch", "pencil"]),64        EditorCommand(id: "pdf", title: String(localized: "PDF à annoter"), symbol: "doc.richtext", group: .media, action: .insertPDF, keywords: ["document", "annotation"]),65        EditorCommand(id: "audio", title: String(localized: "Fichier audio"), symbol: "waveform", group: .media, action: .insertAudio, keywords: ["note vocale", "voice", "son", "enregistrement"]),6667        EditorCommand(id: "table", title: String(localized: "Tableau"), symbol: "tablecells", group: .structure, action: .insertTable, keywords: ["table", "grille"]),68        EditorCommand(id: "rule", title: String(localized: "Séparateur"), symbol: "minus", group: .structure, action: .insertRule, keywords: ["ligne", "divider", "hr"]),69        EditorCommand(id: "details", title: String(localized: "Bloc dépliable"), symbol: "chevron.right.square", group: .structure, action: .insertDetails, keywords: ["toggle", "details", "replier"]),70        EditorCommand(id: "footnote", title: String(localized: "Note de bas de page"), symbol: "text.append", group: .structure, action: .insertFootnote, keywords: ["footnote", "renvoi"]),71        EditorCommand(id: "mermaid", title: String(localized: "Diagramme Mermaid"), symbol: "point.3.connected.trianglepath.dotted", group: .structure, action: .insertMermaid, keywords: ["diagramme", "schema", "flowchart", "graph"]),72        EditorCommand(id: "runcode", title: String(localized: "Code exécutable"), symbol: "terminal", group: .structure, action: .insertRunCode, keywords: ["javascript", "script", "run"]),73        EditorCommand(id: "math", title: String(localized: "Équation"), symbol: "sum", group: .structure, action: .insertMath, keywords: ["latex", "formule", "math"]),74        EditorCommand(id: "inlinemath", title: String(localized: "Équation en ligne"), symbol: "x.squareroot", group: .structure, action: .insertInlineMath, keywords: ["latex", "formule"]),7576        EditorCommand(id: "chart", title: String(localized: "Graphique"), symbol: "chart.bar", group: .data, action: .insertChart, keywords: ["chart", "courbe", "barres", "secteurs", "donnees"]),77        EditorCommand(id: "query", title: String(localized: "Vue dynamique"), symbol: "list.bullet.rectangle.portrait", group: .data, action: .insertQueryView, keywords: ["requete", "query", "taches", "liste vivante", "vue"]),78        EditorCommand(id: "canvas", title: String(localized: "Lien vers un canvas"), symbol: "rectangle.3.group", group: .data, action: .insertCanvasLink, keywords: ["tableau blanc", "whiteboard"]),7980        EditorCommand(id: "notelink", title: String(localized: "Lien vers une note"), symbol: "at", group: .references, action: .insertNoteLink, keywords: ["mention", "page"]),81        EditorCommand(id: "blockref", title: String(localized: "Référence de bloc"), symbol: "arrow.turn.down.right", group: .references, action: .insertBlockRef, keywords: ["transclusion", "embed", "bloc"]),82        EditorCommand(id: "citation", title: String(localized: "Citation de référence"), symbol: "quote.closing", group: .references, action: .insertCitation, keywords: ["cite", "source", "bibliographie"]),83        EditorCommand(id: "bibliography", title: String(localized: "Bibliographie"), symbol: "books.vertical", group: .references, action: .insertBibliography, keywords: ["references", "sources"]),84    ]8586    /// Block styles the slash menu offers on top of the insertions.87    static let styles: [EditorCommand] = [88        EditorCommand(id: "h1", title: String(localized: "Titre 1"), symbol: "1.square", group: .style, action: .heading(1), keywords: ["heading", "titre"]),89        EditorCommand(id: "h2", title: String(localized: "Titre 2"), symbol: "2.square", group: .style, action: .heading(2), keywords: ["heading", "titre"]),90        EditorCommand(id: "h3", title: String(localized: "Titre 3"), symbol: "3.square", group: .style, action: .heading(3), keywords: ["heading", "titre"]),91        EditorCommand(id: "tasks", title: String(localized: "Liste de tâches"), symbol: "checklist", group: .style, action: .list(.task), keywords: ["todo", "case", "checkbox"]),92        EditorCommand(id: "bullets", title: String(localized: "Liste à puces"), symbol: "list.bullet", group: .style, action: .list(.bullet), keywords: ["bullet"]),93        EditorCommand(id: "numbers", title: String(localized: "Liste numérotée"), symbol: "list.number", group: .style, action: .list(.ordered), keywords: ["ordered", "numero"]),94        EditorCommand(id: "quote", title: String(localized: "Citation"), symbol: "text.quote", group: .style, action: .quote, keywords: ["blockquote"]),95        EditorCommand(id: "code", title: String(localized: "Bloc de code"), symbol: "curlybraces", group: .style, action: .codeBlock, keywords: ["code", "programme"]),96    ] + CalloutKind.known.map { kind in97        EditorCommand(id: "callout-" + kind.rawValue, title: String(localized: "Encadré") + " · " + calloutTitle(kind), symbol: "lightbulb", group: .style, action: .calloutKind(kind),98                      keywords: ["callout", "encadre", kind.rawValue])99    }100101    static let slashCommands: [EditorCommand] = styles + insertions102103    static func calloutTitle(_ kind: CalloutKind) -> String {104        switch kind {105        case .note: String(localized: "Note")106        case .info: String(localized: "Information")107        case .warning: String(localized: "Avertissement")108        case .danger: String(localized: "Danger")109        case .success: String(localized: "Succès")110        case .decision: String(localized: "Décision")111        case .question: String(localized: "Question")112        case .hypothesis: String(localized: "Hypothèse")113        default: kind.rawValue114        }115    }116117    /// Slash-menu results for a query: matching commands, best first, catalogue order otherwise.118    static func search(_ query: String) -> [EditorCommand] {119        slashCommands.enumerated().filter { $0.element.matches(query) }120            .sorted { a, b in121                let ra = a.element.rank(query), rb = b.element.rank(query)122                return ra != rb ? ra < rb : a.offset < b.offset123            }124            .map(\.element)125    }126127    /// The Insert menu of the formatting bar: one inline section per group, with its title.128    @MainActor129    static func insertMenu(_ handler: @escaping @MainActor (EditorAction) -> Void) -> UIMenu {130        let groups: [EditorCommand.Group] = [.media, .structure, .data, .references]131        return UIMenu(title: "", children: groups.map { group in132            UIMenu(title: group.title, options: .displayInline, children: insertions.filter { $0.group == group }.map { command in133                let action = UIAction(title: command.title, image: UIImage(systemName: command.symbol)) { _ in handler(command.action) }134                action.accessibilityIdentifier = "insert-" + command.id135                return action136            })137        })138    }139}140
Files main
Menu

Theme