diff --git a/src/blocks/remote-data-container/components/block-binding-controls.tsx b/src/blocks/remote-data-container/components/block-binding-controls.tsx index 32ec0a2e..d39490ce 100644 --- a/src/blocks/remote-data-container/components/block-binding-controls.tsx +++ b/src/blocks/remote-data-container/components/block-binding-controls.tsx @@ -31,7 +31,7 @@ export function BlockBindingFieldControl( props: BlockBindingFieldControlProps ) } interface BlockBindingControlsProps { - attributes: ContextInnerBlockAttributes; + attributes: RemoteDataInnerBlockAttributes; availableBindings: AvailableBindings; blockName: string; removeBinding: ( target: string ) => void; diff --git a/src/blocks/remote-data-container/components/inner-blocks.tsx b/src/blocks/remote-data-container/components/inner-blocks.tsx index 2cb32819..53c2befc 100644 --- a/src/blocks/remote-data-container/components/inner-blocks.tsx +++ b/src/blocks/remote-data-container/components/inner-blocks.tsx @@ -7,7 +7,7 @@ interface InnerBlocksProps { blockConfig: BlockConfig; getInnerBlocks: ( result: Record< string, string > - ) => BlockInstance< ContextInnerBlockAttributes >[]; + ) => BlockInstance< RemoteDataInnerBlockAttributes >[]; remoteData: RemoteData; } diff --git a/src/blocks/remote-data-container/components/loop-template.tsx b/src/blocks/remote-data-container/components/loop-template.tsx index ea0a4886..c9f8d214 100644 --- a/src/blocks/remote-data-container/components/loop-template.tsx +++ b/src/blocks/remote-data-container/components/loop-template.tsx @@ -15,7 +15,7 @@ import { LoopIndexContext } from '@/blocks/remote-data-container/context/loop-in interface LoopTemplateProps { getInnerBlocks: ( result: Record< string, string > - ) => BlockInstance< ContextInnerBlockAttributes >[]; + ) => BlockInstance< RemoteDataInnerBlockAttributes >[]; remoteData: RemoteData; } diff --git a/src/blocks/remote-data-container/edit.tsx b/src/blocks/remote-data-container/edit.tsx index 1793d8fd..b30311d2 100644 --- a/src/blocks/remote-data-container/edit.tsx +++ b/src/blocks/remote-data-container/edit.tsx @@ -15,7 +15,7 @@ import { hasRemoteDataChanged } from '@/utils/block-binding'; import { getBlockConfig } from '@/utils/localized-block-data'; import './editor.scss'; -export function Edit( props: BlockEditProps< ContextBlockAttributes > ) { +export function Edit( props: BlockEditProps< RemoteDataBlockAttributes > ) { const blockConfig = getBlockConfig( props.name ); if ( ! blockConfig ) { diff --git a/src/blocks/remote-data-container/hooks/use-existing-remote-data.ts b/src/blocks/remote-data-container/hooks/use-existing-remote-data.ts index b4e18f42..09ba00bd 100644 --- a/src/blocks/remote-data-container/hooks/use-existing-remote-data.ts +++ b/src/blocks/remote-data-container/hooks/use-existing-remote-data.ts @@ -17,7 +17,7 @@ export function useExistingRemoteData(): RemoteData[] { return blocks .map( clientId => { - const block = getBlocksByClientId< ContextBlockAttributes >( clientId )[ 0 ]; + const block = getBlocksByClientId< RemoteDataBlockAttributes >( clientId )[ 0 ]; return block?.attributes?.remoteData; } ) .filter( ( maybeRemoteData ): maybeRemoteData is RemoteData => Boolean( maybeRemoteData ) ); diff --git a/src/blocks/remote-data-container/hooks/use-patterns.ts b/src/blocks/remote-data-container/hooks/use-patterns.ts index feb7d775..7a129438 100644 --- a/src/blocks/remote-data-container/hooks/use-patterns.ts +++ b/src/blocks/remote-data-container/hooks/use-patterns.ts @@ -33,8 +33,8 @@ export function usePatterns( blockName: string, rootClientId: string ) { return { getInnerBlocks: ( result: Record< string, string > - ): BlockInstance< ContextInnerBlockAttributes >[] => { - return getBlocks< ContextInnerBlockAttributes >( rootClientId ).map( block => + ): BlockInstance< RemoteDataInnerBlockAttributes >[] => { + return getBlocks< RemoteDataInnerBlockAttributes >( rootClientId ).map( block => cloneBlockWithAttributes( block, result ) ); }, diff --git a/src/blocks/remote-data-container/hooks/with-block-binding.tsx b/src/blocks/remote-data-container/hooks/with-block-binding.tsx index d78aafee..17eca447 100644 --- a/src/blocks/remote-data-container/hooks/with-block-binding.tsx +++ b/src/blocks/remote-data-container/hooks/with-block-binding.tsx @@ -17,12 +17,12 @@ import { getMismatchedAttributes } from '@/utils/block-binding'; import { getBlockAvailableBindings } from '@/utils/localized-block-data'; interface BoundBlockEditProps { - attributes: ContextInnerBlockAttributes; + attributes: RemoteDataInnerBlockAttributes; availableBindings: AvailableBindings; blockName: string; children: JSX.Element; remoteDataName: string; - setAttributes: ( attributes: ContextInnerBlockAttributes ) => void; + setAttributes: ( attributes: RemoteDataInnerBlockAttributes ) => void; } function BoundBlockEdit( props: BoundBlockEditProps ) { @@ -78,7 +78,7 @@ function BoundBlockEdit( props: BoundBlockEditProps ) { } export const withBlockBinding = createHigherOrderComponent( BlockEdit => { - return ( props: BlockEditProps< ContextInnerBlockAttributes > ) => { + return ( props: BlockEditProps< RemoteDataInnerBlockAttributes > ) => { const { attributes, context, name, setAttributes } = props; const remoteData = context[ REMOTE_DATA_CONTEXT_KEY ] as RemoteData | undefined; const availableBindings = getBlockAvailableBindings( remoteData?.blockName ?? '' ); @@ -118,7 +118,7 @@ export const withBlockBinding = createHigherOrderComponent( BlockEdit => { // If the block has a binding and the attributes do not match their expected // values, update and merge the attributes. - const mergedAttributes = useMemo< ContextInnerBlockAttributes >( () => { + const mergedAttributes = useMemo< RemoteDataInnerBlockAttributes >( () => { return { ...attributes, ...getMismatchedAttributes( attributes, remoteData.results, index ), diff --git a/src/blocks/remote-data-container/index.ts b/src/blocks/remote-data-container/index.ts index f8e79135..c623834b 100644 --- a/src/blocks/remote-data-container/index.ts +++ b/src/blocks/remote-data-container/index.ts @@ -11,7 +11,7 @@ import './style.scss'; // Register a unique block definition for each of the context blocks. Object.values( getBlocksConfig() ).forEach( blockConfig => { - registerBlockType< ContextBlockAttributes >( blockConfig.name, { + registerBlockType< RemoteDataBlockAttributes >( blockConfig.name, { ...blockConfig.settings, attributes: { remoteData: { diff --git a/src/utils/block-binding.ts b/src/utils/block-binding.ts index c3b7f01e..0fcce909 100644 --- a/src/utils/block-binding.ts +++ b/src/utils/block-binding.ts @@ -31,7 +31,7 @@ function getExpectedAttributeValue( } export function getBoundAttributeEntries( - attributes: ContextInnerBlockAttributes + attributes: RemoteDataInnerBlockAttributes ): [ string, RemoteDataBlockBinding ][] { return Object.entries( attributes.metadata?.bindings ?? {} ).filter( ( [ _target, binding ] ) => binding.source === BLOCK_BINDING_SOURCE @@ -39,10 +39,10 @@ export function getBoundAttributeEntries( } export function getMismatchedAttributes( - attributes: ContextInnerBlockAttributes, + attributes: RemoteDataInnerBlockAttributes, results: RemoteData[ 'results' ], index = 0 -): Partial< ContextInnerBlockAttributes > { +): Partial< RemoteDataInnerBlockAttributes > { return Object.fromEntries( getBoundAttributeEntries( attributes ) .map( ( [ target, binding ] ) => [ @@ -52,7 +52,7 @@ export function getMismatchedAttributes( .filter( ( [ target, value ] ) => null !== value && value !== getAttributeValue( attributes, target ) ) - ) as Partial< ContextInnerBlockAttributes >; + ) as Partial< RemoteDataInnerBlockAttributes >; } export function hasRemoteDataChanged( one: RemoteData, two: RemoteData ): boolean { diff --git a/tests/src/utils/block-binding.test.ts b/tests/src/utils/block-binding.test.ts index 39eb33f9..3232a09f 100644 --- a/tests/src/utils/block-binding.test.ts +++ b/tests/src/utils/block-binding.test.ts @@ -7,7 +7,7 @@ describe( 'block-binding utils', () => { describe( 'getBoundAttributeEntries', () => { it( 'should return bound attribute entries', () => { const name = 'test/block'; - const attributes: ContextInnerBlockAttributes = { + const attributes: RemoteDataInnerBlockAttributes = { metadata: { bindings: { content: { source: BLOCK_BINDING_SOURCE, args: { name, field: 'title' } }, @@ -26,7 +26,7 @@ describe( 'block-binding utils', () => { } ); it( 'should return an empty array when no bindings are present', () => { - const attributes: ContextInnerBlockAttributes = {}; + const attributes: RemoteDataInnerBlockAttributes = {}; const result = getBoundAttributeEntries( attributes ); @@ -37,7 +37,7 @@ describe( 'block-binding utils', () => { describe( 'getMismatchedAttributes', () => { it( 'should return mismatched attributes', () => { const name = 'test/block'; - const attributes: ContextInnerBlockAttributes = { + const attributes: RemoteDataInnerBlockAttributes = { content: 'Old content', url: 'https://old-url.com', alt: 'Old alt', @@ -61,7 +61,7 @@ describe( 'block-binding utils', () => { it( 'should return an empty object when no mismatches are found', () => { const name = 'test/block'; - const attributes: ContextInnerBlockAttributes = { + const attributes: RemoteDataInnerBlockAttributes = { content: 'Title: Current content', url: 'https://current-url.com', metadata: { @@ -84,7 +84,7 @@ describe( 'block-binding utils', () => { it( 'should handle missing results', () => { const name = 'test/block'; - const attributes: ContextInnerBlockAttributes = { + const attributes: RemoteDataInnerBlockAttributes = { content: 'Old content', url: 'https://old-url.com', metadata: { @@ -106,7 +106,7 @@ describe( 'block-binding utils', () => { it( 'should handle missing label', () => { const name = 'test/block'; - const attributes: ContextInnerBlockAttributes = { + const attributes: RemoteDataInnerBlockAttributes = { content: 'My Title', metadata: { bindings: { diff --git a/types/remote-data.d.ts b/types/remote-data.d.ts index 729158ee..584c5ab8 100644 --- a/types/remote-data.d.ts +++ b/types/remote-data.d.ts @@ -23,11 +23,11 @@ interface RemoteData { results: Record< string, string >[]; } -interface ContextBlockAttributes { +interface RemoteDataBlockAttributes { remoteData: RemoteData; } -interface FieldSelection extends ContextBlockAttributes { +interface FieldSelection extends RemoteDataBlockAttributes { selectedField: string; type: 'field' | 'meta'; } @@ -47,7 +47,7 @@ interface RemoteDataBlockBinding { args: RemoteDataBlockBindingArgs; } -interface ContextInnerBlockAttributes { +interface RemoteDataInnerBlockAttributes { alt?: string | RichTextData; content?: string | RichTextData; index?: number; diff --git a/types/wordpress__block-editor/index.d.ts b/types/wordpress__block-editor/index.d.ts index 55866e34..994e672a 100644 --- a/types/wordpress__block-editor/index.d.ts +++ b/types/wordpress__block-editor/index.d.ts @@ -27,7 +27,7 @@ declare module '@wordpress/block-editor' { // Incomplete type for our use case. interface BlockPattern { - blocks: BlockInstance< ContextInnerBlockAttributes >[]; + blocks: BlockInstance< RemoteDataInnerBlockAttributes >[]; name: string; title: string; }