Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into issue/1431-dark-mode
- Loading branch information
Showing
17 changed files
with
451 additions
and
27 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
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
217 changes: 217 additions & 0 deletions
217
WooCommerce/Classes/ViewRelated/Editor/AztecEditorViewController.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,217 @@ | ||
import UIKit | ||
import Aztec | ||
import WordPressEditor | ||
|
||
/// Aztec's Native Editor! | ||
final class AztecEditorViewController: UIViewController, Editor { | ||
var onContentSave: OnContentSave? | ||
|
||
private let content: String | ||
|
||
private let viewProperties: EditorViewProperties | ||
|
||
private let aztecUIConfigurator = AztecUIConfigurator() | ||
|
||
/// The editor view. | ||
/// | ||
private(set) lazy var editorView: Aztec.EditorView = { | ||
|
||
let paragraphStyle = ParagraphStyle.default | ||
paragraphStyle.lineSpacing = 4 | ||
|
||
let missingIcon = UIImage.errorStateImage | ||
|
||
let editorView = Aztec.EditorView( | ||
defaultFont: StyleManager.subheadlineFont, | ||
defaultHTMLFont: StyleManager.subheadlineFont, | ||
defaultParagraphStyle: paragraphStyle, | ||
defaultMissingImage: missingIcon) | ||
|
||
aztecUIConfigurator.configureEditorView(editorView, | ||
textViewDelegate: self, | ||
textViewAttachmentDelegate: textViewAttachmentDelegate) | ||
|
||
return editorView | ||
}() | ||
|
||
/// Aztec's Awesomeness | ||
/// | ||
private var richTextView: Aztec.TextView { | ||
return editorView.richTextView | ||
} | ||
|
||
/// Aztec's Raw HTML Editor | ||
/// | ||
private var htmlTextView: UITextView { | ||
return editorView.htmlTextView | ||
} | ||
|
||
/// Aztec's Text Placeholder | ||
/// | ||
private lazy var placeholderLabel: UILabel = { | ||
let label = UILabel() | ||
label.text = NSLocalizedString("Start writing...", comment: "Aztec's Text Placeholder") | ||
label.textColor = StyleManager.wooGreyMid | ||
label.font = StyleManager.subheadlineFont | ||
label.isUserInteractionEnabled = false | ||
label.translatesAutoresizingMaskIntoConstraints = false | ||
label.numberOfLines = 0 | ||
return label | ||
}() | ||
|
||
private lazy var keyboardFrameObserver: KeyboardFrameObserver = { | ||
let keyboardFrameObserver = KeyboardFrameObserver(onKeyboardFrameUpdate: handleKeyboardFrameUpdate(keyboardFrame:)) | ||
return keyboardFrameObserver | ||
}() | ||
|
||
private let textViewAttachmentDelegate: TextViewAttachmentDelegate | ||
|
||
required init(content: String?, | ||
viewProperties: EditorViewProperties, | ||
textViewAttachmentDelegate: TextViewAttachmentDelegate = AztecTextViewAttachmentHandler()) { | ||
self.content = content ?? "" | ||
self.textViewAttachmentDelegate = textViewAttachmentDelegate | ||
self.viewProperties = viewProperties | ||
super.init(nibName: nil, bundle: nil) | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
registerAttachmentImageProviders() | ||
|
||
configureNavigationBar() | ||
configureView() | ||
configureSubviews() | ||
|
||
aztecUIConfigurator.configureConstraints(editorView: editorView, | ||
editorContainerView: view, | ||
placeholderView: placeholderLabel) | ||
|
||
setHTML(content) | ||
|
||
refreshPlaceholderVisibility() | ||
} | ||
|
||
override func viewWillAppear(_ animated: Bool) { | ||
super.viewWillAppear(animated) | ||
startListeningToNotifications() | ||
} | ||
} | ||
|
||
private extension AztecEditorViewController { | ||
func configureNavigationBar() { | ||
title = viewProperties.navigationTitle | ||
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(saveButtonTapped)) | ||
} | ||
|
||
func configureView() { | ||
edgesForExtendedLayout = UIRectEdge() | ||
view.backgroundColor = StyleManager.wooWhite | ||
} | ||
|
||
func configureSubviews() { | ||
view.addSubview(richTextView) | ||
view.addSubview(htmlTextView) | ||
view.addSubview(placeholderLabel) | ||
} | ||
|
||
func registerAttachmentImageProviders() { | ||
let providers: [TextViewAttachmentImageProvider] = [ | ||
SpecialTagAttachmentRenderer(), | ||
CommentAttachmentRenderer(font: StyleManager.subheadlineBoldFont), | ||
HTMLAttachmentRenderer(font: StyleManager.subheadlineBoldFont), | ||
GutenpackAttachmentRenderer() | ||
] | ||
|
||
for provider in providers { | ||
richTextView.registerAttachmentImageProvider(provider) | ||
} | ||
} | ||
} | ||
|
||
private extension AztecEditorViewController { | ||
func setHTML(_ html: String) { | ||
editorView.setHTML(html) | ||
} | ||
|
||
func getHTML() -> String { | ||
return editorView.getHTML() | ||
} | ||
|
||
func refreshPlaceholderVisibility() { | ||
placeholderLabel.isHidden = richTextView.isHidden || !richTextView.text.isEmpty | ||
} | ||
} | ||
|
||
// MARK: Keyboard frame update handling | ||
// | ||
private extension AztecEditorViewController { | ||
func handleKeyboardFrameUpdate(keyboardFrame: CGRect) { | ||
let referenceView = editorView.activeView | ||
|
||
let bottomInset = view.frame.maxY - (keyboardFrame.minY + self.view.layoutMargins.bottom) | ||
let contentInsets = UIEdgeInsets(top: referenceView.contentInset.top, | ||
left: 0, | ||
bottom: max(0, bottomInset), | ||
right: 0) | ||
|
||
htmlTextView.contentInset = contentInsets | ||
richTextView.contentInset = contentInsets | ||
|
||
updateScrollInsets() | ||
} | ||
|
||
func updateScrollInsets() { | ||
let referenceView = editorView.activeView | ||
var scrollInsets = referenceView.contentInset | ||
var rightMargin = (view.frame.maxX - referenceView.frame.maxX) | ||
rightMargin -= view.safeAreaInsets.right | ||
scrollInsets.right = -rightMargin | ||
referenceView.scrollIndicatorInsets = scrollInsets | ||
} | ||
} | ||
|
||
// MARK: - Notifications | ||
// | ||
private extension AztecEditorViewController { | ||
func startListeningToNotifications() { | ||
keyboardFrameObserver.startObservingKeyboardFrame() | ||
} | ||
} | ||
|
||
// MARK: - Navigation actions | ||
// | ||
private extension AztecEditorViewController { | ||
@objc func saveButtonTapped() { | ||
let content = getHTML() | ||
onContentSave?(content) | ||
|
||
navigationController?.popViewController(animated: true) | ||
} | ||
} | ||
|
||
// MARK: - UITextViewDelegate methods | ||
// | ||
extension AztecEditorViewController: UITextViewDelegate { | ||
|
||
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { | ||
return true | ||
} | ||
|
||
func textViewDidChangeSelection(_ textView: UITextView) { | ||
refreshPlaceholderVisibility() | ||
} | ||
|
||
func textViewDidChange(_ textView: UITextView) { | ||
refreshPlaceholderVisibility() | ||
} | ||
|
||
func textViewShouldBeginEditing(_ textView: UITextView) -> Bool { | ||
return true | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
WooCommerce/Classes/ViewRelated/Editor/AztecTextViewAttachmentHandler.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,24 @@ | ||
import Aztec | ||
|
||
/// Implements Aztec's `TextViewAttachmentDelegate` without media support. | ||
final class AztecTextViewAttachmentHandler: TextViewAttachmentDelegate { | ||
func textView(_ textView: TextView, attachment: NSTextAttachment, imageAt url: URL, onSuccess success: @escaping (UIImage) -> Void, onFailure failure: @escaping () -> Void) { | ||
} | ||
|
||
func textView(_ textView: TextView, urlFor imageAttachment: ImageAttachment) -> URL? { | ||
return nil | ||
} | ||
|
||
func textView(_ textView: TextView, placeholderFor attachment: NSTextAttachment) -> UIImage { | ||
return UIImage.cameraImage | ||
} | ||
|
||
func textView(_ textView: TextView, deletedAttachment attachment: MediaAttachment) { | ||
} | ||
|
||
func textView(_ textView: TextView, selected attachment: NSTextAttachment, atPosition position: CGPoint) { | ||
} | ||
|
||
func textView(_ textView: TextView, deselected attachment: NSTextAttachment, atPosition position: CGPoint) { | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
WooCommerce/Classes/ViewRelated/Editor/AztecUIConfigurator.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,94 @@ | ||
import Aztec | ||
import UIKit | ||
import WordPressEditor | ||
|
||
/// Configures the Aztec UI components, like the styling and Auto Layout constraints. | ||
struct AztecUIConfigurator { | ||
func configureEditorView(_ editorView: EditorView, | ||
textViewDelegate: UITextViewDelegate, | ||
textViewAttachmentDelegate: TextViewAttachmentDelegate) { | ||
editorView.clipsToBounds = false | ||
configureHTMLTextView(editorView.htmlTextView, textViewDelegate: textViewDelegate) | ||
configureRichTextView(editorView.richTextView, | ||
textViewDelegate: textViewDelegate, | ||
textViewAttachmentDelegate: textViewAttachmentDelegate) | ||
} | ||
|
||
func configureConstraints(editorView: EditorView, editorContainerView: UIView, placeholderView: UIView) { | ||
let richTextView = editorView.richTextView | ||
let htmlTextView = editorView.htmlTextView | ||
|
||
NSLayoutConstraint.activate([ | ||
richTextView.leadingAnchor.constraint(equalTo: editorContainerView.readableContentGuide.leadingAnchor), | ||
richTextView.trailingAnchor.constraint(equalTo: editorContainerView.readableContentGuide.trailingAnchor), | ||
richTextView.topAnchor.constraint(equalTo: editorContainerView.topAnchor), | ||
richTextView.bottomAnchor.constraint(equalTo: editorContainerView.bottomAnchor) | ||
]) | ||
|
||
NSLayoutConstraint.activate([ | ||
htmlTextView.leftAnchor.constraint(equalTo: richTextView.leftAnchor), | ||
htmlTextView.rightAnchor.constraint(equalTo: richTextView.rightAnchor), | ||
htmlTextView.topAnchor.constraint(equalTo: richTextView.topAnchor), | ||
htmlTextView.bottomAnchor.constraint(equalTo: richTextView.bottomAnchor) | ||
]) | ||
|
||
let insets = richTextView.textContainerInset | ||
|
||
NSLayoutConstraint.activate([ | ||
placeholderView.leftAnchor.constraint(equalTo: richTextView.leftAnchor, constant: insets.left + richTextView.textContainer.lineFragmentPadding), | ||
placeholderView.rightAnchor.constraint(equalTo: richTextView.rightAnchor, constant: -insets.right - richTextView.textContainer.lineFragmentPadding), | ||
placeholderView.topAnchor.constraint(equalTo: richTextView.topAnchor, constant: insets.top), | ||
placeholderView.bottomAnchor.constraint(lessThanOrEqualTo: richTextView.bottomAnchor, constant: insets.bottom) | ||
]) | ||
} | ||
} | ||
|
||
private extension AztecUIConfigurator { | ||
func configureHTMLTextView(_ textView: UITextView, textViewDelegate: UITextViewDelegate) { | ||
let accessibilityLabel = NSLocalizedString("HTML Content", comment: "Post HTML content") | ||
configureDefaultProperties(for: textView, accessibilityLabel: accessibilityLabel) | ||
|
||
textView.isHidden = true | ||
textView.delegate = textViewDelegate | ||
textView.accessibilityIdentifier = "HTMLContentView" | ||
textView.autocorrectionType = .no | ||
textView.autocapitalizationType = .none | ||
|
||
// We need this false to be able to set negative `scrollInset` values. | ||
textView.clipsToBounds = false | ||
|
||
textView.adjustsFontForContentSizeCategory = true | ||
textView.smartDashesType = .no | ||
textView.smartQuotesType = .no | ||
} | ||
|
||
func configureRichTextView(_ textView: TextView, | ||
textViewDelegate: UITextViewDelegate, | ||
textViewAttachmentDelegate: TextViewAttachmentDelegate) { | ||
textView.load(WordPressPlugin()) | ||
|
||
let accessibilityLabel = NSLocalizedString("Rich Content", comment: "Post Rich content") | ||
configureDefaultProperties(for: textView, accessibilityLabel: accessibilityLabel) | ||
|
||
let linkAttributes: [NSAttributedString.Key: Any] = [.underlineStyle: NSUnderlineStyle.single.rawValue, | ||
.foregroundColor: StyleManager.wooCommerceBrandColor] | ||
|
||
textView.delegate = textViewDelegate | ||
textView.textAttachmentDelegate = textViewAttachmentDelegate | ||
textView.backgroundColor = StyleManager.wooWhite | ||
textView.linkTextAttributes = linkAttributes | ||
|
||
// We need this false to be able to set negative `scrollInset` values. | ||
textView.clipsToBounds = false | ||
|
||
textView.smartDashesType = .no | ||
textView.smartQuotesType = .no | ||
} | ||
|
||
func configureDefaultProperties(for textView: UITextView, accessibilityLabel: String) { | ||
textView.accessibilityLabel = accessibilityLabel | ||
textView.keyboardDismissMode = .interactive | ||
textView.textColor = UIColor.darkText | ||
textView.translatesAutoresizingMaskIntoConstraints = false | ||
} | ||
} |
Oops, something went wrong.