Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Editor: Reimplement BlockEditorProvider using hooks #16357

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 64 additions & 115 deletions packages/block-editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
@@ -1,139 +1,88 @@
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { useEffect, useRef } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
Comment on lines +4 to +5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: I think imports should be in alphabetical order.

Suggested change
import { useEffect, useRef } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { useDispatch, useSelect } from '@wordpress/data';
import { useEffect, useRef } from '@wordpress/element';


/**
* Internal dependencies
*/
import withRegistryProvider from './with-registry-provider';

class BlockEditorProvider extends Component {
componentDidMount() {
this.props.updateSettings( this.props.settings );
this.props.resetBlocks( this.props.value );
this.attachChangeObserver( this.props.registry );
}
function BlockEditorProvider( {
settings,
value,
children,
onChange = () => {},
onInput = () => {},
Comment on lines +16 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Microoptimization: Instead of creating 2 separate functions these could both reference a dummy empty function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically, the noop function provided by Lodash could be used here.

} ) {
const { updateSettings, resetBlocks } = useDispatch( 'core/block-editor' );

componentDidUpdate( prevProps ) {
const {
settings,
updateSettings,
value,
resetBlocks,
registry,
} = this.props;

if ( settings !== prevProps.settings ) {
updateSettings( settings );
}

if ( registry !== prevProps.registry ) {
this.attachChangeObserver( registry );
}

if ( this.isSyncingOutcomingValue ) {
this.isSyncingOutcomingValue = false;
} else if ( value !== prevProps.value ) {
this.isSyncingIncomingValue = true;
resetBlocks( value );
}
}

componentWillUnmount() {
if ( this.unsubscribe ) {
this.unsubscribe();
}
}

/**
* Given a registry object, overrides the default dispatch behavior for the
* `core/block-editor` store to interpret a state change and decide whether
* we should call `onChange` or `onInput` depending on whether the change
* is persistent or not.
*
* This needs to be done synchronously after state changes (instead of using
* `componentDidUpdate`) in order to avoid batching these changes.
*
* @param {WPDataRegistry} registry Registry from which block editor
* dispatch is to be overriden.
*/
attachChangeObserver( registry ) {
if ( this.unsubscribe ) {
this.unsubscribe();
}
useEffect( () => {
updateSettings( settings );
}, [ settings ] );

const { blocks, isPersistent, isIgnored } = useSelect( ( select ) => {
const {
getBlocks,
isLastBlockChangePersistent,
__unstableIsLastBlockChangeIgnored,
} = registry.select( 'core/block-editor' );
} = select( 'core/block-editor' );

let blocks = getBlocks();
let isPersistent = isLastBlockChangePersistent();

this.unsubscribe = registry.subscribe( () => {
const {
onChange,
onInput,
} = this.props;
const newBlocks = getBlocks();
const newIsPersistent = isLastBlockChangePersistent();
if (
newBlocks !== blocks && (
this.isSyncingIncomingValue ||
__unstableIsLastBlockChangeIgnored()
)
) {
this.isSyncingIncomingValue = false;
blocks = newBlocks;
isPersistent = newIsPersistent;
return;
return {
blocks: getBlocks(),
isPersistent: isLastBlockChangePersistent(),
isIgnored: __unstableIsLastBlockChangeIgnored(),
};
} );
Comment on lines +25 to +37
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A big optimization that could be made here is adding an empty dependency array to this useSelect.


const previousBlocks = useRef( blocks );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a use-case for a usePrevious hook. I'm actually using one already in #21181, and I wonder if we should add it to @wordpress/compose.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usePrevious has now been added to the @wordpress/compose package, so it could definitely be used here.

const previousIsPersistent = useRef( isPersistent );
const isSyncingIncomingValue = useRef( false );
const isSyncingOutgoingValue = useRef( false );

useEffect( () => {
if (
blocks !== previousBlocks.current && (
isSyncingIncomingValue.current ||
isIgnored
)
) {
isSyncingIncomingValue.current = false;
previousBlocks.current = blocks;
previousIsPersistent.current = isPersistent;
} else if (
blocks !== previousBlocks.current ||
// This happens when a previous input is explicitely marked as persistent.
( isPersistent && ! previousIsPersistent.current )
) {
// When knowing the blocks value is changing, assign instance
// value to skip reset in subsequent `componentDidUpdate`.
if ( blocks !== previousBlocks.current ) {
isSyncingOutgoingValue.current = true;
}

if (
newBlocks !== blocks ||
// This happens when a previous input is explicitely marked as persistent.
( newIsPersistent && ! isPersistent )
) {
// When knowing the blocks value is changing, assign instance
// value to skip reset in subsequent `componentDidUpdate`.
if ( newBlocks !== blocks ) {
this.isSyncingOutcomingValue = true;
}
previousBlocks.current = blocks;
previousIsPersistent.current = isPersistent;

blocks = newBlocks;
isPersistent = newIsPersistent;

if ( isPersistent ) {
onChange( blocks );
} else {
onInput( blocks );
}
if ( isPersistent ) {
onChange( blocks );
} else {
onInput( blocks );
}
} );
}
}
}, [ blocks, isPersistent ] );

render() {
const { children } = this.props;
useEffect( () => {
if ( isSyncingOutgoingValue.current ) {
isSyncingOutgoingValue.current = false;
} else {
isSyncingIncomingValue.current = true;
resetBlocks( value );
}
}, [ value ] );

return children;
}
return children;
}

export default compose( [
withRegistryProvider,
withDispatch( ( dispatch ) => {
const {
updateSettings,
resetBlocks,
} = dispatch( 'core/block-editor' );

return {
updateSettings,
resetBlocks,
};
} ),
] )( BlockEditorProvider );
export default withRegistryProvider( BlockEditorProvider );
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import applyMiddlewares from '../../store/middlewares';
const withRegistryProvider = createHigherOrderComponent( ( WrappedComponent ) => {
return withRegistry( ( { useSubRegistry = true, registry, ...props } ) => {
if ( ! useSubRegistry ) {
return <WrappedComponent registry={ registry } { ...props } />;
return <WrappedComponent { ...props } />;
}

const [ subRegistry, setSubRegistry ] = useState( null );
Expand All @@ -32,7 +32,7 @@ const withRegistryProvider = createHigherOrderComponent( ( WrappedComponent ) =>

return (
<RegistryProvider value={ subRegistry }>
<WrappedComponent registry={ subRegistry } { ...props } />
<WrappedComponent { ...props } />
</RegistryProvider>
);
} );
Expand Down