Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 36 additions & 17 deletions search-replace-for-block-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}
13 changes: 11 additions & 2 deletions src/core/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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( '' );
}
};

/**
Expand All @@ -61,7 +71,6 @@ const SearchReplaceForBlockEditor = (): JSX.Element => {
*/
const closeModal = (): void => {
setIsModalVisible( false );
setReplacements( 0 );
};

/**
Expand Down