Skip to content

Commit

Permalink
Editor: Make serializeBlocks a util.
Browse files Browse the repository at this point in the history
  • Loading branch information
epiqueras committed Aug 15, 2019
1 parent f823e03 commit 1f1cdb6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 58 deletions.
12 changes: 0 additions & 12 deletions docs/designers-developers/developers/data/data-core-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -1261,18 +1261,6 @@ _Related_

- selectBlock in core/block-editor store.

<a name="serializeBlocks" href="#serializeBlocks">#</a> **serializeBlocks**

Serializes blocks following backwards compatibility conventions.

_Parameters_

- _blocksForSerialization_ `Array`: The blocks to serialize.

_Returns_

- `string`: The blocks serialization.

<a name="setTemplateValidity" href="#setTemplateValidity">#</a> **setTemplateValidity**

_Related_
Expand Down
46 changes: 2 additions & 44 deletions packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -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';

Expand Down Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
51 changes: 51 additions & 0 deletions packages/editor/src/store/utils/serialize-blocks.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 1f1cdb6

Please sign in to comment.