|
| 1 | +// |
| 2 | +// RichTextAction.swift |
| 3 | +// RichEditorSwiftUI |
| 4 | +// |
| 5 | +// Created by Divyesh Vekariya on 21/10/24. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | +import Combine |
| 10 | + |
| 11 | +/** |
| 12 | + This enum defines rich text actions that can be executed on |
| 13 | + a rich text editor. |
| 14 | + |
| 15 | + This type also serves as a type namespace for other related |
| 16 | + types and views, like ``RichTextAction/Button``. |
| 17 | + */ |
| 18 | +public enum RichTextAction: Identifiable, Equatable { |
| 19 | + |
| 20 | + /// Copy the currently selected text, if any. |
| 21 | + case copy |
| 22 | + |
| 23 | + /// Dismiss any presented software keyboard. |
| 24 | + case dismissKeyboard |
| 25 | + |
| 26 | + /// Paste a single image. |
| 27 | +// case pasteImage(RichTextInsertion<ImageRepresentable>) |
| 28 | +// |
| 29 | +// /// Paste multiple images. |
| 30 | +// case pasteImages(RichTextInsertion<[ImageRepresentable]>) |
| 31 | +// |
| 32 | +// /// Paste plain text. |
| 33 | +// case pasteText(RichTextInsertion<String>) |
| 34 | + |
| 35 | + /// A print command. |
| 36 | + case print |
| 37 | + |
| 38 | + /// Redo the latest undone change. |
| 39 | + case redoLatestChange |
| 40 | + |
| 41 | + /// Select a range. |
| 42 | + case selectRange(NSRange) |
| 43 | + |
| 44 | + /// Set the text alignment. |
| 45 | + case setAlignment(_ alignment: RichTextAlignment) |
| 46 | + |
| 47 | + /// Set the entire attributed string. |
| 48 | + case setAttributedString(NSAttributedString) |
| 49 | + |
| 50 | + // Change background color |
| 51 | + case setColor(RichTextColor, ColorRepresentable) |
| 52 | + |
| 53 | + // Highlighted renge |
| 54 | + case setHighlightedRange(NSRange?) |
| 55 | + |
| 56 | + // Change highlighting style |
| 57 | + case setHighlightingStyle(RichTextHighlightingStyle) |
| 58 | + |
| 59 | + /// Set a certain ``RichTextStyle``. |
| 60 | + case setStyle(RichTextStyle, Bool) |
| 61 | + |
| 62 | + /// Step the font size. |
| 63 | + case stepFontSize(points: Int) |
| 64 | + |
| 65 | + /// Step the indent level. |
| 66 | + case stepIndent(points: CGFloat) |
| 67 | + |
| 68 | + /// Step the line spacing. |
| 69 | + case stepLineSpacing(points: CGFloat) |
| 70 | + |
| 71 | + /// Step the superscript level. |
| 72 | + case stepSuperscript(steps: Int) |
| 73 | + |
| 74 | + /// Toggle a certain style. |
| 75 | + case toggleStyle(_ style: RichTextStyle) |
| 76 | + |
| 77 | + /// Undo the latest change. |
| 78 | + case undoLatestChange |
| 79 | + |
| 80 | + /// Set HeaderStyle. |
| 81 | + case setHeaderStyle(_ style: RichTextStyle, range: NSRange) |
| 82 | +} |
| 83 | + |
| 84 | +public extension RichTextAction { |
| 85 | + |
| 86 | + typealias Publisher = PassthroughSubject<Self, Never> |
| 87 | + |
| 88 | + /// The action's unique identifier. |
| 89 | + var id: String { UUID().uuidString } |
| 90 | + |
| 91 | + /// The action's standard icon. |
| 92 | + |
| 93 | +} |
| 94 | + |
| 95 | +// MARK: - Aliases |
| 96 | + |
| 97 | +public extension RichTextAction { |
| 98 | + |
| 99 | + /// A name alias for `.redoLatestChange`. |
| 100 | + static var redo: RichTextAction { .redoLatestChange } |
| 101 | + |
| 102 | + /// A name alias for `.undoLatestChange`. |
| 103 | + static var undo: RichTextAction { .undoLatestChange } |
| 104 | +} |
| 105 | + |
| 106 | +public extension CGFloat { |
| 107 | + |
| 108 | + /// The default rich text indent step size. |
| 109 | + static var defaultRichTextIntentStepSize: CGFloat = 30.0 |
| 110 | +} |
| 111 | + |
| 112 | +public extension UInt { |
| 113 | + |
| 114 | + /// The default rich text indent step size. |
| 115 | + static var defaultRichTextIntentStepSize: UInt = 30 |
| 116 | +} |
| 117 | + |
0 commit comments