Skip to content

Commit

Permalink
polish post-entity source handler
Browse files Browse the repository at this point in the history
  • Loading branch information
retrofox committed Feb 12, 2024
1 parent 95c4064 commit 688c4db
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions packages/editor/src/bindings/post-entity/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
* External dependencies
*/
/**
* WordPress dependencies
*/
Expand All @@ -19,8 +16,15 @@ import { store as editorStore } from '../../store';
* @return {Object} The source value and setter.
*/
const useSource = ( blockProps, sourceArgs ) => {
const { context } = blockProps;
if ( typeof sourceArgs === 'undefined' ) {
throw new Error( 'The "args" argument is required.' );
}

if ( ! sourceArgs?.prop ) {
throw new Error( 'The "prop" argument is required.' );
}

const { context } = blockProps;
const { postType: contextPostType } = context;

const { prop: entityPropName, entity: entityType = 'postType' } =
Expand Down Expand Up @@ -59,27 +63,29 @@ const useSource = ( blockProps, sourceArgs ) => {
* Create the product-entity
* block binding source handler.
*
* source ID:
* `woo/product-entity`
* source ID: `core/post-entity`
* args:
* - prop: The name of the entity property to bind.
* - prop: The name of the post entity property to bind.
*
* example:
* The following metadata will bind the post title
* to the `content` attribute of the block.
*
* ```
* metadata: {
* bindings: {
* content: {
* source: 'woo/product-entity',
* source: 'core/post-entity',
* args: {
* prop: 'short_description',
* prop: 'title',
* },
* },
* },
* ```
*/
export default {
name: 'core/post-entity',
label: __( 'Core Entity' ),
label: __( 'Core Post Entity block binding source' ),
useSource,
lockAttributesEditing: false,
};

0 comments on commit 688c4db

Please sign in to comment.