Skip to content

Commit 9b71255

Browse files
committed
Check all isEditorReady conditions after waiting.
1 parent 54d915b commit 9b71255

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

projects/plugins/jetpack/extensions/shared/wait-for-editor.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
import { select, subscribe } from '@wordpress/data';
22

3+
/**
4+
* Checks if the editor is ready by verifying if it's a clean new post or has blocks.
5+
*
6+
* @return {boolean} Whether the editor is ready.
7+
*/
8+
const isEditorReady = () => {
9+
return (
10+
select( 'core/editor' ).isCleanNewPost() || select( 'core/block-editor' ).getBlocks().length > 0
11+
);
12+
};
13+
314
/**
415
* Indicates if the block editor has been initialized.
516
*
617
* @return {Promise} Promise that resolves when the editor has been initialized.
718
*/
819
export const waitForEditor = async () =>
920
new Promise( resolve => {
10-
// Resolve immediately if it's a clean new post or has blocks
11-
if (
12-
select( 'core/editor' ).isCleanNewPost() ||
13-
select( 'core/block-editor' ).getBlocks().length > 0
14-
) {
21+
// Resolve immediately if editor is ready
22+
if ( isEditorReady() ) {
1523
resolve();
1624
return;
1725
}
@@ -23,8 +31,7 @@ export const waitForEditor = async () =>
2331
}, 2000 );
2432

2533
const unsubscribe = subscribe( () => {
26-
const blocks = select( 'core/block-editor' ).getBlocks();
27-
if ( blocks.length > 0 ) {
34+
if ( isEditorReady() ) {
2835
clearTimeout( timeoutId );
2936
unsubscribe();
3037
resolve();

0 commit comments

Comments
 (0)