Skip to content

Commit

Permalink
Restore block cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Jan 5, 2021
1 parent 83abcfc commit 0688fcc
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 59 deletions.
1 change: 1 addition & 0 deletions packages/blocks/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export {
getSaveElement,
getSaveContent,
getBlockProps as __unstableGetBlockProps,
__unstableSerializeAndClean,
} from './serializer';

// Validation is the process of comparing a block source with its output before
Expand Down
25 changes: 24 additions & 1 deletion packages/blocks/src/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { isEmpty, reduce, isObject, castArray, startsWith } from 'lodash';
import { Component, cloneElement, renderToString } from '@wordpress/element';
import { hasFilter, applyFilters } from '@wordpress/hooks';
import isShallowEqual from '@wordpress/is-shallow-equal';
import { removep } from '@wordpress/autop';

/**
* Internal dependencies
Expand All @@ -19,7 +20,7 @@ import {
getUnregisteredTypeHandlerName,
hasBlockSupport,
} from './registration';
import { normalizeBlockType } from './utils';
import { isUnmodifiedDefaultBlock, normalizeBlockType } from './utils';
import BlockContentProvider from '../block-content-provider';

/**
Expand Down Expand Up @@ -349,6 +350,28 @@ export function serializeBlock( block, { isInnerBlocks = false } = {} ) {
return getCommentDelimitedContent( blockName, saveAttributes, saveContent );
}

export function __unstableSerializeAndClean( blocks ) {
// A single unmodified default block is assumed to
// be equivalent to an empty post.
if ( blocks.length === 1 && isUnmodifiedDefaultBlock( blocks[ 0 ] ) ) {
blocks = [];
}

let content = serialize( blocks );

// For compatibility, treat a post consisting of a
// single freeform block as legacy content and apply
// pre-block-editor removep'd content formatting.
if (
blocks.length === 1 &&
blocks[ 0 ].name === getFreeformContentHandlerName()
) {
content = removep( content );
}

return content;
}

/**
* Takes a block or set of blocks and returns the serialized post content.
*
Expand Down
4 changes: 2 additions & 2 deletions packages/core-data/src/entity-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
useEffect,
} from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { parse, serialize } from '@wordpress/blocks';
import { parse, __unstableSerializeAndClean } from '@wordpress/blocks';

const EMPTY_ARRAY = [];

Expand Down Expand Up @@ -183,7 +183,7 @@ export function useEntityBlockEditor( kind, type, { id: _id } = {} ) {
// to make sure the edit makes the post dirty and creates
// a new undo level.
edits.content = ( { blocks: blocksForSerialization = [] } ) =>
serialize( blocksForSerialization );
__unstableSerializeAndClean( blocksForSerialization );

editEntityRecord( 'postType', type, id, edits );
},
Expand Down
9 changes: 6 additions & 3 deletions packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import { has } from 'lodash';
import deprecated from '@wordpress/deprecated';
import { controls } from '@wordpress/data';
import { apiFetch } from '@wordpress/data-controls';
import { parse, synchronizeBlocksWithTemplate } from '@wordpress/blocks';
import {
parse,
synchronizeBlocksWithTemplate,
__unstableSerializeAndClean,
} from '@wordpress/blocks';
import { store as noticesStore } from '@wordpress/notices';

/**
Expand All @@ -21,7 +25,6 @@ import {
getNotificationArgumentsForSaveFail,
getNotificationArgumentsForTrashFail,
} from './utils/notice-builder';
import serializeBlocks from './utils/serialize-blocks';

/**
* Returns an action generator used in signalling that editor has initialized with
Expand Down Expand Up @@ -624,7 +627,7 @@ export function* resetEditorBlocks( blocks, options = {} ) {
// to make sure the edit makes the post dirty and creates
// a new undo level.
edits.content = ( { blocks: blocksForSerialization = [] } ) =>
serializeBlocks( blocksForSerialization );
__unstableSerializeAndClean( blocksForSerialization );
}
yield* editPost( edits );
}
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
getFreeformContentHandlerName,
getDefaultBlockName,
isUnmodifiedDefaultBlock,
__unstableSerializeAndClean,
} from '@wordpress/blocks';
import { isInTheFuture, getDate } from '@wordpress/date';
import { addQueryArgs } from '@wordpress/url';
Expand All @@ -38,7 +39,6 @@ import {
AUTOSAVE_PROPERTIES,
} from './constants';
import { getPostRawValue } from './reducer';
import serializeBlocks from './utils/serialize-blocks';
import { cleanForSlug } from '../utils/url';

/**
Expand Down Expand Up @@ -991,7 +991,7 @@ export const getEditedPostContent = createRegistrySelector(
if ( typeof record.content === 'function' ) {
return record.content( record );
} else if ( record.blocks ) {
return serializeBlocks( record.blocks );
return __unstableSerializeAndClean( record.blocks );
} else if ( record.content ) {
return record.content;
}
Expand Down
51 changes: 0 additions & 51 deletions packages/editor/src/store/utils/serialize-blocks.js

This file was deleted.

0 comments on commit 0688fcc

Please sign in to comment.