diff --git a/WordPress/Classes/Models/BasePost.m b/WordPress/Classes/Models/BasePost.m index c3b10ee13060..322537380092 100644 --- a/WordPress/Classes/Models/BasePost.m +++ b/WordPress/Classes/Models/BasePost.m @@ -107,7 +107,8 @@ - (BOOL)hasContent - (BOOL)isContentEmpty { - return self.content ? self.content.isEmpty : YES; + BOOL isContentAnEmptyGBParagraph = [self.content isEqualToString:@"\n
\n"]; + return self.content ? (self.content.isEmpty || isContentAnEmptyGBParagraph) : YES; } @end diff --git a/WordPress/Classes/ViewRelated/Gutenberg/GutenbergViewController.swift b/WordPress/Classes/ViewRelated/Gutenberg/GutenbergViewController.swift index efad01a7f478..1faaa5469613 100644 --- a/WordPress/Classes/ViewRelated/Gutenberg/GutenbergViewController.swift +++ b/WordPress/Classes/ViewRelated/Gutenberg/GutenbergViewController.swift @@ -610,6 +610,35 @@ extension GutenbergViewController: GutenbergBridgeDelegate { present(alertController, animated: true, completion: nil) } + func showAlertForEmptyPostPublish() { + + let title: String = (self.post is Page) ? EmptyPostActionSheet.titlePage : EmptyPostActionSheet.titlePost + let message: String = EmptyPostActionSheet.message + let alertController = UIAlertController(title: title, message: message, preferredStyle: .actionSheet) + let dismissAction = UIAlertAction(title: MediaAttachmentActionSheet.dismissActionTitle, style: .cancel) { (action) in + + } + alertController.addAction(dismissAction) + + alertController.title = title + alertController.message = message + alertController.popoverPresentationController?.sourceView = view + alertController.popoverPresentationController?.sourceRect = view.frame + alertController.popoverPresentationController?.permittedArrowDirections = .any + present(alertController, animated: true, completion: nil) + } + + func editorHasContent(title: String, content: String) -> Bool { + let hasTitle = !title.isEmpty + var hasContent = !content.isEmpty + if let contentInfo = contentInfo { + let isEmpty = contentInfo.blockCount == 0 + let isOneEmptyParagraph = (contentInfo.blockCount == 1 && contentInfo.paragraphCount == 1 && contentInfo.characterCount == 0) + hasContent = !(isEmpty || isOneEmptyParagraph) + } + return hasTitle || hasContent + } + func gutenbergDidProvideHTML(title: String, html: String, changed: Bool, contentInfo: ContentInfo?) { if changed { self.html = html @@ -622,7 +651,11 @@ extension GutenbergViewController: GutenbergBridgeDelegate { requestHTMLReason = nil // clear the reason switch reason { case .publish: - handlePublishButtonTap() + if editorHasContent(title: title, content: html) { + handlePublishButtonTap() + } else { + showAlertForEmptyPostPublish() + } case .close: cancelEditing() case .more: @@ -938,6 +971,13 @@ private extension GutenbergViewController { } private extension GutenbergViewController { + + struct EmptyPostActionSheet { + static let titlePost = NSLocalizedString("Can't publish an empty post", comment: "Alert message that is shown when trying to publish empty post") + static let titlePage = NSLocalizedString("Can't publish an empty page", comment: "Alert message that is shown when trying to publish empty page") + static let message = NSLocalizedString("Please add some content before trying to publish.", comment: "Suggestion to add content before trying to publish post or page") + } + struct MediaAttachmentActionSheet { static let title = NSLocalizedString("Media Options", comment: "Title for action sheet with media options.") static let dismissActionTitle = NSLocalizedString("Dismiss", comment: "User action to dismiss media options.")