Skip to content

Commit

Permalink
Merge pull request #14329 from wordpress-mobile/gutenberg/dont_save_e…
Browse files Browse the repository at this point in the history
…mtpy_posts

Gutenberg: Don't save empty posts
  • Loading branch information
SergioEstevao authored Jun 26, 2020
2 parents 972af68 + bb48229 commit eb810f8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
3 changes: 2 additions & 1 deletion WordPress/Classes/Models/BasePost.m
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ - (BOOL)hasContent

- (BOOL)isContentEmpty
{
return self.content ? self.content.isEmpty : YES;
BOOL isContentAnEmptyGBParagraph = [self.content isEqualToString:@"<!-- wp:paragraph -->\n<p></p>\n<!-- /wp:paragraph -->"];
return self.content ? (self.content.isEmpty || isContentAnEmptyGBParagraph) : YES;
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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.")
Expand Down

0 comments on commit eb810f8

Please sign in to comment.