Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize the Edit Menu! #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"originHash" : "cf25ad6b94f02765d03b56d831794bbda3a3babd339e9cf04bd230e98b5dad2e",
"pins" : [
{
"identity" : "down",
"kind" : "remoteSourceControl",
"location" : "https://github.com/johnxnguyen/Down.git",
"state" : {
"branch" : "master",
"revision" : "e754ab1c80920dd51a8e08290c912ac1c2ac8b58"
}
},
{
"identity" : "highlightswift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/appstefan/highlightswift.git",
"state" : {
"revision" : "1ee708afe9473828f36d0202722c9cc065d74110",
"version" : "1.0.7"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "d029d9d39c87bed85b1c50adee7c41795261a192",
"version" : "1.0.6"
}
}
],
"version" : 3
}
39 changes: 36 additions & 3 deletions Sources/SelectableMarkdownUI/View/AttributedTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ struct AttributedTextView: UIViewControllerRepresentable {
let text: NSAttributedString

private let textView = ContentTextView()


init(text: NSAttributedString, editMenuActions: [ EditMenuAction ]? = nil, didChangeHeight: @escaping (CGFloat) -> Void) {
self.text = text
textView.editMenuActions = editMenuActions ?? []
self.didChangeHeight = didChangeHeight
}

func makeUIViewController(context: Context) -> UIViewControllerType {
let vc = UIViewController()

textView.contentInset = .zero
textView.isSelectable = true
textView.isEditable = false

textView.backgroundColor = .clear
textView.isScrollEnabled = false
textView.attributedText = text
Expand Down Expand Up @@ -56,11 +62,38 @@ struct AttributedTextView: UIViewControllerRepresentable {
/// ContentTextView
/// subclass of UITextView returning contentSize as intrinsicContentSize
private class ContentTextView: UITextView {
// override var canBecomeFirstResponder: Bool { false }
var editMenuActions = [ EditMenuAction ]()

override var intrinsicContentSize: CGSize {
let x = frame.height > 0 ? contentSize : super.intrinsicContentSize
return super.intrinsicContentSize
}

override func editMenu(for textRange: UITextRange, suggestedActions: [UIMenuElement]) -> UIMenu? {
var actions = suggestedActions

editMenuActions.reversed().forEach { actionTemplate in
let action = UIAction(title: actionTemplate.label) { (action) in
if let range = self.selectedTextRange,
let selectedText = self.text(in: range) {
actionTemplate.action(selectedText)
}
}

actions.insert(action, at: 0)
}

return UIMenu(children: actions)
}
}
}

public struct EditMenuAction {
var label: String
var action: (String) -> Void

public init(label: String, action: @escaping (String) -> Void) {
self.label = label
self.action = action
}
}
27 changes: 25 additions & 2 deletions Sources/SelectableMarkdownUI/View/SelectableMarkdownView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,23 @@ public struct SelectableMarkdownView: View {
@Environment(\.colorScheme) var colorScheme: ColorScheme
@State private var formattedText: NSAttributedString
@State private var messageHeight: CGFloat = 0

let text: String
let editMenuActions: [ EditMenuAction ]?

let styler: CodeStyler = CodeStyler.sharedInstance
let down: Down

public init(text: String) {
public init(text: String, editMenuActions: [ EditMenuAction ]? = nil) {
self.text = text
self.editMenuActions = editMenuActions

self.down = Down(markdownString: text)
self.formattedText = (try? down.toAttributedString(styler: styler)) ?? NSAttributedString(string: text)
}

public var body: some View {
AttributedTextView(text: formattedText) { newHeight in
AttributedTextView(text: formattedText, editMenuActions: editMenuActions) { newHeight in
self.messageHeight = newHeight
}
.frame(height: messageHeight)
Expand All @@ -46,3 +51,21 @@ public struct SelectableMarkdownView: View {
}
}
}

#Preview("Simple") {
SelectableMarkdownView(text: "This is just a *simple* example")
}

#Preview("With Actions") {
SelectableMarkdownView(
text: "This is just a *simple* example",
editMenuActions: [
EditMenuAction(label: "Print Hightlight") {
print($0)
},
EditMenuAction(label: "Second one") {
print("Second: `\($0)`")
}
]
)
}