From 35a8a684f13f8bb79cec75186bd59bd1b67ecfcb Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Thu, 18 May 2023 12:25:56 +0200 Subject: [PATCH 1/2] Support negation operator in selectors --- .../src/utils/interactivity/hooks.js | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/block-library/src/utils/interactivity/hooks.js b/packages/block-library/src/utils/interactivity/hooks.js index ca3bd20964d51..072c01641a59c 100644 --- a/packages/block-library/src/utils/interactivity/hooks.js +++ b/packages/block-library/src/utils/interactivity/hooks.js @@ -19,26 +19,28 @@ export const directive = ( name, cb ) => { // Resolve the path to some property of the store object. const resolve = ( path, ctx ) => { - // If path starts with !, remove it and save a flag. - const hasNegationOperator = - path[ 0 ] === '!' && !! ( path = path.slice( 1 ) ); let current = { ...store, context: ctx }; path.split( '.' ).forEach( ( p ) => ( current = current[ p ] ) ); - return hasNegationOperator ? ! current : current; + return current; }; // Generate the evaluate function. const getEvaluate = ( { ref } = {} ) => ( path, extraArgs = {} ) => { + // If path starts with !, remove it and save a flag. + const hasNegationOperator = + path[ 0 ] === '!' && !! ( path = path.slice( 1 ) ); const value = resolve( path, extraArgs.context ); - return typeof value === 'function' - ? value( { - ref: ref.current, - ...store, - ...extraArgs, - } ) - : value; + const returnValue = + typeof value === 'function' + ? value( { + ref: ref.current, + ...store, + ...extraArgs, + } ) + : value; + return hasNegationOperator ? ! returnValue : returnValue; }; // Directive wrapper. From 61e61e6932959d78e4bd14d6e2492827e8987890 Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Fri, 19 May 2023 09:23:51 +0200 Subject: [PATCH 2/2] Change file block to use negation operator --- lib/experimental/interactivity-api/blocks.php | 2 +- packages/block-library/src/file/interactivity.js | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/experimental/interactivity-api/blocks.php b/lib/experimental/interactivity-api/blocks.php index 755c1d1d4fa7d..647bad93c0d03 100644 --- a/lib/experimental/interactivity-api/blocks.php +++ b/lib/experimental/interactivity-api/blocks.php @@ -23,7 +23,7 @@ function gutenberg_block_core_file_add_directives_to_content( $block_content, $b $processor->next_tag(); $processor->set_attribute( 'data-wp-island', '' ); $processor->next_tag( 'object' ); - $processor->set_attribute( 'data-wp-bind.hidden', 'selectors.core.file.hasNoPdfPreview' ); + $processor->set_attribute( 'data-wp-bind.hidden', '!selectors.core.file.hasPdfPreview' ); $processor->set_attribute( 'hidden', true ); return $processor->get_updated_html(); } diff --git a/packages/block-library/src/file/interactivity.js b/packages/block-library/src/file/interactivity.js index cf9ae41002b27..8060f7addf3a2 100644 --- a/packages/block-library/src/file/interactivity.js +++ b/packages/block-library/src/file/interactivity.js @@ -2,15 +2,13 @@ * Internal dependencies */ import { store } from '../utils/interactivity'; -import { browserSupportsPdfs } from './utils'; +import { browserSupportsPdfs as hasPdfPreview } from './utils'; store( { selectors: { core: { file: { - hasNoPdfPreview() { - return ! browserSupportsPdfs(); - }, + hasPdfPreview, }, }, },