Skip to content

Commit

Permalink
Block Editor: Replace dependency on Core Data with an Editor filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
epiqueras committed Sep 16, 2019
1 parent 9b19acb commit c484874
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 62 deletions.
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/block-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"@wordpress/blocks": "file:../blocks",
"@wordpress/components": "file:../components",
"@wordpress/compose": "file:../compose",
"@wordpress/core-data": "file:../core-data",
"@wordpress/data": "file:../data",
"@wordpress/deprecated": "file:../deprecated",
"@wordpress/dom": "file:../dom",
Expand Down
61 changes: 1 addition & 60 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,7 @@ import { animated } from 'react-spring/web.cjs';
/**
* WordPress dependencies
*/
import { useEntityProp } from '@wordpress/core-data';
import {
useMemo,
useCallback,
useRef,
useEffect,
useLayoutEffect,
useState,
} from '@wordpress/element';
import { useRef, useEffect, useLayoutEffect, useState } from '@wordpress/element';
import {
focus,
isTextField,
Expand Down Expand Up @@ -60,51 +52,6 @@ import Inserter from '../inserter';
import { isInsideRootBlock } from '../../utils/dom';
import useMovingAnimation from './moving-animation';

const EMPTY_OBJECT = {};
function useMetaAttributeSourceBackwardsCompatibility(
name,
_attributes,
_setAttributes
) {
const { attributes: attributeTypes = EMPTY_OBJECT } =
getBlockType( name ) || EMPTY_OBJECT;
let [ attributes, setAttributes ] = [ _attributes, _setAttributes ];

if ( Object.values( attributeTypes ).some( ( type ) => type.source === 'meta' ) ) {
// eslint-disable-next-line react-hooks/rules-of-hooks
const [ meta, setMeta ] = useEntityProp( 'postType', 'post', 'meta' );

// eslint-disable-next-line react-hooks/rules-of-hooks
attributes = useMemo(
() => ( {
..._attributes,
...Object.keys( attributeTypes ).reduce( ( acc, key ) => {
if ( attributeTypes[ key ].source === 'meta' ) {
acc[ key ] = meta[ attributeTypes[ key ].meta ];
}
return acc;
}, {} ),
} ),
[ attributeTypes, meta, _attributes ]
);

// eslint-disable-next-line react-hooks/rules-of-hooks
setAttributes = useCallback(
( ...args ) => {
Object.keys( args[ 0 ] ).forEach( ( key ) => {
if ( attributeTypes[ key ].source === 'meta' ) {
setMeta( { [ attributeTypes[ key ].meta ]: args[ 0 ][ key ] } );
}
} );
return _setAttributes( ...args );
},
[ attributeTypes, setMeta, _setAttributes ]
);
}

return [ attributes, setAttributes ];
}

/**
* Prevents default dragging behavior within a block to allow for multi-
* selection to take effect unhampered.
Expand Down Expand Up @@ -155,12 +102,6 @@ function BlockListBlock( {
isNavigationMode,
enableNavigationMode,
} ) {
[ attributes, setAttributes ] = useMetaAttributeSourceBackwardsCompatibility(
name,
attributes,
setAttributes
);

// Random state used to rerender the component if needed, ideally we don't need this
const [ , updateRerenderState ] = useState( {} );
const rerender = () => updateRerenderState( {} );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* WordPress dependencies
*/
import { getBlockType } from '@wordpress/blocks';
import { useEntityProp } from '@wordpress/core-data';
import { useMemo, useCallback } from '@wordpress/element';
import { createHigherOrderComponent } from '@wordpress/compose';
import { addFilter } from '@wordpress/hooks';

const EMPTY_OBJECT = {};
function useMetaAttributeSource( name, _attributes, _setAttributes ) {
const { attributes: attributeTypes = EMPTY_OBJECT } =
getBlockType( name ) || EMPTY_OBJECT;
let [ attributes, setAttributes ] = [ _attributes, _setAttributes ];

if ( Object.values( attributeTypes ).some( ( type ) => type.source === 'meta' ) ) {
// eslint-disable-next-line react-hooks/rules-of-hooks
const [ meta, setMeta ] = useEntityProp( 'postType', 'post', 'meta' );

// eslint-disable-next-line react-hooks/rules-of-hooks
attributes = useMemo(
() => ( {
..._attributes,
...Object.keys( attributeTypes ).reduce( ( acc, key ) => {
if ( attributeTypes[ key ].source === 'meta' ) {
acc[ key ] = meta[ attributeTypes[ key ].meta ];
}
return acc;
}, {} ),
} ),
[ attributeTypes, meta, _attributes ]
);

// eslint-disable-next-line react-hooks/rules-of-hooks
setAttributes = useCallback(
( ...args ) => {
Object.keys( args[ 0 ] ).forEach( ( key ) => {
if ( attributeTypes[ key ].source === 'meta' ) {
setMeta( { [ attributeTypes[ key ].meta ]: args[ 0 ][ key ] } );
}
} );
return _setAttributes( ...args );
},
[ attributeTypes, setMeta, _setAttributes ]
);
}

return [ attributes, setAttributes ];
}
const withMetaAttributeSource = createHigherOrderComponent(
( BlockListBlock ) => ( { attributes, setAttributes, name, ...props } ) => {
[ attributes, setAttributes ] = useMetaAttributeSource(
name,
attributes,
setAttributes
);
return (
<BlockListBlock
attributes={ attributes }
setAttributes={ setAttributes }
name={ name }
{ ...props }
/>
);
},
'withMetaAttributeSource'
);

addFilter(
'editor.BlockListBlock',
'core/editor/custom-sources-backwards-compatibility/with-meta-attribute-source',
withMetaAttributeSource
);
1 change: 1 addition & 0 deletions packages/editor/src/hooks/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/**
* Internal dependencies
*/
import './custom-sources-backwards-compatibility';
import './default-autocompleters';

0 comments on commit c484874

Please sign in to comment.