-
Notifications
You must be signed in to change notification settings - Fork 13
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
Adding set/remove link methods. #87
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,14 +11,22 @@ class RCTAztecView: Aztec.TextView { | |
@objc var onContentSizeChange: RCTBubblingEventBlock? = nil | ||
@objc var onSelectionChange: RCTBubblingEventBlock? = nil | ||
@objc var onActiveFormatsChange: RCTBubblingEventBlock? = nil | ||
|
||
@objc var onActiveFormatAttributesChange: RCTBubblingEventBlock? = nil | ||
|
||
private var previousContentSize: CGSize = .zero | ||
|
||
private lazy var placeholderLabel: UILabel = { | ||
let label = UILabel(frame: .zero) | ||
return label | ||
}() | ||
|
||
private let formatStringMap: [FormattingIdentifier: String] = [ | ||
.bold: "Bold", | ||
.italic: "italic", | ||
.strikethrough: "strikethrough", | ||
.link: "link", | ||
] | ||
|
||
override init(defaultFont: UIFont, defaultParagraphStyle: ParagraphStyle, defaultMissingImage: UIImage) { | ||
super.init(defaultFont: defaultFont, defaultParagraphStyle: defaultParagraphStyle, defaultMissingImage: defaultMissingImage) | ||
commonInit() | ||
|
@@ -182,6 +190,35 @@ class RCTAztecView: Aztec.TextView { | |
default: print("Format not recognized") | ||
} | ||
} | ||
|
||
@objc | ||
func setLink(with url: String, and title: String?) { | ||
guard let url = URL(string: url) else { | ||
return | ||
} | ||
if let title = title { | ||
setLink(url, title: title, inRange: selectedRange) | ||
} else { | ||
setLink(url, inRange: selectedRange) | ||
} | ||
} | ||
|
||
@objc | ||
func removeLink() { | ||
guard let expandedRange = linkFullRange(forRange: selectedRange) else { | ||
return | ||
} | ||
removeLink(inRange: expandedRange) | ||
} | ||
|
||
func linkAttributes() -> [String: Any] { | ||
var attributes: [String: Any] = ["isActive": false] | ||
if let expandedRange = linkFullRange(forRange: selectedRange) { | ||
attributes["url"] = linkURL(forRange: expandedRange)?.absoluteString ?? "" | ||
attributes["isActive"] = true | ||
} | ||
return attributes | ||
} | ||
|
||
// MARK: - Event Propagation | ||
|
||
|
@@ -202,17 +239,21 @@ class RCTAztecView: Aztec.TextView { | |
} else { | ||
identifiers = formatIdentifiersForTypingAttributes() | ||
} | ||
let formats = identifiers.compactMap( { (identifier) -> String? in | ||
switch identifier { | ||
case .bold: return "bold" | ||
case .italic: return "italic" | ||
case .strikethrough: return "strikethrough" | ||
default: return nil | ||
} | ||
}) | ||
let formats = identifiers.compactMap(formatString) | ||
onActiveFormatsChange(["formats": formats]) | ||
} | ||
|
||
func propagateAttributesChanges() { | ||
let attributes: [String: [String: Any]] = [ | ||
"link": linkAttributes() | ||
] | ||
onActiveFormatAttributesChange?(["attributes": attributes]) | ||
} | ||
|
||
private func formatString(from identifier: FormattingIdentifier) -> String? { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Considering that:
I'm wondering if we really need this to be a method and not just a closure, such as |
||
return formatStringMap[identifier] | ||
} | ||
|
||
func propagateSelectionChanges() { | ||
guard let onSelectionChange = onSelectionChange else { | ||
return | ||
|
@@ -226,6 +267,7 @@ class RCTAztecView: Aztec.TextView { | |
extension RCTAztecView: UITextViewDelegate { | ||
|
||
func textViewDidChangeSelection(_ textView: UITextView) { | ||
propagateAttributesChanges() | ||
propagateFormatChanges() | ||
propagateSelectionChanges() | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,10 +12,13 @@ @interface RCT_EXTERN_MODULE(RCTAztecViewManager, NSObject) | |
RCT_EXPORT_VIEW_PROPERTY(onSelectionChange, RCTDirectEventBlock) | ||
|
||
RCT_EXPORT_VIEW_PROPERTY(onActiveFormatsChange, RCTBubblingEventBlock) | ||
RCT_EXPORT_VIEW_PROPERTY(onActiveFormatAttributesChange, RCTBubblingEventBlock) | ||
|
||
RCT_EXPORT_VIEW_PROPERTY(placeholder, NSString) | ||
RCT_EXPORT_VIEW_PROPERTY(placeholderTextColor, UIColor) | ||
|
||
RCT_EXTERN_METHOD(applyFormat:(nonnull NSNumber *)node format:(NSString *)format) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think we could use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh right, applyFormat only accepts a string (the format) as parameter, I guess we need something else for the url 👍 |
||
RCT_EXTERN_METHOD(setLink:(nonnull NSNumber *)node url:(nonnull NSString *)url title:(nullable NSString *)title) | ||
RCT_EXTERN_METHOD(removeLink:(nonnull NSNumber *)node) | ||
|
||
@end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The initial character in the string here is capitalized, but not for the others. I'd suggest to standardize for clarity.