From 1f1cdb628a7bc5b08f5cabf63d9100a841d29105 Mon Sep 17 00:00:00 2001 From: epiqueras Date: Tue, 13 Aug 2019 11:39:44 -0700 Subject: [PATCH] Editor: Make `serializeBlocks` a util. --- .../developers/data/data-core-editor.md | 12 ----- packages/editor/src/store/actions.js | 46 +---------------- packages/editor/src/store/selectors.js | 2 +- packages/editor/src/store/test/selectors.js | 2 +- .../src/store/utils/serialize-blocks.js | 51 +++++++++++++++++++ 5 files changed, 55 insertions(+), 58 deletions(-) create mode 100644 packages/editor/src/store/utils/serialize-blocks.js diff --git a/docs/designers-developers/developers/data/data-core-editor.md b/docs/designers-developers/developers/data/data-core-editor.md index 82fffb3387cce2..905e9cc077847d 100644 --- a/docs/designers-developers/developers/data/data-core-editor.md +++ b/docs/designers-developers/developers/data/data-core-editor.md @@ -1261,18 +1261,6 @@ _Related_ - selectBlock in core/block-editor store. -# **serializeBlocks** - -Serializes blocks following backwards compatibility conventions. - -_Parameters_ - -- _blocksForSerialization_ `Array`: The blocks to serialize. - -_Returns_ - -- `string`: The blocks serialization. - # **setTemplateValidity** _Related_ diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index 30d577179c9cbf..f181e21e357647 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -2,21 +2,13 @@ * External dependencies */ import { has, castArray } from 'lodash'; -import memoize from 'memize'; /** * WordPress dependencies */ import deprecated from '@wordpress/deprecated'; import { dispatch, select, apiFetch } from '@wordpress/data-controls'; -import { - parse, - synchronizeBlocksWithTemplate, - serialize, - isUnmodifiedDefaultBlock, - getFreeformContentHandlerName, -} from '@wordpress/blocks'; -import { removep } from '@wordpress/autop'; +import { parse, synchronizeBlocksWithTemplate } from '@wordpress/blocks'; import isShallowEqual from '@wordpress/is-shallow-equal'; /** @@ -32,6 +24,7 @@ import { getNotificationArgumentsForSaveFail, getNotificationArgumentsForTrashFail, } from './utils/notice-builder'; +import serializeBlocks from './utils/serialize-blocks'; import { awaitNextStateChange, getRegistry } from './controls'; import * as sources from './block-sources'; @@ -761,41 +754,6 @@ export function unlockPostSaving( lockName ) { }; } -/** - * Serializes blocks following backwards compatibility conventions. - * - * @param {Array} blocksForSerialization The blocks to serialize. - * - * @return {string} The blocks serialization. - */ -export const serializeBlocks = memoize( - ( blocksForSerialization ) => { - // A single unmodified default block is assumed to - // be equivalent to an empty post. - if ( - blocksForSerialization.length === 1 && - isUnmodifiedDefaultBlock( blocksForSerialization[ 0 ] ) - ) { - blocksForSerialization = []; - } - - let content = serialize( blocksForSerialization ); - - // For compatibility, treat a post consisting of a - // single freeform block as legacy content and apply - // pre-block-editor removep'd content formatting. - if ( - blocksForSerialization.length === 1 && - blocksForSerialization[ 0 ].name === getFreeformContentHandlerName() - ) { - content = removep( content ); - } - - return content; - }, - { maxSize: 1 } -); - /** * Returns an action object used to signal that the blocks have been updated. * diff --git a/packages/editor/src/store/selectors.js b/packages/editor/src/store/selectors.js index 10c2a0405ed6e8..00859bf4c567b5 100644 --- a/packages/editor/src/store/selectors.js +++ b/packages/editor/src/store/selectors.js @@ -37,7 +37,7 @@ import { AUTOSAVE_PROPERTIES, } from './constants'; import { getPostRawValue } from './reducer'; -import { serializeBlocks } from './actions'; +import serializeBlocks from './utils/serialize-blocks'; /** * Shared reference to an empty object for cases where it is important to avoid diff --git a/packages/editor/src/store/test/selectors.js b/packages/editor/src/store/test/selectors.js index 2185f04c807649..5151345cbe7b8f 100644 --- a/packages/editor/src/store/test/selectors.js +++ b/packages/editor/src/store/test/selectors.js @@ -20,10 +20,10 @@ import { RawHTML } from '@wordpress/element'; /** * Internal dependencies */ -import { serializeBlocks } from '../actions'; import * as _selectors from '../selectors'; import { PREFERENCES_DEFAULTS } from '../defaults'; import { POST_UPDATE_TRANSACTION_ID } from '../constants'; +import serializeBlocks from '../utils/serialize-blocks'; const selectors = { ..._selectors }; const selectorNames = Object.keys( selectors ); diff --git a/packages/editor/src/store/utils/serialize-blocks.js b/packages/editor/src/store/utils/serialize-blocks.js new file mode 100644 index 00000000000000..7301350399ca50 --- /dev/null +++ b/packages/editor/src/store/utils/serialize-blocks.js @@ -0,0 +1,51 @@ +/** + * External dependencies + */ +import memoize from 'memize'; + +/** + * WordPress dependencies + */ +import { + isUnmodifiedDefaultBlock, + serialize, + getFreeformContentHandlerName, +} from '@wordpress/blocks'; +import { removep } from '@wordpress/autop'; + +/** + * Serializes blocks following backwards compatibility conventions. + * + * @param {Array} blocksForSerialization The blocks to serialize. + * + * @return {string} The blocks serialization. + */ +const serializeBlocks = memoize( + ( blocksForSerialization ) => { + // A single unmodified default block is assumed to + // be equivalent to an empty post. + if ( + blocksForSerialization.length === 1 && + isUnmodifiedDefaultBlock( blocksForSerialization[ 0 ] ) + ) { + blocksForSerialization = []; + } + + let content = serialize( blocksForSerialization ); + + // For compatibility, treat a post consisting of a + // single freeform block as legacy content and apply + // pre-block-editor removep'd content formatting. + if ( + blocksForSerialization.length === 1 && + blocksForSerialization[ 0 ].name === getFreeformContentHandlerName() + ) { + content = removep( content ); + } + + return content; + }, + { maxSize: 1 } +); + +export default serializeBlocks;