From 01daa00771f4cae9efda94192934a8088aff4f2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Tue, 6 Jul 2021 18:08:47 +0200 Subject: [PATCH 01/13] Export members of legacy window.wp.editor from the new window.wp.editor module for BC --- packages/editor/src/index.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/editor/src/index.js b/packages/editor/src/index.js index 31fe9a672a72d1..d60be9ac1c2668 100644 --- a/packages/editor/src/index.js +++ b/packages/editor/src/index.js @@ -7,6 +7,14 @@ export { storeConfig, store } from './store'; export * from './components'; export * from './utils'; +export const { + autop, + removep, + initialize, + remove, + getContent, +} = window.wp.oldEditor; + /* * Backward compatibility */ From b644a7dc1134919205b1ef5226733363bfd413f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Tue, 6 Jul 2021 18:10:49 +0200 Subject: [PATCH 02/13] Add an explanatory comment --- packages/editor/src/index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/editor/src/index.js b/packages/editor/src/index.js index d60be9ac1c2668..8b7efd0a8e486a 100644 --- a/packages/editor/src/index.js +++ b/packages/editor/src/index.js @@ -7,6 +7,10 @@ export { storeConfig, store } from './store'; export * from './components'; export * from './utils'; +// This module is exposed as window.wp.editor +// Problem: there is quite some code expecting another, legacy object available under window.wp.editor +// Solution: export all the members of legacy window.wp.editor from this new module to maintain backward compatibility +// For more context, see https://github.com/WordPress/gutenberg/issues/33203 export const { autop, removep, From d880b51728114a8d5f34fe174b8cf14be5c2c7d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Tue, 6 Jul 2021 18:20:27 +0200 Subject: [PATCH 03/13] Use optional chaining for editor/index.js exports --- packages/editor/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/editor/src/index.js b/packages/editor/src/index.js index 8b7efd0a8e486a..57ec5e45942822 100644 --- a/packages/editor/src/index.js +++ b/packages/editor/src/index.js @@ -17,7 +17,7 @@ export const { initialize, remove, getContent, -} = window.wp.oldEditor; +} = window?.wp?.oldEditor; /* * Backward compatibility From 8d4a984cac919c5241136b941b11a436284ec9f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Tue, 6 Jul 2021 18:28:23 +0200 Subject: [PATCH 04/13] Default to an empty object on export from oldEditor --- packages/editor/src/index.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/packages/editor/src/index.js b/packages/editor/src/index.js index 57ec5e45942822..bb7207d4ce95a0 100644 --- a/packages/editor/src/index.js +++ b/packages/editor/src/index.js @@ -11,13 +11,8 @@ export * from './utils'; // Problem: there is quite some code expecting another, legacy object available under window.wp.editor // Solution: export all the members of legacy window.wp.editor from this new module to maintain backward compatibility // For more context, see https://github.com/WordPress/gutenberg/issues/33203 -export const { - autop, - removep, - initialize, - remove, - getContent, -} = window?.wp?.oldEditor; +export const { autop, removep, initialize, remove, getContent } = + window?.wp?.oldEditor || {}; /* * Backward compatibility From ae6615575da22bf9ae7e51316c721b5a27dbb6f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Wed, 7 Jul 2021 18:14:56 +0200 Subject: [PATCH 05/13] Use an inline script instead of modifying the @wordpress/editor package --- lib/widgets-customize.php | 1 + lib/widgets-page.php | 1 + lib/widgets.php | 16 ++++++++++++++++ packages/editor/src/index.js | 7 ------- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/widgets-customize.php b/lib/widgets-customize.php index fce4d6dc576809..5e0a614f105c3d 100644 --- a/lib/widgets-customize.php +++ b/lib/widgets-customize.php @@ -146,6 +146,7 @@ function gutenberg_customize_widgets_init() { wp_enqueue_style( 'wp-customize-widgets' ); wp_enqueue_script( 'wp-format-library' ); wp_enqueue_style( 'wp-format-library' ); + gutenberg_enqueue_wp_old_editor_compatibility_snippet(); do_action( 'enqueue_block_editor_assets' ); } diff --git a/lib/widgets-page.php b/lib/widgets-page.php index 067317a74299ad..23e7456984d814 100644 --- a/lib/widgets-page.php +++ b/lib/widgets-page.php @@ -76,6 +76,7 @@ function gutenberg_widgets_init( $hook ) { return; } + gutenberg_enqueue_wp_old_editor_compatibility_snippet(); add_filter( 'admin_body_class', 'gutenberg_widgets_editor_add_admin_body_classes' ); $widgets_editor_context = new WP_Block_Editor_Context(); diff --git a/lib/widgets.php b/lib/widgets.php index a2cc51fa293d57..5944461271bff8 100644 --- a/lib/widgets.php +++ b/lib/widgets.php @@ -50,6 +50,22 @@ function gutenberg_use_widgets_block_editor() { ); } +/** + * wp-editor module is exposed as window.wp.editor + * Problem: there is quite some code expecting window.wp.oldEditor object available under window.wp.editor + * Solution: fuse the two objects together to maintain backward compatibility + * For more context, see https://github.com/WordPress/gutenberg/issues/33203 + * + * @return void + */ +function gutenberg_enqueue_wp_old_editor_compatibility_snippet() { + wp_add_inline_script( + 'wp-editor', + 'Object.assign(window.wp.editor, window.wp.oldEditor);', + 'after' + ); +} + /** * Returns the settings required by legacy widgets blocks. * diff --git a/packages/editor/src/index.js b/packages/editor/src/index.js index bb7207d4ce95a0..31fe9a672a72d1 100644 --- a/packages/editor/src/index.js +++ b/packages/editor/src/index.js @@ -7,13 +7,6 @@ export { storeConfig, store } from './store'; export * from './components'; export * from './utils'; -// This module is exposed as window.wp.editor -// Problem: there is quite some code expecting another, legacy object available under window.wp.editor -// Solution: export all the members of legacy window.wp.editor from this new module to maintain backward compatibility -// For more context, see https://github.com/WordPress/gutenberg/issues/33203 -export const { autop, removep, initialize, remove, getContent } = - window?.wp?.oldEditor || {}; - /* * Backward compatibility */ From ed8f3a8b0cb64913987f4d8aab97fa221582d208 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Wed, 7 Jul 2021 18:21:37 +0200 Subject: [PATCH 06/13] Lint --- lib/widgets.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/widgets.php b/lib/widgets.php index 5944461271bff8..388d9c572caab7 100644 --- a/lib/widgets.php +++ b/lib/widgets.php @@ -51,9 +51,9 @@ function gutenberg_use_widgets_block_editor() { } /** - * wp-editor module is exposed as window.wp.editor - * Problem: there is quite some code expecting window.wp.oldEditor object available under window.wp.editor - * Solution: fuse the two objects together to maintain backward compatibility + * Wp-editor module is exposed as window.wp.editor. + * Problem: there is quite some code expecting window.wp.oldEditor object available under window.wp.editor. + * Solution: fuse the two objects together to maintain backward compatibility. * For more context, see https://github.com/WordPress/gutenberg/issues/33203 * * @return void From f4601f76cf5f8657840d2aee6b5a363067fbb5d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Thu, 8 Jul 2021 12:10:26 +0200 Subject: [PATCH 07/13] Move the inline script to gutenberg_override_script --- lib/client-assets.php | 12 ++++++++++++ lib/widgets.php | 16 ---------------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index fa1832d2f23729..1c0332c4512c02 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -102,6 +102,18 @@ function gutenberg_override_script( $scripts, $handle, $src, $deps = array(), $v $output = sprintf( "wp.i18n.setLocaleData( { 'text direction\u0004ltr': [ '%s' ] }, 'default' );", $ltr ); $scripts->add_inline_script( 'wp-i18n', $output, 'after' ); } + + /* + * Wp-editor module is exposed as window.wp.editor. + * Problem: there is quite some code expecting window.wp.oldEditor object available under window.wp.editor. + * Solution: fuse the two objects together to maintain backward compatibility. + * For more context, see https://github.com/WordPress/gutenberg/issues/33203 + */ + wp_add_inline_script( + 'wp-editor', + 'Object.assign(window.wp.editor, window.wp.oldEditor);', + 'after' + ); } /** diff --git a/lib/widgets.php b/lib/widgets.php index 388d9c572caab7..a2cc51fa293d57 100644 --- a/lib/widgets.php +++ b/lib/widgets.php @@ -50,22 +50,6 @@ function gutenberg_use_widgets_block_editor() { ); } -/** - * Wp-editor module is exposed as window.wp.editor. - * Problem: there is quite some code expecting window.wp.oldEditor object available under window.wp.editor. - * Solution: fuse the two objects together to maintain backward compatibility. - * For more context, see https://github.com/WordPress/gutenberg/issues/33203 - * - * @return void - */ -function gutenberg_enqueue_wp_old_editor_compatibility_snippet() { - wp_add_inline_script( - 'wp-editor', - 'Object.assign(window.wp.editor, window.wp.oldEditor);', - 'after' - ); -} - /** * Returns the settings required by legacy widgets blocks. * From 723a9c8a190c7598617cd8b848f596fc3d11ee40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Thu, 8 Jul 2021 12:14:36 +0200 Subject: [PATCH 08/13] Remove gutenberg_enqueue_wp_old_editor_compatibility_snippet --- lib/client-assets.php | 2 +- lib/widgets-customize.php | 1 - lib/widgets-page.php | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 1c0332c4512c02..1cc21e77d34b26 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -109,7 +109,7 @@ function gutenberg_override_script( $scripts, $handle, $src, $deps = array(), $v * Solution: fuse the two objects together to maintain backward compatibility. * For more context, see https://github.com/WordPress/gutenberg/issues/33203 */ - wp_add_inline_script( + $scripts->add_inline_script( 'wp-editor', 'Object.assign(window.wp.editor, window.wp.oldEditor);', 'after' diff --git a/lib/widgets-customize.php b/lib/widgets-customize.php index 5e0a614f105c3d..fce4d6dc576809 100644 --- a/lib/widgets-customize.php +++ b/lib/widgets-customize.php @@ -146,7 +146,6 @@ function gutenberg_customize_widgets_init() { wp_enqueue_style( 'wp-customize-widgets' ); wp_enqueue_script( 'wp-format-library' ); wp_enqueue_style( 'wp-format-library' ); - gutenberg_enqueue_wp_old_editor_compatibility_snippet(); do_action( 'enqueue_block_editor_assets' ); } diff --git a/lib/widgets-page.php b/lib/widgets-page.php index 23e7456984d814..067317a74299ad 100644 --- a/lib/widgets-page.php +++ b/lib/widgets-page.php @@ -76,7 +76,6 @@ function gutenberg_widgets_init( $hook ) { return; } - gutenberg_enqueue_wp_old_editor_compatibility_snippet(); add_filter( 'admin_body_class', 'gutenberg_widgets_editor_add_admin_body_classes' ); $widgets_editor_context = new WP_Block_Editor_Context(); From 59db57cc3dc5b50cc02850ff1baa6743f4c64b85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Thu, 8 Jul 2021 12:15:18 +0200 Subject: [PATCH 09/13] Formatting --- lib/client-assets.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 1cc21e77d34b26..af4baa362d4cae 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -111,7 +111,7 @@ function gutenberg_override_script( $scripts, $handle, $src, $deps = array(), $v */ $scripts->add_inline_script( 'wp-editor', - 'Object.assign(window.wp.editor, window.wp.oldEditor);', + 'Object.assign( window.wp.editor, window.wp.oldEditor );', 'after' ); } From ae4b7ac16131944d2a29af47371949258123311c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Thu, 8 Jul 2021 18:20:11 +0200 Subject: [PATCH 10/13] Adjust wp-editor-meta-box E2E test --- .../e2e-tests/specs/editor/plugins/wp-editor-meta-box.test.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/e2e-tests/specs/editor/plugins/wp-editor-meta-box.test.js b/packages/e2e-tests/specs/editor/plugins/wp-editor-meta-box.test.js index 3045575ff15f5d..c5d967815a8597 100644 --- a/packages/e2e-tests/specs/editor/plugins/wp-editor-meta-box.test.js +++ b/packages/e2e-tests/specs/editor/plugins/wp-editor-meta-box.test.js @@ -39,8 +39,6 @@ describe( 'WP Editor Meta Boxes', () => { ( textarea ) => textarea.value ); - expect( content ).toMatchInlineSnapshot( - `"

