| 1 | #if os(macOS) |
| 2 | import AppKit |
| 3 | import SwiftUI |
| 4 | |
| 5 | public struct DocsEditorCommands: Commands { |
| 6 | @FocusedValue(\.docsEditor) private var editor |
| 7 | public init() {} |
| 8 | public var body: some Commands { |
| 9 | |
| 10 | CommandGroup(replacing: .printItem) {} |
| 11 | |
| 12 | CommandMenu(docsMacString("Format")) { |
| 13 | Group { |
| 14 | Button(docsMacString("Bold")) { editor?.actions?.perform("bold") }.keyboardShortcut("b") |
| 15 | Button(docsMacString("Italic")) { editor?.actions?.perform("italic") }.keyboardShortcut("i") |
| 16 | Button(docsMacString("Inline code")) { editor?.actions?.perform("code") } |
| 17 | Menu(docsMacString("Paragraph style")) { |
| 18 | Button(docsMacString("Normal")) { editor?.actions?.block("paragraph") } |
| 19 | ForEach(1...6, id: \.self) { level in |
| 20 | Button(String(format: docsMacString("Heading %d"), level)) { editor?.actions?.block("heading", level: level) } |
| 21 | } |
| 22 | Button(docsMacString("Bullet list")) { editor?.actions?.block("bullet_list") } |
| 23 | Button(docsMacString("Numbered list")) { editor?.actions?.block("ordered_list") } |
| 24 | Button(docsMacString("Block quote")) { editor?.actions?.block("blockquote") } |
| 25 | } |
| 26 | Menu(docsMacString("Font")) { |
| 27 | ForEach(["sans", "serif", "mono"], id: \.self) { family in |
| 28 | Button(String(localized: String.LocalizationValue("docs.editor.toolbar.font." + family), bundle: DocsResources.bundle)) { editor?.actions?.format("font_" + family, exclusive: ["font_sans", "font_serif", "font_mono"]) } |
| 29 | } |
| 30 | } |
| 31 | Menu(docsMacString("Size")) { |
| 32 | ForEach([8, 9, 10, 11, 12, 14, 18, 24, 36, 48, 72], id: \.self) { size in |
| 33 | Button(size.formatted()) { editor?.actions?.format("size_\(size)", exclusive: [8,9,10,11,12,14,18,24,36,48,72].map { "size_\($0)" } + ["size_sm", "size_lg", "size_xl"]) } |
| 34 | } |
| 35 | } |
| 36 | Menu(docsMacString("Alignment")) { |
| 37 | ForEach(["left", "center", "right"], id: \.self) { value in |
| 38 | Button(String(localized: String.LocalizationValue("docs.editor.toolbar.align." + value), bundle: DocsResources.bundle)) { editor?.actions?.blockAttribute("align", value: value == "left" ? nil : value) } |
| 39 | } |
| 40 | } |
| 41 | Button(docsMacString("Indent")) { editor?.actions?.perform("indent") } |
| 42 | Button(docsMacString("Outdent")) { editor?.actions?.perform("outdent") } |
| 43 | Button(docsMacString("Clear formatting")) { editor?.actions?.perform("clear-format") } |
| 44 | }.disabled(editor?.actions?.isEnabled != true || !(NSApp.keyWindow?.firstResponder is DocsTextView)) |
| 45 | } |
| 46 | CommandGroup(after: .toolbar) { |
| 47 | Menu(docsMacString("Zoom")) { |
| 48 | ForEach([0.5, 0.75, 1.0, 1.25, 1.5, 2.0], id: \.self) { value in |
| 49 | Button { editor?.zoom.wrappedValue = value } label: { Text(value, format: .percent) } |
| 50 | } |
| 51 | }.disabled(editor == nil) |
| 52 | Button(docsMacString("Actual size")) { editor?.zoom.wrappedValue = 1 }.keyboardShortcut("0").disabled(editor == nil) |
| 53 | Button(docsMacString("Document inspector")) { editor?.inspecting.wrappedValue.toggle() }.disabled(editor == nil) |
| 54 | if let editor { Toggle(docsMacString("Pages"), isOn: editor.paged) } |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | #endif |
| 59 | |