From b86b88fa1bf52ff8f2c1520a1b11195c8f3e4134 Mon Sep 17 00:00:00 2001 From: Alexandra Nyitraiova Date: Fri, 25 Aug 2017 13:37:56 +0200 Subject: [PATCH] Add function which checks if content of post is empty --- editor/selectors.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/editor/selectors.js b/editor/selectors.js index 6d9716418cc81..53fac4d7732d3 100644 --- a/editor/selectors.js +++ b/editor/selectors.js @@ -12,6 +12,9 @@ import { serialize, getBlockType } from '@wordpress/blocks'; import { __ } from '@wordpress/i18n'; import { addQueryArgs } from '@wordpress/url'; +const REGEXP_EMPTY_CONTENT = /^$/mg; +const CONTENT_LENGTH_ASSUME_SET = 63; + /** * Returns the current editing mode. * @@ -243,12 +246,23 @@ export function isEditedPostPublishable( state ) { */ export function isEditedPostSaveable( state ) { return ( - !! getEditedPostContent( state ) || + ( ( !! getEditedPostContent( state ) ) && ( ! isEmptyContent( state ) ) ) || !! getEditedPostTitle( state ) || !! getEditedPostExcerpt( state ) ); } +/** + * Check if the content is empty (ignoring empty tags) + * + * @param {Object} state Global application state + * @return {Boolean} Whether it's considered empty + */ +export function isEmptyContent( state ) { + const content = getEditedPostContent( state ); + return ( ! content || ( content.length < CONTENT_LENGTH_ASSUME_SET && REGEXP_EMPTY_CONTENT.test( content ) ) ); +} + /** * Return true if the post being edited is being scheduled. Preferring the * unsaved status values.