Typing in a metabox

"` - ); + expect( content ).toBe( 'Typing in a metabox' ); } ); } ); From 48c19298c267b5f1c261996b5acac31eb27b2bd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Thu, 8 Jul 2021 18:55:24 +0200 Subject: [PATCH 11/13] Only add inline script if the handle is wp-editor --- lib/client-assets.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index af4baa362d4cae..4adc79fc689042 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -109,11 +109,13 @@ function gutenberg_override_script( $scripts, $handle, $src, $deps = array(), $v * Solution: fuse the two objects together to maintain backward compatibility. * For more context, see https://github.com/WordPress/gutenberg/issues/33203 */ - $scripts->add_inline_script( - 'wp-editor', - 'Object.assign( window.wp.editor, window.wp.oldEditor );', - 'after' - ); + if ( 'wp-editor' === $handle ) { + $scripts->add_inline_script( + 'wp-editor', + 'Object.assign( window.wp.editor, window.wp.oldEditor );', + 'after' + ); + } } /** From 7277ce94af3898ed8d945d6021b53471746d377b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Fri, 9 Jul 2021 14:02:29 +0200 Subject: [PATCH 12/13] Add explanatory comment for the e2e test --- .../specs/editor/plugins/wp-editor-meta-box.test.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/e2e-tests/specs/editor/plugins/wp-editor-meta-box.test.js b/packages/e2e-tests/specs/editor/plugins/wp-editor-meta-box.test.js index c5d967815a8597..ab75e6ee54bf14 100644 --- a/packages/e2e-tests/specs/editor/plugins/wp-editor-meta-box.test.js +++ b/packages/e2e-tests/specs/editor/plugins/wp-editor-meta-box.test.js @@ -39,6 +39,18 @@ describe( 'WP Editor Meta Boxes', () => { ( textarea ) => textarea.value ); + /* + * `content` may or may not contain the

