-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1f4b43
commit 3e258a0
Showing
2 changed files
with
88 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} ); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters