SPB Git

Repository Public

CommandPalette.swift 8.7 KB 90 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Features/Search/CommandPalette.swift3// Description: The application's commands (navigation, notes, formatting, every block that can be inserted, synchronisation, application) as one catalogue. They are registered in the single command registry of SPNotesFeatures and shown by its Command Center (⌘K, and the historical ⇧⌘P and ⌘O) — see Features/Hosting/CommandCenterHost.swift.4import SwiftUI5import SPNotesStore6import SPNotesDocument7import SPNotesEditor8import SPNotesDesign910/// One command of the application. The catalogue below is the only list; it feeds the shared command registry.11struct AppCommand: Identifiable {12    enum Group: String, CaseIterable { case go = "Aller à", note = "Note", format = "Format", insert = "Insérer", sync = "Synchronisation", app = "Application" }13    let id: String, title: String, symbol: String14    var shortcut = ""15    var group: Group16    /// Extra words the search matches (synonyms, English terms).17    var keywords = ""18    let run: () -> Void19}2021@MainActor enum AppCommandCatalog {22    static func commands(env: AppEnvironment, window: WindowModel) -> [AppCommand] {23        let v = window.destination == .notes && window.session?.isEditable == true ? window.session?.editor.textView : nil24        var c: [AppCommand] = []25        func add(_ id: String, _ title: String, _ symbol: String, _ group: AppCommand.Group, shortcut: String = "", keywords: String = "", _ run: @escaping () -> Void) { c.append(AppCommand(id: id, title: title, symbol: symbol, shortcut: shortcut, group: group, keywords: keywords, run: run)) }26        // navigation: the notes, then every feature screen that exists in this build27        add("go.notes", "Notes", Destination.notes.symbol, .go, keywords: "library bibliothèque") { window.destination = .notes }28        for d in Destination.features where env.features.isAvailable(d, env: env) { add("go." + d.rawValue, d.title, d.symbol, .go, keywords: d.rawValue) { window.databaseId = nil; window.canvasPageId = nil; window.graphPageId = nil; window.destination = d } }29        add("go.all", "Toutes les notes", "tray.full", .go) { window.destination = .notes; window.library.selection = .list(.all) }30        add("go.fav", "Favoris", "star", .go) { window.destination = .notes; window.library.selection = .list(.favorites) }31        add("go.recent", "Récentes", "clock", .go) { window.destination = .notes; window.library.selection = .list(.recent) }32        add("go.archive", "Archives", "archivebox", .go) { window.destination = .notes; window.library.selection = .list(.archived) }33        add("go.trash", "Corbeille", "trash", .go) { window.destination = .notes; window.library.selection = .list(.trash) }34        add("search", "Rechercher dans toutes les notes", "magnifyingglass", .go, shortcut: "⌥⌘F", keywords: "find") { window.destination = .notes; window.focusSearch += 1 }35        // notes36        add("new", "Nouvelle note", "square.and.pencil", .note, shortcut: "⌘N", keywords: "new create") { window.newNote() }37        add("newfolder", "Nouveau dossier", "folder.badge.plus", .note, shortcut: "⇧⌘N") { window.newNote(asFolder: true) }38        add("quick", "Capture rapide", "bolt", .note, shortcut: env.preferences.quickNoteShortcut.display, keywords: "note rapide quick") { AppDelegate.shared?.toggleQuickNote() }39        add("newtask", "Nouvelle tâche…", "checklist", .note, keywords: "todo task") { window.destination = .tasks }40        if window.session != nil {41            add("history", "Historique des versions", "clock.arrow.circlepath", .note) { window.showHistory = true }42            add("inspector", "Afficher ou masquer l'inspecteur", "sidebar.right", .note, shortcut: "⌥⌘I", keywords: "liens rétroliens backlinks tags") { window.showInspector.toggle() }43            if env.preferences.aiEnabled { add("assistant", "Afficher ou masquer l'assistant", "sparkles", .note, shortcut: "⇧⌘A", keywords: "ia ai") { window.showAssistant.toggle() } }44            if let id = window.currentNoteId {45                if env.features.isAvailable(.graph, env: env) { add("localgraph", "Graphe autour de cette note", "point.3.connected.trianglepath.dotted", .note, keywords: "graph local liens") { window.graphPageId = id; window.destination = .graph } }46                if env.features.isAvailable(.timeline, env: env) { add("pagetimeline", "Chronologie et machine à remonter le temps de cette note", "clock.arrow.circlepath", .note, keywords: "timeline time machine historique") { window.pageTimeline = id } }47                if env.features.isAvailable(.ask, env: env) { add("askpage", "Demander à propos de cette note", "sparkles", .note, keywords: "ask question ia") { window.destination = .ask } }48                add("newwindow", "Ouvrir la note dans une nouvelle fenêtre", "macwindow.badge.plus", .note) { OpenNoteWindow.open?(id) }49                add("copylink", "Copier le lien de la note", "link", .note, keywords: "spnotes url") { NSPasteboard.general.clearContents(); NSPasteboard.general.setString(Route(pageId: id).url.absoluteString, forType: .string); env.toasts.show("Lien copié", symbol: "link") }50                for f in ExportFormat.allCases { add("export." + f.rawValue, "Exporter en \(f.label)", "square.and.arrow.up", .note, keywords: "export") { Task { await window.session?.saveNow(reason: nil); await Exporter(env: env).export(noteIds: [id], format: f) } } }51            }52        }53        add("import", "Importer des fichiers Markdown, texte ou RTF…", "square.and.arrow.down", .note, shortcut: "⇧⌘I") { AppActions.importFiles(env: env, window: window) }54        if let v {55            add("f.p", "Texte", "textformat", .format, shortcut: "⌥⌘0") { v.setBlock(.paragraph) }56            for l in 1...3 { add("f.h\(l)", "Titre \(l)", "\(l).square", .format, shortcut: "⌥⌘\(l)", keywords: "heading") { v.setBlock(.heading(l), toggle: false) } }57            add("f.ul", "Liste à puces", "list.bullet", .format, shortcut: "⇧⌘8") { v.setBlock(.bullet) }58            add("f.ol", "Liste numérotée", "list.number", .format, shortcut: "⇧⌘7") { v.setBlock(.ordered(start: 1)) }59            add("f.todo", "Liste de tâches", "checklist", .format, shortcut: "⇧⌘9") { v.setBlock(.task) }60            add("f.quote", "Citation", "text.quote", .format) { v.setBlock(.quote) }61            add("f.code", "Bloc de code", "chevron.left.forwardslash.chevron.right", .format, shortcut: "⌥⌘C") { v.setBlock(.code) }62            for k in CalloutKind.known { add("f.callout." + k.rawValue, "Encadré : " + k.label, "lightbulb", .format, keywords: "callout " + k.rawValue) { v.setCallout(k) } }63            add("f.task", "Échéance et priorité de la tâche…", "calendar.badge.clock", .format, shortcut: "⌃⌘D", keywords: "due priority") { v.requestTaskDetails() }64            add("f.clear", "Effacer la mise en forme de la sélection", "eraser", .format) { v.clearFormatting() }65            for item in EditorInsertions.all(window: window, env: env) { add("i." + item.id, item.title, item.symbol, .insert, keywords: item.keywords) { item.run(v) } }66        }67        add("sync", "Synchroniser maintenant", "arrow.triangle.2.circlepath", .sync, shortcut: "⌘R") { env.syncMonitor.syncNow() }68        add("account", "Compte et synchronisation", "person.crop.circle", .sync, keywords: "passkey clé connexion") { window.showAccount = true }69        add("exportall", "Exporter toute la bibliothèque (ZIP)…", "archivebox", .app, keywords: "backup sauvegarde") { Task { await Exporter(env: env).exportLibrary() } }70        add("settings", "Réglages…", "gearshape", .app, shortcut: "⌘,", keywords: "preferences") { NSApp.sendAction(Selector(("showSettingsWindow:")), to: nil, from: nil) }71        if env.lock.isAvailable { add("lock", "Verrouiller SP Notes", "lock", .app, shortcut: "⌃⌘L") { Task { await env.flushAll(); env.lock.lock() } } }72        return c73    }7475}7677extension CalloutKind {78    var label: String {79        switch rawValue { case "note": "Note"; case "info": "Information"; case "warning": "Avertissement"; case "danger": "Danger"; case "success": "Réussite"; case "decision": "Décision"; case "question": "Question ouverte"; case "hypothesis": "Hypothèse"; default: rawValue }80    }81}8283enum AppActions {84    @MainActor static func importFiles(env: AppEnvironment, window: WindowModel?) {85        guard !env.isBlocked else { return }86        let panel = NSOpenPanel(); panel.allowsMultipleSelection = true; panel.allowedContentTypes = Exporter.importTypes; panel.message = "Chaque fichier devient une note."87        panel.begin { r in guard r == .OK else { return }; let urls = panel.urls; Task { let ids = await Exporter(env: env).importFiles(urls); await window?.library.reloadNotes(); if let last = ids.last { window?.open(last) } } }88    }89}90
Files main
Menu

Theme