| 1 | import SwiftUI |
| 2 | @MainActor struct EditorToolbar: View { |
| 3 | @Bindable var model: DocsModel |
| 4 | var body: some View { |
| 5 | // One observed value, assigned only when it differs; kept while |
| 6 | // input is pending (DocsModel.refreshToolbarSelection). |
| 7 | let selection = model.toolbarSelection |
| 8 | ScrollView(.horizontal) { |
| 9 | HStack(spacing: 12) { |
| 10 | // The observed history, set with each publication: current |
| 11 | // for a reader too, and while input is pending, when the |
| 12 | // toolbar selection keeps its last state. |
| 13 | command("docs.editor.toolbar.undo", icon: "arrow.uturn.backward", op: "undo", enabled: model.history.canUndo) |
| 14 | command("docs.editor.toolbar.redo", icon: "arrow.uturn.forward", op: "redo", enabled: model.history.canRedo) |
| 15 | Divider().frame(height: 24) |
| 16 | Menu { |
| 17 | Button { block("paragraph") } label: { Text("docs.editor.toolbar.block.paragraph", bundle: DocsResources.bundle) } |
| 18 | ForEach(1...3, id: \.self) { level in |
| 19 | Button { block("heading", level: level) } label: { Text(LocalizedStringKey(EditorToolbar.headingKey(level)), bundle: DocsResources.bundle) } |
| 20 | } |
| 21 | } label: { Text(selection.styleLabel).frame(minWidth: 100, alignment: .leading) }.accessibilityLabel(Text("native.style", bundle: DocsResources.bundle)) |
| 22 | #if os(macOS) |
| 23 | DocsFontPopUp(selected: selection.font) { key in ready { $0.setFont(key) } }.fixedSize() |
| 24 | #else |
| 25 | DocsFontPickerButton(selected: selection.font) { key in ready { $0.setFont(key) } } |
| 26 | #endif |
| 27 | Menu { |
| 28 | ForEach([12, 8, 9, 10, 11, 14, 18, 24, 36, 48, 72], id: \.self) { size in |
| 29 | Button(size.formatted()) { format("size_\(size)", exclusive: [8,9,10,11,12,14,18,24,36,48,72].map { "size_\($0)" } + ["size_sm", "size_lg", "size_xl"]) } |
| 30 | } |
| 31 | } label: { Text(selection.size.formatted()).frame(minWidth: 32) }.accessibilityLabel(Text("native.size", bundle: DocsResources.bundle)) |
| 32 | Divider().frame(height: 24) |
| 33 | mark("docs.editor.toolbar.bold", icon: "bold", name: "strong", active: selection.marks.contains("strong")) |
| 34 | mark("docs.editor.toolbar.italic", icon: "italic", name: "em", active: selection.marks.contains("em")) |
| 35 | mark("docs.editor.toolbar.code", icon: "chevron.left.forwardslash.chevron.right", name: "code", active: selection.marks.contains("code")) |
| 36 | Divider().frame(height: 24) |
| 37 | ForEach(["left", "center", "right"], id: \.self) { alignment in |
| 38 | Button { blockAttribute("align", value: alignment) } label: { Image(systemName: "text.align" + alignment) } |
| 39 | .accessibilityLabel(Text(LocalizedStringKey("docs.editor.toolbar.align." + alignment), bundle: DocsResources.bundle)) |
| 40 | .foregroundStyle(selection.alignment == alignment ? Color.accentColor : Color.primary) |
| 41 | .accessibilityAddTraits(selection.alignment == alignment ? .isSelected : []) |
| 42 | } |
| 43 | Divider().frame(height: 24) |
| 44 | ForEach(["bullet_list", "ordered_list", "blockquote"], id: \.self) { type in |
| 45 | Button { block(type) } label: { Image(systemName: type == "bullet_list" ? "list.bullet" : type == "ordered_list" ? "list.number" : "text.quote") } |
| 46 | .accessibilityLabel(Text(LocalizedStringKey("native." + type), bundle: DocsResources.bundle)) |
| 47 | .foregroundStyle(selection.blocks.contains(type) ? Color.accentColor : Color.primary) |
| 48 | .accessibilityAddTraits(selection.blocks.contains(type) ? .isSelected : []) |
| 49 | } |
| 50 | command("docs.editor.menu.format.indent", icon: "increase.indent", op: "indent", enabled: true) |
| 51 | command("docs.editor.menu.format.outdent", icon: "decrease.indent", op: "outdent", enabled: true) |
| 52 | command("docs.editor.menu.format.clear", icon: "eraser", op: "clear-format", enabled: selection.hasSelection) |
| 53 | }.buttonStyle(.borderless).padding(.horizontal, 12).padding(.vertical, 8) |
| 54 | } |
| 55 | .scrollIndicators(.hidden) |
| 56 | .fixedSize(horizontal: false, vertical: true) |
| 57 | .accessibilityIdentifier("docs.formatting-toolbar") |
| 58 | // Replica work briefly gates commands on every keystroke. Keep that |
| 59 | // synchronization gate out of disabled styling so the bar does not |
| 60 | // blink (and is not evaluated again every key): a tap then runs once |
| 61 | // the input is drawn. Held for longer, the bar shows disabled. |
| 62 | .disabled(!actions.isAvailable || model.commandsHeld) |
| 63 | } |
| 64 | private func command(_ key: String, icon: String, op: String, enabled: Bool) -> some View { |
| 65 | Button { ready { $0.perform(op) } } label: { Image(systemName: icon) } |
| 66 | .accessibilityLabel(Text(LocalizedStringKey(key), bundle: DocsResources.bundle)).disabled(!enabled) |
| 67 | } |
| 68 | private func mark(_ key: String, icon: String, name: String, active: Bool) -> some View { |
| 69 | Button { format(name) } label: { Image(systemName: icon) }.accessibilityLabel(Text(LocalizedStringKey(key), bundle: DocsResources.bundle)) |
| 70 | .foregroundStyle(active ? Color.accentColor : Color.primary) |
| 71 | .accessibilityAddTraits(active ? .isSelected : []) |
| 72 | } |
| 73 | private var actions: DocumentCommands { DocumentCommands(model: model, content: model.surface.content) } |
| 74 | private func format(_ name: String, exclusive: [String] = []) { ready { $0.format(name, exclusive: exclusive) } } |
| 75 | private func block(_ type: String, level: Int = 1) { ready { $0.block(type, level: level) } } |
| 76 | private func blockAttribute(_ name: String, value: String) { ready { $0.blockAttribute(name, value: value == "left" ? nil : value) } } |
| 77 | /// Runs a command now, or once the input pending when it was tapped |
| 78 | /// has been drawn (`DocsModel.whenCommandsReady`). |
| 79 | private func ready(_ command: @escaping @MainActor (DocumentCommands) -> Void) { |
| 80 | model.whenCommandsReady(command) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | extension EditorToolbar { |
| 85 | /// The heading menu's key for `level`, built as a plain String first: |
| 86 | /// an interpolated LocalizedStringKey literal turns the level into a |
| 87 | /// format placeholder, looks up "…heading%lld", finds nothing and shows |
| 88 | /// the raw key (Docs 0.1.9's style menu). check-native.sh refuses that |
| 89 | /// pattern anywhere in native/apple/Sources. |
| 90 | nonisolated static func headingKey(_ level: Int) -> String { |
| 91 | "docs.editor.toolbar.block.heading" + String(level) |
| 92 | } |
| 93 | } |
| 94 | |