Skip to content

Commit 8b058b9

Browse files
Merge pull request #1281 from wordpress-mobile/issue/1189_invoke_should_change_text_in
Check with the delegate if changes are allowed when custom pastes happens.
2 parents c6a8103 + 7179664 commit 8b058b9

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

Aztec/Classes/Extensions/UITextView+Delegate.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,11 @@ extension UITextView {
1212
delegate?.textViewDidChange?(self)
1313
NotificationCenter.default.post(name: UITextView.textDidChangeNotification, object: self)
1414
}
15+
16+
final func shouldChangeText(in range: NSRange, with text:String) -> Bool {
17+
guard let result = self.delegate?.textView?(self, shouldChangeTextIn: range, replacementText: text) else {
18+
return true
19+
}
20+
return result
21+
}
1522
}

Aztec/Classes/TextKit/TextViewPasteboardDelegate.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ open class AztecTextViewPasteboardDelegate: TextViewPasteboardDelegate {
2828
let selectedRange = textView.selectedRange
2929

3030
if selectedRange.length == 0 {
31+
guard textView.shouldChangeText(in: selectedRange, with: url.absoluteString) else {
32+
return true
33+
}
3134
textView.setLink(url, title:url.absoluteString, inRange: selectedRange)
3235
} else {
3336
textView.setLink(url, inRange: selectedRange)
@@ -45,6 +48,10 @@ open class AztecTextViewPasteboardDelegate: TextViewPasteboardDelegate {
4548
textView.storage.htmlConverter.isSupported(html) else {
4649
return false
4750
}
51+
let string = textView.storage.htmlConverter.attributedString(from: html)
52+
guard textView.shouldChangeText(in: textView.selectedRange, with: string.string) else {
53+
return true
54+
}
4855

4956
textView.replace(textView.selectedRange, withHTML: html)
5057
return true
@@ -60,6 +67,11 @@ open class AztecTextViewPasteboardDelegate: TextViewPasteboardDelegate {
6067
}
6168
string.loadLazyAttachments()
6269
let selectedRange = textView.selectedRange
70+
71+
guard textView.shouldChangeText(in: selectedRange, with: string.string) else {
72+
return true
73+
}
74+
6375
let storage = textView.storage
6476

6577
let finalRange = NSRange(location: selectedRange.location, length: string.length)
@@ -105,6 +117,11 @@ open class AztecTextViewPasteboardDelegate: TextViewPasteboardDelegate {
105117
}
106118

107119
let selectedRange = textView.selectedRange
120+
121+
guard textView.shouldChangeText(in: selectedRange, with: string.string) else {
122+
return true
123+
}
124+
108125
let finalRange = NSRange(location: selectedRange.location, length: string.length)
109126
let originalText = textView.attributedText.attributedSubstring(from: selectedRange)
110127

@@ -133,4 +150,5 @@ open class AztecTextViewPasteboardDelegate: TextViewPasteboardDelegate {
133150

134151
return true
135152
}
153+
136154
}

Example/Example/EditorDemoController.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,10 @@ extension EditorDemoController : UITextViewDelegate {
534534
func scrollViewDidScroll(_ scrollView: UIScrollView) {
535535
updateTitlePosition()
536536
}
537+
538+
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
539+
return true
540+
}
537541
}
538542

539543
extension EditorDemoController : Aztec.TextViewFormattingDelegate {

WordPressEditor/WordPressEditor/Classes/Plugins/WordPressPlugin/Calypso/Embeds/WordPressPasteboardDelegate.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ class WordPressTextViewPasteboardDelegate: AztecTextViewPasteboardDelegate {
1717
}
1818

1919
let result = super.tryPastingString(in: textView)
20-
21-
// Bump the input to the next line – we need the embed link to be the only
22-
// text on this line – otherwise it can't be autoconverted.
23-
textView.insertText(String(.lineSeparator))
20+
if result {
21+
// Bump the input to the next line – we need the embed link to be the only
22+
// text on this line – otherwise it can't be autoconverted.
23+
textView.insertText(String(.lineSeparator))
24+
}
2425

2526
return result
2627
}

0 commit comments

Comments
 (0)