From 62de815decd77814cd294e7dc4ae5a0a5698ed67 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 30 Dec 2021 17:37:05 +0200 Subject: [PATCH 1/3] [Block Library - Query Loop]: Add block settings menu item to reset(remove the Innerblocks) --- .../block-library/src/query/edit/index.js | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/query/edit/index.js b/packages/block-library/src/query/edit/index.js index bf8e95d06e21a8..751e7d7c7015c8 100644 --- a/packages/block-library/src/query/edit/index.js +++ b/packages/block-library/src/query/edit/index.js @@ -13,8 +13,9 @@ import { store as blockEditorStore, useInnerBlocksProps, __experimentalBlockPatternSetup as BlockPatternSetup, + BlockSettingsMenuControls, } from '@wordpress/block-editor'; -import { SelectControl } from '@wordpress/components'; +import { SelectControl, MenuItem } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; /** @@ -27,7 +28,34 @@ import { DEFAULTS_POSTS_PER_PAGE } from '../constants'; import { getFirstQueryClientIdFromBlocks } from '../utils'; const TEMPLATE = [ [ 'core/post-template' ] ]; -export function QueryContent( { attributes, setAttributes } ) { + +function ResetQueryLoopMenuItem( { clientId } ) { + const selectedBlockClientId = useSelect( + ( select ) => select( blockEditorStore ).getSelectedBlockClientId(), + [] + ); + const { replaceInnerBlocks } = useDispatch( blockEditorStore ); + if ( selectedBlockClientId !== clientId ) { + return null; + } + return ( + + { ( { onClose } ) => ( + { + replaceInnerBlocks( clientId, [] ); + onClose(); + } } + info={ __( 'Remove all innerblocks and start fresh' ) } + > + { __( 'Reset Query Loop' ) } + + ) } + + ); +} + +export function QueryContent( { attributes, setAttributes, clientId } ) { const { queryId, query, @@ -104,6 +132,7 @@ export function QueryContent( { attributes, setAttributes } ) { setDisplayLayout={ updateDisplayLayout } /> + Date: Fri, 7 Jan 2022 11:25:04 +0200 Subject: [PATCH 2/3] use `isSelected` prop and only show on top level blocks --- .../block-library/src/query/edit/index.js | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/packages/block-library/src/query/edit/index.js b/packages/block-library/src/query/edit/index.js index 751e7d7c7015c8..a75159fcdc4458 100644 --- a/packages/block-library/src/query/edit/index.js +++ b/packages/block-library/src/query/edit/index.js @@ -29,13 +29,14 @@ import { getFirstQueryClientIdFromBlocks } from '../utils'; const TEMPLATE = [ [ 'core/post-template' ] ]; -function ResetQueryLoopMenuItem( { clientId } ) { - const selectedBlockClientId = useSelect( - ( select ) => select( blockEditorStore ).getSelectedBlockClientId(), - [] +function ResetQueryLoopMenuItem( { clientId, isSelected } ) { + const rootClientId = useSelect( + ( select ) => + select( blockEditorStore ).getBlockRootClientId( clientId ), + [ clientId ] ); const { replaceInnerBlocks } = useDispatch( blockEditorStore ); - if ( selectedBlockClientId !== clientId ) { + if ( ! isSelected || rootClientId ) { return null; } return ( @@ -55,7 +56,12 @@ function ResetQueryLoopMenuItem( { clientId } ) { ); } -export function QueryContent( { attributes, setAttributes, clientId } ) { +export function QueryContent( { + attributes, + setAttributes, + clientId, + isSelected, +} ) { const { queryId, query, @@ -132,7 +138,10 @@ export function QueryContent( { attributes, setAttributes, clientId } ) { setDisplayLayout={ updateDisplayLayout } /> - + Date: Wed, 12 Jan 2022 14:21:56 +0200 Subject: [PATCH 3/3] remove top level block check --- packages/block-library/src/query/edit/index.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/block-library/src/query/edit/index.js b/packages/block-library/src/query/edit/index.js index a75159fcdc4458..80c160ca83bb00 100644 --- a/packages/block-library/src/query/edit/index.js +++ b/packages/block-library/src/query/edit/index.js @@ -30,13 +30,8 @@ import { getFirstQueryClientIdFromBlocks } from '../utils'; const TEMPLATE = [ [ 'core/post-template' ] ]; function ResetQueryLoopMenuItem( { clientId, isSelected } ) { - const rootClientId = useSelect( - ( select ) => - select( blockEditorStore ).getBlockRootClientId( clientId ), - [ clientId ] - ); const { replaceInnerBlocks } = useDispatch( blockEditorStore ); - if ( ! isSelected || rootClientId ) { + if ( ! isSelected ) { return null; } return (