-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97e8871
commit 382eb14
Showing
28 changed files
with
1,539 additions
and
418 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
Sources/RichEditorSwiftUI/Actions/RichTextAction+KeyboardShortcutModifier.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// RichTextAction+KeyboardShortcutModifier.swift | ||
// RichEditorSwiftUI | ||
// | ||
// Created by Divyesh Vekariya on 30/10/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
public extension RichTextAction { | ||
|
||
/** | ||
This view modifier can apply keyboard shortcuts for any | ||
``RichTextAction`` to any view. | ||
|
||
You can also apply it with the `.keyboardShortcut(for:)` | ||
view modifier. | ||
*/ | ||
struct KeyboardShortcutModifier: ViewModifier { | ||
|
||
public init(_ action: RichTextAction) { | ||
self.action = action | ||
} | ||
|
||
private let action: RichTextAction | ||
|
||
public func body(content: Content) -> some View { | ||
content.keyboardShortcut(for: action) | ||
} | ||
} | ||
} | ||
|
||
public extension View { | ||
|
||
/// Apply a ``RichTextAction/KeyboardShortcutModifier``. | ||
@ViewBuilder | ||
func keyboardShortcut(for action: RichTextAction) -> some View { | ||
#if iOS || macOS || os(visionOS) | ||
switch action { | ||
case .copy: keyboardShortcut("c", modifiers: .command) | ||
case .dismissKeyboard: self | ||
case .print: keyboardShortcut("p", modifiers: .command) | ||
case .redoLatestChange: keyboardShortcut("z", modifiers: [.command, .shift]) | ||
// case .setAlignment(let align): keyboardShortcut(for: align) | ||
case .stepFontSize(let points): keyboardShortcut(points < 0 ? "-" : "+", modifiers: .command) | ||
case .stepIndent(let steps): keyboardShortcut(steps < 0 ? "Ö" : "Ä", modifiers: .command) | ||
case .stepSuperscript: self | ||
// case .toggleStyle(let style): keyboardShortcut(for: style) | ||
case .undoLatestChange: keyboardShortcut("z", modifiers: .command) | ||
default: self // TODO: Probably not defined, object to discuss. | ||
} | ||
#else | ||
self | ||
#endif | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
Sources/RichEditorSwiftUI/Actions/RichTextActionButton.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// RichTextActionButton.swift | ||
// RichEditorSwiftUI | ||
// | ||
// Created by Divyesh Vekariya on 29/10/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
public extension RichTextAction { | ||
|
||
/** | ||
This button can be used to trigger a ``RichTextAction``. | ||
|
||
This renders a plain `Button`, which means that you can | ||
use and configure it as a normal button. | ||
*/ | ||
struct Button: View { | ||
/** | ||
Create a rich text action button. | ||
|
||
- Parameters: | ||
- action: The action to trigger. | ||
- context: The context to affect. | ||
- fillVertically: WhetherP or not fill up vertical space, by default `false`. | ||
*/ | ||
public init( | ||
action: RichTextAction, | ||
context: RichEditorState, | ||
fillVertically: Bool = false | ||
) { | ||
self.action = action | ||
self._context = ObservedObject(wrappedValue: context) | ||
self.fillVertically = fillVertically | ||
} | ||
|
||
private let action: RichTextAction | ||
private let fillVertically: Bool | ||
|
||
@ObservedObject | ||
private var context: RichEditorState | ||
|
||
public var body: some View { | ||
SwiftUI.Button(action: triggerAction) { | ||
action.label | ||
.labelStyle(.iconOnly) | ||
.frame(maxHeight: fillVertically ? .infinity : nil) | ||
.contentShape(Rectangle()) | ||
} | ||
.keyboardShortcut(for: action) | ||
.disabled(!context.canHandle(action)) | ||
} | ||
} | ||
} | ||
|
||
private extension RichTextAction.Button { | ||
|
||
func triggerAction() { | ||
context.handle(action) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.