-
Notifications
You must be signed in to change notification settings - Fork 83
/
RichTextViewContext.swift
87 lines (74 loc) · 3.41 KB
/
RichTextViewContext.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
// RichTextViewContext.swift
// Proton
//
// Created by Rajdeep Kwatra on 14/1/20.
// Copyright © 2020 Rajdeep Kwatra. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import UIKit
class RichTextViewContext: NSObject, UITextViewDelegate {
weak var activeTextView: RichTextView?
weak var selectedTextView: RichTextView?
func textView(_ textView: UITextView, shouldInteractWith textAttachment: NSTextAttachment, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
return interaction != .presentActions
}
func textViewDidChangeSelection(_ textView: UITextView) {
guard textView.delegate === self else { return }
if textView.selectedTextRange != nil {
selectedTextView = textView.asRichTextView
} else {
selectedTextView = nil
}
guard let richTextView = textView as? RichTextView else { return }
let range = textView.selectedRange
resetAttachmentSelection(textView)
guard range.length > 0 else {
if textView.attributedText.length == 0 {
richTextView.resetTypingAttributes()
}
var attributes = richTextView.typingAttributes
let contentType = attributes[.blockContentType] as? EditorContent.Name ?? .unknown
attributes[.blockContentType] = nil
richTextView.richTextViewDelegate?.richTextView(richTextView, didChangeSelection: range, attributes: attributes, contentType: contentType)
return
}
let substring = textView.attributedText.attributedSubstring(from: range)
// Mark attachments as selected if there are any in the selected range
substring.enumerateAttribute(.attachment, in: substring.fullRange, options: .longestEffectiveRangeNotRequired) { attachment, range, _ in
if let attachment = attachment as? Attachment {
attachment.isSelected = true
}
}
var attributes = substring.attributes(at: 0, effectiveRange: nil)
let contentType = attributes[.blockContentType] as? EditorContent.Name ?? .unknown
attributes[.blockContentType] = nil
richTextView.richTextViewDelegate?.richTextView(richTextView, didChangeSelection: range, attributes: attributes, contentType: contentType)
}
func resetAttachmentSelection(_ textView: UITextView) {
guard textView.delegate === self else { return }
guard let attributedText = textView.attributedText else { return }
attributedText.enumerateAttribute(.attachment, in: attributedText.fullRange, options: .longestEffectiveRangeNotRequired) { attachment, _, _ in
if let attachment = attachment as? Attachment {
attachment.isSelected = false
}
}
}
}
extension UITextView {
var asRichTextView: RichTextView? {
self as? RichTextView
}
}