Skip to content

Commit

Permalink
Move to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
cbravobernal committed Jul 8, 2024
1 parent f1f4b43 commit 3e258a0
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 59 deletions.
56 changes: 56 additions & 0 deletions packages/editor/src/bindings/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
type MetaData = {
bindings?: Record< string, string >;
};
type UpdateBlockAttributes = (
id: string,
attributes: Record< string, any >
) => void;

export const addConnection = (
value: string,
attribute: string,
metadata: MetaData,
_id: string,
updateBlockAttributes: UpdateBlockAttributes
) => {
// Assuming the block expects a flat structure for its metadata attribute
const newMetadata = {
...metadata,
// Adjust this according to the actual structure expected by your block
bindings: {
...metadata?.bindings,
[ attribute ]: {
source: 'core/post-meta',
args: { key: value },
},
},
};

// Update the block's attributes with the new metadata
updateBlockAttributes( _id, {
metadata: newMetadata,
} );
};

export const removeConnection = (
key: string,
metadata: MetaData,
_id: string,
updateBlockAttributes: (
id: string,
attributes: Record< string, any >
) => void
) => {
const newMetadata = { ...metadata };
if ( ! newMetadata.bindings ) {
return;
}
delete newMetadata.bindings[ key ];
if ( Object.keys( newMetadata.bindings ).length === 0 ) {
delete newMetadata.bindings;
}
updateBlockAttributes( _id, {
metadata:
Object.keys( newMetadata ).length === 0 ? undefined : newMetadata,
} );
};
91 changes: 32 additions & 59 deletions packages/editor/src/hooks/block-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from './use-bindings-attributes';
import { unlock } from '../lock-unlock';
import { store as editorStore } from '../store';
import { removeConnection, addConnection } from '../bindings/utils';

/**
* WordPress dependencies
Expand Down Expand Up @@ -36,7 +37,7 @@ const {
DropdownMenuItemHelpTextV2: DropDownMenuItemHelpText,
} = unlock( componentsPrivateApis );

const BlockBindingsPanel = ( { name, metadata } ) => {
const BlockBindingsPanel = ( { name, attributes: { metadata } } ) => {
const { bindings } = metadata || {};
const { sources } = useSelect( ( select ) => {
const _sources = unlock(
Expand Down Expand Up @@ -77,47 +78,13 @@ const BlockBindingsPanel = ( { name, metadata } ) => {
};
}, [] );

const onCloseNewConnection = ( value, attribute ) => {
// Assuming the block expects a flat structure for its metadata attribute
const newMetadata = {
...metadata,
// Adjust this according to the actual structure expected by your block
bindings: {
...bindings,
[ attribute ]: {
source: 'core/post-meta',
args: { key: value },
},
},
};

// Update the block's attributes with the new metadata
updateBlockAttributes( _id, {
metadata: newMetadata,
} );
};

const removeConnection = ( key ) => {
const newMetadata = { ...metadata };
delete newMetadata.bindings[ key ];
if ( Object.keys( newMetadata.bindings ).length === 0 ) {
delete newMetadata.bindings;
}
updateBlockAttributes( _id, {
metadata:
Object.keys( newMetadata ).length === 0
? undefined
: newMetadata,
} );
};

const allAttributesBinded =
Object.keys( filteredBindings ).length === bindableAttributes?.length;

return (
<InspectorControls>
<PanelBody
title={ __( 'Attributes' ) }
title={ __( 'Connections' ) }
className="components-panel__block-bindings-panel"
>
<BaseControl
Expand Down Expand Up @@ -161,9 +128,12 @@ const BlockBindingsPanel = ( { name, metadata } ) => {
<DropdownMenuItem
key={ key }
onClick={ () => {
onCloseNewConnection(
addConnection(
key,
attribute
attribute,
metadata,
_id,
updateBlockAttributes
);
} }
>
Expand All @@ -179,27 +149,30 @@ const BlockBindingsPanel = ( { name, metadata } ) => {
) ) }
</DropdownMenu>
) }
<MenuGroup>
{ Object.keys( filteredBindings ).map( ( key ) => {
const source = sources[
filteredBindings[ key ].source
]
? sources[ filteredBindings[ key ].source ]
.label
: filteredBindings[ key ].source;
return (
<MenuItem
key={ key }
onClick={ () =>
removeConnection( key )
}
icon={ reset }
>
{ key } - { source }
</MenuItem>
);
} ) }
</MenuGroup>
{ Object.keys( filteredBindings ).map( ( key ) => {
const source = sources[
filteredBindings[ key ].source
]
? sources[ filteredBindings[ key ].source ]
.label
: filteredBindings[ key ].source;
return (
<MenuItem
key={ key }
onClick={ () =>
removeConnection(
key,
metadata,
_id,
updateBlockAttributes
)
}
icon={ reset }
>
{ key } - { source }
</MenuItem>
);
} ) }
</MenuGroup>
</BaseControl>
</PanelBody>
Expand Down

0 comments on commit 3e258a0

Please sign in to comment.