Skip to content

Commit

Permalink
Stop trying to autosave when title and classic block content both are…
Browse files Browse the repository at this point in the history
… empty. (#10404)

* Do not auto-update if title and classic block content is empty.

* Update isEditedPostEmpty: use hasKnownBlocks and check if freeform or unregistered or empty content block is available in post.

* Update isEditablePost logic to check length of availalbe block first.

* Editor: Improve selector consideration of empty content edit
  • Loading branch information
rahulsprajapati authored and danielbachhuber committed Oct 31, 2018
1 parent f3451ad commit fae7777
Show file tree
Hide file tree
Showing 2 changed files with 274 additions and 31 deletions.
34 changes: 26 additions & 8 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,32 @@ export function isEditedPostSaveable( state ) {
* @return {boolean} Whether post has content.
*/
export function isEditedPostEmpty( state ) {
// While the condition of truthy content string would be sufficient for
// determining emptiness, testing saveable blocks length is a trivial
// operation by comparison. Since this function can be called frequently,
// optimize for the fast case where saveable blocks are non-empty.
return (
! getBlocksForSerialization( state ).length &&
! getEditedPostAttribute( state, 'content' )
);
const blocks = getBlocksForSerialization( state );

// While the condition of truthy content string is sufficient to determine
// emptiness, testing saveable blocks length is a trivial operation. Since
// this function can be called frequently, optimize for the fast case as a
// condition of the mere existence of blocks. Note that the value of edited
// content is used in place of blocks, thus allowed to fall through.
if ( blocks.length && ! ( 'content' in getPostEdits( state ) ) ) {
// Pierce the abstraction of the serializer in knowing that blocks are
// joined with with newlines such that even if every individual block
// produces an empty save result, the serialized content is non-empty.
if ( blocks.length > 1 ) {
return false;
}

// Freeform and unregistered blocks omit comment delimiters in their
// output. The freeform block specifically may produce an empty string
// to save. In the case of a single freeform block, fall through to the
// full serialize. Otherwise, the single block is assumed non-empty by
// virtue of its comment delimiters.
if ( blocks[ 0 ].name !== getFreeformContentHandlerName() ) {
return false;
}
}

return ! getEditedPostContent( state );
}

/**
Expand Down
Loading

0 comments on commit fae7777

Please sign in to comment.