-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Allow setting custom menu actions in the textview
- Loading branch information
Showing
7 changed files
with
159 additions
and
18 deletions.
There are no files selected for viewing
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
51 changes: 51 additions & 0 deletions
51
Sources/AuroraEditorTextView/TextView/Menu/CustomMenuItem.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,51 @@ | ||
// | ||
// CustomMenuItem.swift | ||
// | ||
// | ||
// Created by Nanashi Li on 2024/06/21. | ||
// | ||
|
||
import ObjectiveC | ||
|
||
/// A concrete implementation of ``MenuItemProvider``. | ||
/// | ||
/// This struct provides a convenient way to create menu items, including separators. | ||
public struct CustomMenuItem: MenuItemProvider { | ||
/// The title of the menu item. | ||
public let title: String | ||
|
||
/// The action to be performed when the menu item is selected. | ||
public let action: Selector? | ||
|
||
/// The key equivalent for the menu item. | ||
public let keyEquivalent: String | ||
|
||
/// Indicates whether this item represents a separator in the menu. | ||
public let isSeparator: Bool | ||
|
||
/// Creates a new CustomMenuItem. | ||
/// | ||
/// - Parameters: | ||
/// - title: The title of the menu item. Defaults to an empty string for separators. | ||
/// - action: The action to be performed when the menu item is selected. Defaults to `nil`. | ||
/// - keyEquivalent: The key equivalent for the menu item. Defaults to an empty string. | ||
/// - isSeparator: Whether this item should be a separator. Defaults to `false`. | ||
public init(title: String, | ||
action: Selector? = nil, | ||
keyEquivalent: String = "", | ||
isSeparator: Bool = false) { | ||
self.title = title | ||
self.action = action | ||
self.keyEquivalent = keyEquivalent | ||
self.isSeparator = isSeparator | ||
} | ||
|
||
/// Creates a separator menu item. | ||
/// | ||
/// This is a convenience method for creating a separator menu item. | ||
/// | ||
/// - Returns: A CustomMenuItem configured as a separator. | ||
public static func separator() -> CustomMenuItem { | ||
return CustomMenuItem(title: "", isSeparator: true) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
Sources/AuroraEditorTextView/TextView/Menu/MenuItemProvider.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,33 @@ | ||
// | ||
// MenuItemProvider.swift | ||
// | ||
// | ||
// Created by Nanashi Li on 2024/06/21. | ||
// | ||
|
||
import ObjectiveC | ||
|
||
/// Represents a single menu item in a context menu. | ||
/// | ||
/// This protocol defines the essential properties of a menu item, including its title, action, | ||
/// key equivalent, and whether it's a separator. | ||
public protocol MenuItemProvider { | ||
/// The title of the menu item. | ||
var title: String { get } | ||
|
||
/// The action to be performed when the menu item is selected. | ||
/// | ||
/// - Note: This property is optional. If `nil`, the menu item will be disabled. | ||
var action: Selector? { get } | ||
|
||
/// The key equivalent for the menu item. | ||
/// | ||
/// This is a string representing the keyboard shortcut for the menu item. | ||
/// For example, "cmd+c" for copy. | ||
var keyEquivalent: String { get } | ||
|
||
/// Indicates whether this item represents a separator in the menu. | ||
/// | ||
/// If `true`, this item will be displayed as a separator line in the menu. | ||
var isSeparator: Bool { get } | ||
} |
21 changes: 21 additions & 0 deletions
21
Sources/AuroraEditorTextView/TextView/Menu/MenuItemsSource.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,21 @@ | ||
// | ||
// MenuItemsSource.swift | ||
// | ||
// | ||
// Created by Nanashi Li on 2024/06/21. | ||
// | ||
|
||
/// A source of menu items for a TextView. | ||
/// | ||
/// Conform to this protocol to provide custom menu items for a TextView. | ||
/// | ||
/// - Important: This protocol inherits from AnyObject, which means only class types can conform to it. | ||
/// | ||
/// - Note: The implementing type is responsible for creating and returning an array of ``MenuItemProvider`` objects. | ||
public protocol MenuItemsSource: AnyObject { | ||
/// Provides an array of menu items for the given TextView. | ||
/// | ||
/// - Parameter textView: The TextView requesting the menu items. | ||
/// - Returns: An array of ``MenuItemProvider`` objects representing the menu items. | ||
func menuItems(for textView: TextView) -> [MenuItemProvider] | ||
} |
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