From 01a0772920968b8dc08f7a46abcab098d8788d67 Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Wed, 24 May 2023 09:23:00 +0200 Subject: [PATCH 1/2] Pattern: Try inner blocks syncing for editable content attributes --- lib/experimental/pattern.php | 39 ++++++++++++ packages/block-library/src/pattern/edit.js | 68 ++++++++++++++++++--- packages/block-library/src/pattern/index.js | 3 +- packages/block-library/src/pattern/save.js | 12 ++++ 4 files changed, 114 insertions(+), 8 deletions(-) create mode 100644 lib/experimental/pattern.php create mode 100644 packages/block-library/src/pattern/save.js diff --git a/lib/experimental/pattern.php b/lib/experimental/pattern.php new file mode 100644 index 00000000000000..eb048785c0922d --- /dev/null +++ b/lib/experimental/pattern.php @@ -0,0 +1,39 @@ + esc_html__( 'Get In Touch', 'default' ), + 'categories' => array( 'call-to-action' ), + 'content' => implode( + '', + array( + '', + '

' . esc_html__( 'Get In Touch', 'default' ) . '

', + '', + '', + '
', + '
', + '

' . esc_html__( '20 Cooper Avenue', 'default' ) . '
' . esc_html__( 'New York, New York 10023', 'default' ) . '

', + '
', + '', + '', + '
', + '

' . esc_html__( '(555) 555-5555', 'default' ) . '
' . esc_html__( 'example@example.com', 'default' ) . '

', + '
', + '
', + '', + '', + '
', + '', + '
', + '', + ) + ), + ) +); diff --git a/packages/block-library/src/pattern/edit.js b/packages/block-library/src/pattern/edit.js index c22536a59eb03f..c4e5c8efc2d4cb 100644 --- a/packages/block-library/src/pattern/edit.js +++ b/packages/block-library/src/pattern/edit.js @@ -9,8 +9,9 @@ import { useBlockProps, useInnerBlocksProps, } from '@wordpress/block-editor'; +import { Placeholder, Spinner } from '@wordpress/components'; -const PatternEdit = ( { attributes, clientId } ) => { +function PatternFullEdit( { attributes, clientId } ) { const { slug, syncStatus } = attributes; const { selectedPattern, innerBlocks } = useSelect( ( select ) => { @@ -69,15 +70,68 @@ const PatternEdit = ( { attributes, clientId } ) => { className: slug?.replace( '/', '-' ), } ); + return
; +} + +function PatternPartialEdit( { attributes: { slug }, clientId } ) { + const { designPattern } = useSelect( + ( select ) => { + return { + designPattern: + select( blockEditorStore ).__experimentalGetParsedPattern( + slug + ), + /*innerBlocks: + select( blockEditorStore ).getBlock( clientId ) + ?.innerBlocks,*/ + }; + }, + [ slug ] + ); + + const blockProps = useBlockProps( { + className: slug?.replace( '/', '-' ), + } ); + if ( ! designPattern?.blocks?.length ) { + return ( +
+ + + +
+ ); + } + + return ( + + ); +} + +function PatternInnerBlocks( { clientId, blockProps, template } ) { const innerBlocksProps = useInnerBlocksProps( blockProps, { - templateLock: syncStatus === 'partial' ? 'contentOnly' : false, + templateLock: 'contentOnly', } ); - if ( syncStatus !== 'partial' ) { - return
; - } + const { replaceInnerBlocks } = useDispatch( blockEditorStore ); + + useEffect( () => { + const blocks = template.map( ( block ) => cloneBlock( block ) ); + replaceInnerBlocks( clientId, blocks ); + }, [ clientId, replaceInnerBlocks, template ] ); return
; -}; +} -export default PatternEdit; +export default function PatternEdit( props ) { + const { syncStatus } = props.attributes; + + return syncStatus === 'partial' ? ( + + ) : ( + + ); +} diff --git a/packages/block-library/src/pattern/index.js b/packages/block-library/src/pattern/index.js index 27e74510eb5972..a7445c8b6e857d 100644 --- a/packages/block-library/src/pattern/index.js +++ b/packages/block-library/src/pattern/index.js @@ -5,12 +5,13 @@ import initBlock from '../utils/init-block'; import metadata from './block.json'; import PatternEditV1 from './v1/edit'; import PatternEditV2 from './edit'; +import save from './save'; const { name } = metadata; export { metadata, name }; export const settings = window?.__experimentalEnablePatternEnhancements - ? { edit: PatternEditV2 } + ? { edit: PatternEditV2, save } : { edit: PatternEditV1 }; export const init = () => initBlock( { name, metadata, settings } ); diff --git a/packages/block-library/src/pattern/save.js b/packages/block-library/src/pattern/save.js new file mode 100644 index 00000000000000..5e49e3651d076c --- /dev/null +++ b/packages/block-library/src/pattern/save.js @@ -0,0 +1,12 @@ +/** + * WordPress dependencies + */ +import { InnerBlocks } from '@wordpress/block-editor'; + +export default function save( { attributes: { syncStatus } } ) { + if ( syncStatus !== 'partial' ) { + return null; + } + + return ; +} From e8a87e8ee0414c804cf77280f61d6c711c0fff74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20Zi=C3=B3=C5=82kowski?= Date: Fri, 26 May 2023 11:35:18 +0200 Subject: [PATCH 2/2] Remove the pattern.php file --- lib/experimental/pattern.php | 39 ------------------------------------ 1 file changed, 39 deletions(-) delete mode 100644 lib/experimental/pattern.php diff --git a/lib/experimental/pattern.php b/lib/experimental/pattern.php deleted file mode 100644 index eb048785c0922d..00000000000000 --- a/lib/experimental/pattern.php +++ /dev/null @@ -1,39 +0,0 @@ - esc_html__( 'Get In Touch', 'default' ), - 'categories' => array( 'call-to-action' ), - 'content' => implode( - '', - array( - '', - '

' . esc_html__( 'Get In Touch', 'default' ) . '

', - '', - '', - '
', - '
', - '

' . esc_html__( '20 Cooper Avenue', 'default' ) . '
' . esc_html__( 'New York, New York 10023', 'default' ) . '

', - '
', - '', - '', - '
', - '

' . esc_html__( '(555) 555-5555', 'default' ) . '
' . esc_html__( 'example@example.com', 'default' ) . '

', - '
', - '
', - '', - '', - '', - '', - ) - ), - ) -);