-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding test name block with support for entity data store
- Loading branch information
1 parent
970c1ef
commit a3dfaaf
Showing
11 changed files
with
219 additions
and
53 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
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
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
5 changes: 4 additions & 1 deletion
5
packages/js/product-editor/src/components/editor/init-blocks.ts
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { init as initName } from '../name'; | ||
|
||
export const initBlocks = () => {}; | ||
export const initBlocks = () => { | ||
initName(); | ||
}; |
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,23 @@ | ||
export default { | ||
$schema: 'https://schemas.wp.org/trunk/block.json', | ||
apiVersion: 2, | ||
name: 'woocommerce/product-name', | ||
title: 'Product name', | ||
category: 'widgets', | ||
description: 'The product name.', | ||
keywords: [ 'products', 'name', 'title' ], | ||
textdomain: 'default', | ||
attributes: { | ||
name: { | ||
type: 'string', | ||
}, | ||
}, | ||
supports: { | ||
align: false, | ||
html: false, | ||
multiple: false, | ||
reusable: false, | ||
inserter: false, | ||
lock: false, | ||
}, | ||
}; |
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,38 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { createElement } from '@wordpress/element'; | ||
import interpolateComponents from '@automattic/interpolate-components'; | ||
import { TextControl } from '@woocommerce/components'; | ||
import { useBlockProps } from '@wordpress/block-editor'; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore No types for this exist yet. | ||
// eslint-disable-next-line @woocommerce/dependency-group | ||
import { useEntityProp } from '@wordpress/core-data'; | ||
|
||
export function Edit() { | ||
const blockProps = useBlockProps(); | ||
const [ name, setName ] = useEntityProp( 'postType', 'product', 'name' ); | ||
|
||
return ( | ||
<div { ...blockProps }> | ||
<TextControl | ||
label={ interpolateComponents( { | ||
mixedString: __( 'Name {{required/}}', 'woocommerce' ), | ||
components: { | ||
required: ( | ||
<span className="woocommerce-product-form__optional-input"> | ||
{ __( '(required)', 'woocommerce' ) } | ||
</span> | ||
), | ||
}, | ||
} ) } | ||
name={ 'woocommerce-product-name' } | ||
placeholder={ __( 'e.g. 12 oz Coffee Mug', 'woocommerce' ) } | ||
onChange={ setName } | ||
value={ name } | ||
/> | ||
</div> | ||
); | ||
} |
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,17 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { initBlock } from '../../utils'; | ||
import metadata from './block'; | ||
import { Edit } from './edit'; | ||
|
||
const { name } = metadata; | ||
|
||
export { metadata, name }; | ||
|
||
export const settings = { | ||
example: {}, | ||
edit: Edit, | ||
}; | ||
|
||
export const init = () => initBlock( { name, metadata, settings } ); |
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
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,26 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { BlockConfiguration, registerBlockType } from '@wordpress/blocks'; | ||
|
||
interface BlockRepresentation { | ||
name: string; | ||
metadata: BlockConfiguration; | ||
settings: Partial< BlockConfiguration >; | ||
} | ||
|
||
/** | ||
* Function to register an individual block. | ||
* | ||
* @param {Object} block The block to be registered. | ||
* | ||
* @return {?WPBlockType} The block, if it has been successfully registered; | ||
* otherwise `undefined`. | ||
*/ | ||
export const initBlock = ( block: BlockRepresentation ) => { | ||
if ( ! block ) { | ||
return; | ||
} | ||
const { metadata, settings, name } = block; | ||
return registerBlockType( { name, ...metadata }, settings ); | ||
}; |
22 changes: 22 additions & 0 deletions
22
packages/js/product-editor/src/utils/parse-product-to-blocks.ts
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,22 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { Product } from '@woocommerce/data'; | ||
import { BlockInstance, createBlock } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import nameBlock from '../components/name/block'; | ||
|
||
export function parseProductToBlocks( product: Partial< Product > ) { | ||
const blocks: BlockInstance[] = []; | ||
|
||
blocks.push( | ||
createBlock( nameBlock.name, { | ||
name: product.name, | ||
} ) | ||
); | ||
|
||
return blocks; | ||
} |
Oops, something went wrong.