Skip to content

Commit

Permalink
Editor: Improve selector consideration of empty content edit
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Oct 31, 2018
1 parent 75517a2 commit 4c07ae6
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 83 deletions.
36 changes: 21 additions & 15 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,24 +374,30 @@ export function isEditedPostSaveable( state ) {
export function isEditedPostEmpty( state ) {
const blocks = getBlocksForSerialization( 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.
if ( ! blocks.length ) {
return true;
}

const freeFormBlockName = getFreeformContentHandlerName();
// 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;
}

if ( ! some( blocks, { name: freeFormBlockName } ) ) {
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;
}
}

// Content serialization is considered an expensive operation, and should
// only be considered after more trivial operations of assuming presence of
// non-freeform blocks as implying that serializable content exists.
return ! getEditedPostAttribute( state, 'content' );
return ! getEditedPostContent( state );
}

/**
Expand Down
Loading

0 comments on commit 4c07ae6

Please sign in to comment.