Skip to content
Merged
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
80 changes: 40 additions & 40 deletions src/blockparty-modal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ import {
addLinkedModalAttribute,
} from './utils';

registerBlockType(metadata.name, {
registerBlockType( metadata.name, {
icon: modal,
edit: Edit,
save,
});
} );

/**
* Returns the list of block names allowed as modal triggers (same as filter blockparty_modal_trigger_allowed_blocks).
Expand All @@ -41,99 +41,99 @@ registerBlockType(metadata.name, {
*/
function getModalTriggerAllowedBlocks() {
try {
const settings = select('core/block-editor').getSettings();
const settings = select( 'core/block-editor' ).getSettings();
const list = settings?.blockpartyModalTriggerAllowedBlocks;
return Array.isArray(list) ? list : ['core/button'];
return Array.isArray( list ) ? list : [ 'core/button' ];
} catch {
return ['core/button'];
return [ 'core/button' ];
}
}

// Add linkedModalId attribute only to blocks allowed as modal triggers.
addFilter(
'blocks.registerBlockType',
'blockparty-modal/add-linked-modal-attribute',
(settings, blockName) => {
( settings, blockName ) => {
const allowedBlocks = getModalTriggerAllowedBlocks();
if (!allowedBlocks.includes(blockName)) {
if ( ! allowedBlocks.includes( blockName ) ) {
return settings;
}
return addLinkedModalAttribute(settings);
return addLinkedModalAttribute( settings );
}
);

// Blocks registered before our script loaded (e.g. core blocks) didn't get the
// filter — re-register only allowed blocks so linkedModalId is persisted on save.
const allowedBlocks = getModalTriggerAllowedBlocks();
const blockTypes = getBlockTypes();
blockTypes.forEach((blockType) => {
blockTypes.forEach( ( blockType ) => {
if (
allowedBlocks.includes(blockType.name) &&
!blockType.attributes?.[LINKED_MODAL_ATTR]
allowedBlocks.includes( blockType.name ) &&
! blockType.attributes?.[ LINKED_MODAL_ATTR ]
) {
registerBlockType(
blockType.name,
addLinkedModalAttribute(blockType)
addLinkedModalAttribute( blockType )
);
}
});
} );

// Add "Attached modal" panel with Combobox only to blocks allowed as modal triggers (see filter blockparty_modal_trigger_allowed_blocks).
addFilter(
'editor.BlockEdit',
'blockparty-modal/with-modal-trigger-control',
(BlockEdit) => (props) => {
( BlockEdit ) => ( props ) => {
const { name, attributes, setAttributes } = props;

if (name === MODAL_BLOCK_NAME) {
return <BlockEdit {...props} />;
if ( name === MODAL_BLOCK_NAME ) {
return <BlockEdit { ...props } />;
}

const triggerAllowedBlocks = useSelect((storeSelect) => {
const settings = storeSelect('core/block-editor').getSettings();
const triggerAllowedBlocks = useSelect( ( storeSelect ) => {
const settings = storeSelect( 'core/block-editor' ).getSettings();
const list = settings?.blockpartyModalTriggerAllowedBlocks;
return Array.isArray(list) ? list : ['core/button'];
}, []);
return Array.isArray( list ) ? list : [ 'core/button' ];
}, [] );

if (!triggerAllowedBlocks.includes(name)) {
return <BlockEdit {...props} />;
if ( ! triggerAllowedBlocks.includes( name ) ) {
return <BlockEdit { ...props } />;
}

const modalOptions = useSelect((storeSelect) => {
return getModalOptionsFromEditor(storeSelect);
}, []);
const modalOptions = useSelect( ( storeSelect ) => {
return getModalOptionsFromEditor( storeSelect );
}, [] );

const options = [
{ value: '', label: __('None', 'blockparty-modal') },
{ value: '', label: __( 'None', 'blockparty-modal' ) },
...modalOptions,
];

const value = attributes[LINKED_MODAL_ATTR] || '';
const value = attributes[ LINKED_MODAL_ATTR ] || '';

return (
<>
<BlockEdit {...props} />
<BlockEdit { ...props } />
<InspectorControls key="blockparty-modal-trigger">
<PanelBody
title={__('Attached modal', 'blockparty-modal')}
initialOpen={false}
title={ __( 'Attached modal', 'blockparty-modal' ) }
initialOpen={ false }
>
<ComboboxControl
label={__(
label={ __(
'Modal to open when block is clicked',
'blockparty-modal'
)}
value={value}
options={options}
onChange={(newValue) =>
setAttributes({
[LINKED_MODAL_ATTR]: newValue || '',
})
) }
value={ value }
options={ options }
onChange={ ( newValue ) =>
setAttributes( {
[ LINKED_MODAL_ATTR ]: newValue || '',
} )
}
placeholder={__(
placeholder={ __(
'Select a modal…',
'blockparty-modal'
)}
) }
/>
</PanelBody>
</InspectorControls>
Expand Down
Loading