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

Footnotes: combine format store subscription #58129

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 32 additions & 33 deletions packages/block-library/src/footnotes/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
privateApis,
} from '@wordpress/block-editor';
import { useSelect, useDispatch, useRegistry } from '@wordpress/data';
import { useEntityProp } from '@wordpress/core-data';
import { store as coreDataStore } from '@wordpress/core-data';
import { createBlock, store as blocksStore } from '@wordpress/blocks';

/**
Expand Down Expand Up @@ -54,43 +54,42 @@ export const format = {
getBlockName,
getBlockParentsByBlockName,
} = registry.select( blockEditorStore );
const hasFootnotesBlockType = useSelect(
( select ) =>
!! select( blocksStore ).getBlockType( 'core/footnotes' ),
[]
const isFootnotesSupported = useSelect(
( select ) => {
if (
! select( blocksStore ).getBlockType( 'core/footnotes' )
) {
return false;
}

const entityRecord = select( coreDataStore ).getEntityRecord(
'postType',
postType,
postId
);

if ( 'string' !== typeof entityRecord?.meta?.footnotes ) {
return false;
}

// Checks if the selected block lives within a pattern.
const {
getBlockParentsByBlockName: _getBlockParentsByBlockName,
getSelectedBlockClientId: _getSelectedBlockClientId,
} = select( blockEditorStore );
const parentCoreBlocks = _getBlockParentsByBlockName(
_getSelectedBlockClientId(),
SYNCED_PATTERN_BLOCK_NAME
);
return ! parentCoreBlocks || parentCoreBlocks.length === 0;
},
[ postType, postId ]
);
/*
* This useSelect exists because we need to use its return value
* outside the event callback.
*/
const isBlockWithinPattern = useSelect( ( select ) => {
const {
getBlockParentsByBlockName: _getBlockParentsByBlockName,
getSelectedBlockClientId: _getSelectedBlockClientId,
} = select( blockEditorStore );
const parentCoreBlocks = _getBlockParentsByBlockName(
_getSelectedBlockClientId(),
SYNCED_PATTERN_BLOCK_NAME
);
return parentCoreBlocks && parentCoreBlocks.length > 0;
}, [] );

const [ meta ] = useEntityProp( 'postType', postType, 'meta', postId );
const footnotesSupported = 'string' === typeof meta?.footnotes;

const { selectionChange, insertBlock } =
useDispatch( blockEditorStore );

if ( ! hasFootnotesBlockType ) {
return null;
}

if ( ! footnotesSupported ) {
return null;
}

// Checks if the selected block lives within a pattern.
if ( isBlockWithinPattern ) {
if ( ! isFootnotesSupported ) {
return null;
}

Expand Down