Skip to content

Commit 0c90a33

Browse files
authored
Merge pull request #65 from badasswp/feat/show-text-count-for-selection-on-modal-open
Feat: On Modal open, show items found for Highlighted text
2 parents 9fc3e5b + b392b2d commit 0c90a33

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

search-replace-for-block-editor.php

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,39 @@
2020
die;
2121
}
2222

23+
define( 'SRFBE', 'search-replace-for-block-editor' );
24+
2325
/**
2426
* Load Search & Replace Script for Block Editor.
2527
*
2628
* @since 1.0.0
2729
* @since 1.0.2 Load asset via plugin directory URL.
2830
* @since 1.2.2 Localise WP version.
31+
* @since 1.7.0 Use webpack generated PHP asset file.
2932
*
3033
* @wp-hook 'enqueue_block_editor_assets'
3134
*/
3235
add_action( 'enqueue_block_editor_assets', function() {
3336
global $wp_version;
3437

38+
$assets = get_assets( plugin_dir_path( __FILE__ ) . './dist/app.asset.php' );
39+
3540
wp_enqueue_script(
36-
'search-replace-for-block-editor',
41+
SRFBE,
3742
trailingslashit( plugin_dir_url( __FILE__ ) ) . 'dist/app.js',
38-
[
39-
'wp-i18n',
40-
'wp-element',
41-
'wp-blocks',
42-
'wp-components',
43-
'wp-editor',
44-
'wp-hooks',
45-
'wp-compose',
46-
'wp-plugins',
47-
'wp-edit-post',
48-
],
49-
'1.6.0',
43+
$assets['dependencies'],
44+
$assets['version'],
5045
false,
5146
);
5247

5348
wp_set_script_translations(
54-
'search-replace-for-block-editor',
55-
'search-replace-for-block-editor',
49+
SRFBE,
50+
SRFBE,
5651
plugin_dir_path( __FILE__ ) . 'languages'
5752
);
5853

5954
wp_localize_script(
60-
'search-replace-for-block-editor',
55+
SRFBE,
6156
'srfbe',
6257
[
6358
'wpVersion' => $wp_version,
@@ -74,8 +69,32 @@
7469
*/
7570
add_action( 'init', function() {
7671
load_plugin_textdomain(
77-
'search-replace-for-block-editor',
72+
SRFBE,
7873
false,
7974
dirname( plugin_basename( __FILE__ ) ) . '/languages'
8075
);
8176
} );
77+
78+
/**
79+
* Get Asset dependencies.
80+
*
81+
* @since 1.7.0
82+
*
83+
* @param string $path Path to webpack generated PHP asset file.
84+
* @return array
85+
*/
86+
function get_assets( string $path ): array {
87+
$assets = [
88+
'version' => strval( time() ),
89+
'dependencies' => [],
90+
];
91+
92+
if ( ! file_exists( $path ) ) {
93+
return $assets;
94+
}
95+
96+
// phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable
97+
$assets = require_once $path;
98+
99+
return $assets;
100+
}

src/core/app.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,17 @@ const SearchReplaceForBlockEditor = (): JSX.Element => {
5252
*/
5353
const openModal = (): void => {
5454
setIsModalVisible( true );
55-
setReplacements( 0 );
55+
56+
// Get selected text, if any.
57+
const selectedText: string = getBlockEditorIframe()
58+
.getSelection()
59+
.toString();
60+
61+
// By default, reset count and search input.
62+
if ( ! selectedText ) {
63+
setReplacements( 0 );
64+
setSearchInput( '' );
65+
}
5666
};
5767

5868
/**
@@ -64,7 +74,6 @@ const SearchReplaceForBlockEditor = (): JSX.Element => {
6474
*/
6575
const closeModal = (): void => {
6676
setIsModalVisible( false );
67-
setReplacements( 0 );
6877
};
6978

7079
/**

0 commit comments

Comments
 (0)