tags depending on hasWpautop value in this line: + * https://github.com/WordPress/wordpress-develop/blob/2382765afa36e10bf3c74420024ad4e85763a47c/src/js/_enqueues/vendor/tinymce/plugins/wordpress/plugin.js#L15 + * + * Now, for the purposes of this e2e test we explicitly set wpautop to true in the test plugin: + * https://github.com/WordPress/gutenberg/blob/3da717b8d0ac7d7821fc6d0475695ccf3ae2829f/packages/e2e-tests/plugins/wp-editor-metabox.php#L36 + * + * If this test randomly fails because of the actual value being wrapped in

like

Typing in a metabox

, it means that + * hasWpautop has been errorneously set to false in the line above. You may want to check: + * * Is window.wp.editor.autop a function? It should be one since https://github.com/WordPress/gutenberg/pull/33228 + * * Is wpautop still true in the second link mentioned in this comment? + */ expect( content ).toBe( 'Typing in a metabox' ); } ); } ); From c714c89173c1080f20f27504c8351c1ecf0316f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Fri, 9 Jul 2021 14:03:54 +0200 Subject: [PATCH 13/13] Add even more context to that comment --- .../e2e-tests/specs/editor/plugins/wp-editor-meta-box.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/e2e-tests/specs/editor/plugins/wp-editor-meta-box.test.js b/packages/e2e-tests/specs/editor/plugins/wp-editor-meta-box.test.js index ab75e6ee54bf14..867c3cf7b40138 100644 --- a/packages/e2e-tests/specs/editor/plugins/wp-editor-meta-box.test.js +++ b/packages/e2e-tests/specs/editor/plugins/wp-editor-meta-box.test.js @@ -50,6 +50,8 @@ describe( 'WP Editor Meta Boxes', () => { * hasWpautop has been errorneously set to false in the line above. You may want to check: * * Is window.wp.editor.autop a function? It should be one since https://github.com/WordPress/gutenberg/pull/33228 * * Is wpautop still true in the second link mentioned in this comment? + * + * For more context, see https://github.com/WordPress/gutenberg/pull/33228/files#r666897885 */ expect( content ).toBe( 'Typing in a metabox' ); } );