diff --git a/search-replace-for-block-editor.php b/search-replace-for-block-editor.php index 4575535..30244fc 100644 --- a/search-replace-for-block-editor.php +++ b/search-replace-for-block-editor.php @@ -20,44 +20,39 @@ die; } +define( 'SRFBE', 'search-replace-for-block-editor' ); + /** * Load Search & Replace Script for Block Editor. * * @since 1.0.0 * @since 1.0.2 Load asset via plugin directory URL. * @since 1.2.2 Localise WP version. + * @since 1.7.0 Use webpack generated PHP asset file. * * @wp-hook 'enqueue_block_editor_assets' */ add_action( 'enqueue_block_editor_assets', function() { global $wp_version; + $assets = get_assets( plugin_dir_path( __FILE__ ) . './dist/app.asset.php' ); + wp_enqueue_script( - 'search-replace-for-block-editor', + SRFBE, trailingslashit( plugin_dir_url( __FILE__ ) ) . 'dist/app.js', - [ - 'wp-i18n', - 'wp-element', - 'wp-blocks', - 'wp-components', - 'wp-editor', - 'wp-hooks', - 'wp-compose', - 'wp-plugins', - 'wp-edit-post', - ], - '1.6.0', + $assets['dependencies'], + $assets['version'], false, ); wp_set_script_translations( - 'search-replace-for-block-editor', - 'search-replace-for-block-editor', + SRFBE, + SRFBE, plugin_dir_path( __FILE__ ) . 'languages' ); wp_localize_script( - 'search-replace-for-block-editor', + SRFBE, 'srfbe', [ 'wpVersion' => $wp_version, @@ -74,8 +69,32 @@ */ add_action( 'init', function() { load_plugin_textdomain( - 'search-replace-for-block-editor', + SRFBE, false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); } ); + +/** + * Get Asset dependencies. + * + * @since 1.7.0 + * + * @param string $path Path to webpack generated PHP asset file. + * @return array + */ +function get_assets( string $path ): array { + $assets = [ + 'version' => strval( time() ), + 'dependencies' => [], + ]; + + if ( ! file_exists( $path ) ) { + return $assets; + } + + // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable + $assets = require_once $path; + + return $assets; +} diff --git a/src/core/app.tsx b/src/core/app.tsx index 91ce729..1de741f 100644 --- a/src/core/app.tsx +++ b/src/core/app.tsx @@ -49,7 +49,17 @@ const SearchReplaceForBlockEditor = (): JSX.Element => { */ const openModal = (): void => { setIsModalVisible( true ); - setReplacements( 0 ); + + // Get selected text, if any. + const selectedText: string = getBlockEditorIframe() + .getSelection() + .toString(); + + // By default, reset count and search input. + if ( ! selectedText ) { + setReplacements( 0 ); + setSearchInput( '' ); + } }; /** @@ -61,7 +71,6 @@ const SearchReplaceForBlockEditor = (): JSX.Element => { */ const closeModal = (): void => { setIsModalVisible( false ); - setReplacements( 0 ); }; /**