Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gutenberg: Don't save empty posts #14329

Merged
merged 6 commits into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -602,6 +602,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 @@ -614,7 +643,11 @@ extension GutenbergViewController: GutenbergBridgeDelegate {
requestHTMLReason = nil // clear the reason
switch reason {
case .publish:
handlePublishButtonTap()
if editorHasContent(title: title, content: html) {
SergioEstevao marked this conversation as resolved.
Show resolved Hide resolved
handlePublishButtonTap()
} else {
showAlertForEmptyPostPublish()
}
case .close:
cancelEditing()
case .more:
Expand Down Expand Up @@ -930,6 +963,13 @@ private extension GutenbergViewController {
}

private extension GutenbergViewController {

struct EmptyPostActionSheet {
static let titlePost = NSLocalizedString("Can't publish an empty post", comment: "Alert message that is show when trying to publish empty post")
static let titlePage = NSLocalizedString("Can't publish an empty page", comment: "Alert message that is show when trying to publish empty page")
static let message = NSLocalizedString("Please write some content before trying to publish.", comment: "Suggestion to write content before trying to publish post or page")
}
SergioEstevao marked this conversation as resolved.
Show resolved Hide resolved

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