import Foundation struct DocumentKeyboardBinding: Identifiable { let action: String let titleKey: String let key: String var command = false var shift = false var id: String { action + key + String(command) + String(shift) } var display: String { let glyph = key == "\t" ? "⇥" : key == "\r" ? "↩" : key.uppercased() return (shift ? "⇧" : "") + (command ? "⌘" : "") + glyph } } /// Both the native responder and the help sheet read this table. enum DocumentKeyboardBindings { static let all: [DocumentKeyboardBinding] = [ .init(action: "jump", titleKey: "docs.jumpto.label", key: "k", command: true), .init(action: "bold", titleKey: "docs.editor.toolbar.bold", key: "b", command: true), .init(action: "italic", titleKey: "docs.editor.toolbar.italic", key: "i", command: true), .init(action: "undo", titleKey: "docs.editor.toolbar.undo", key: "z", command: true), .init(action: "redo", titleKey: "docs.editor.toolbar.redo", key: "z", command: true, shift: true), .init(action: "redo", titleKey: "docs.editor.toolbar.redo", key: "y", command: true), .init(action: "indent", titleKey: "docs.editor.shortcuts.indent", key: "\t"), .init(action: "outdent", titleKey: "docs.editor.shortcuts.outdent", key: "\t", shift: true), .init(action: "hard-break", titleKey: "docs.editor.shortcuts.linebreak", key: "\r", shift: true), ] static func action(key: String, command: Bool, shift: Bool, option: Bool = false, control: Bool = false) -> String? { guard !option, !control else { return nil } let key = key == "\n" ? "\r" : key.lowercased() return all.first { $0.key == key && $0.command == command && $0.shift == shift }?.action } }