Skip to content

Commit

Permalink
Fix focus loss
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed Nov 20, 2023
1 parent fb43ec2 commit 6befb37
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 37 deletions.
25 changes: 7 additions & 18 deletions packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import classnames from 'classnames';
import fastDeepEqual from 'fast-deep-equal';

/**
* WordPress dependencies
Expand Down Expand Up @@ -226,8 +225,7 @@ export default function ReusableBlockEdit( {
// but won't create an undo level.
// This can be abstracted into a `useSyncDerivedAttributes` hook if needed.
useEffect( () => {
const { getBlocks, getBlockAttributes } =
registry.select( blockEditorStore );
const { getBlocks } = registry.select( blockEditorStore );
const { syncDerivedBlockAttributes } = unlock(
registry.dispatch( blockEditorStore )
);
Expand All @@ -236,21 +234,12 @@ export default function ReusableBlockEdit( {
const blocks = getBlocks( patternClientId );
if ( blocks !== prevBlocks ) {
prevBlocks = blocks;
// TODO: We should probably cache this somehow to improve performance.
const nextDynamicContent = getDynamicContentFromBlocks(
blocks,
defaultValuesRef.current
);
if (
! fastDeepEqual(
getBlockAttributes( patternClientId ).dynamicContent,
nextDynamicContent
)
) {
syncDerivedBlockAttributes( patternClientId, {
dynamicContent: nextDynamicContent,
} );
}
syncDerivedBlockAttributes( patternClientId, {
dynamicContent: getDynamicContentFromBlocks(
blocks,
defaultValuesRef.current
),
} );
}
}, blockEditorStore );
}, [ patternClientId, registry ] );
Expand Down
38 changes: 19 additions & 19 deletions packages/block-library/src/block/v1/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,33 +114,29 @@ export default function ReusableBlockEdit( {
: InnerBlocks.ButtonBlockAppender,
} );

let children = null;

if ( hasAlreadyRendered ) {
return (
<div { ...blockProps }>
<Warning>
{ __( 'Block cannot be rendered inside itself.' ) }
</Warning>
</div>
children = (
<Warning>
{ __( 'Block cannot be rendered inside itself.' ) }
</Warning>
);
}

if ( isMissing ) {
return (
<div { ...blockProps }>
<Warning>
{ __( 'Block has been deleted or is unavailable.' ) }
</Warning>
</div>
children = (
<Warning>
{ __( 'Block has been deleted or is unavailable.' ) }
</Warning>
);
}

if ( ! hasResolved ) {
return (
<div { ...blockProps }>
<Placeholder>
<Spinner />
</Placeholder>
</div>
children = (
<Placeholder>
<Spinner />
</Placeholder>
);
}

Expand All @@ -157,7 +153,11 @@ export default function ReusableBlockEdit( {
/>
</PanelBody>
</InspectorControls>
<div { ...innerBlocksProps } />
{ children === null ? (
<div { ...innerBlocksProps } />
) : (
<div { ...blockProps }>{ children }</div>
) }
</RecursionProvider>
);
}

0 comments on commit 6befb37

Please sign in to comment.