From 1253b26004df921cb1a07ac98a5f8cb946f6d520 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Fri, 21 Jul 2023 11:37:22 +0400 Subject: [PATCH 1/4] Site Editor: Don't register the Footnotes block --- packages/block-library/src/footnotes/index.js | 5 ++--- packages/edit-site/src/index.js | 4 +++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/footnotes/index.js b/packages/block-library/src/footnotes/index.js index c0f3d60ada5432..f95f964177a5af 100644 --- a/packages/block-library/src/footnotes/index.js +++ b/packages/block-library/src/footnotes/index.js @@ -21,9 +21,8 @@ export const settings = { edit, }; -// Would be good to remove the format and HoR if the block is unregistered. -registerFormatType( formatName, format ); - export const init = () => { + // Would be good to remove the format and HoR if the block is unregistered. + registerFormatType( formatName, format ); initBlock( { name, metadata, settings } ); }; diff --git a/packages/edit-site/src/index.js b/packages/edit-site/src/index.js index ea2899a8abc105..818c8285933a02 100644 --- a/packages/edit-site/src/index.js +++ b/packages/edit-site/src/index.js @@ -29,6 +29,8 @@ import './hooks'; import { store as editSiteStore } from './store'; import App from './components/app'; +const DISALLOWED_BLOCKS = [ 'core/freeform', 'core/footnotes' ]; + /** * Initializes the site editor screen. * @@ -45,7 +47,7 @@ export function initializeEditor( id, settings ) { dispatch( blocksStore ).__experimentalReapplyBlockTypeFilters(); const coreBlocks = __experimentalGetCoreBlocks().filter( - ( { name } ) => name !== 'core/freeform' + ( { name } ) => ! DISALLOWED_BLOCKS.includes( name ) ); registerCoreBlocks( coreBlocks ); dispatch( blocksStore ).setFreeformFallbackBlockName( 'core/html' ); From 2d1b1bd5a713e4b2988a64c3ec756dab0b23e85c Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Fri, 21 Jul 2023 12:20:39 +0400 Subject: [PATCH 2/4] Fix unit tests --- packages/block-library/src/cover/test/edit.js | 8 +++++++- .../integration/helpers/integration-test-editor.js | 14 +++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/cover/test/edit.js b/packages/block-library/src/cover/test/edit.js index e399f379e21553..8cd521b4975c10 100644 --- a/packages/block-library/src/cover/test/edit.js +++ b/packages/block-library/src/cover/test/edit.js @@ -39,7 +39,13 @@ const disabledColorSettings = { async function setup( attributes, useCoreBlocks, customSettings ) { const testBlock = { name: 'core/cover', attributes }; const settings = customSettings || defaultSettings; - return initializeEditor( testBlock, useCoreBlocks, settings ); + const skippedBlocks = [ 'core/footnotes' ]; + return initializeEditor( + testBlock, + useCoreBlocks, + settings, + skippedBlocks + ); } async function createAndSelectBlock() { diff --git a/test/integration/helpers/integration-test-editor.js b/test/integration/helpers/integration-test-editor.js index 6b954a895a51ac..6bcf2a263a0789 100644 --- a/test/integration/helpers/integration-test-editor.js +++ b/test/integration/helpers/integration-test-editor.js @@ -18,7 +18,10 @@ import { ObserveTyping, } from '@wordpress/block-editor'; import { Popover, SlotFillProvider } from '@wordpress/components'; -import { registerCoreBlocks } from '@wordpress/block-library'; +import { + registerCoreBlocks, + __experimentalGetCoreBlocks, +} from '@wordpress/block-library'; import { ShortcutProvider } from '@wordpress/keyboard-shortcuts'; import '@wordpress/format-library'; import { @@ -100,14 +103,19 @@ export function Editor( { testBlocks, settings = {} } ) { * @param {Object | Array} testBlocks Block or array of block settings for blocks to be tested. * @param {boolean} useCoreBlocks Defaults to true. If false, core blocks will not be registered. * @param {Object} settings Any additional editor settings to be passed to the editor. + * @param {Array} skippedBlocks Blocks that should be skipped during registration. */ export async function initializeEditor( testBlocks, useCoreBlocks = true, - settings + settings, + skippedBlocks = [] ) { if ( useCoreBlocks ) { - registerCoreBlocks(); + const coreBlocks = __experimentalGetCoreBlocks().filter( + ( { name } ) => ! skippedBlocks.includes( name ) + ); + registerCoreBlocks( coreBlocks ); } const blocks = Array.isArray( testBlocks ) ? testBlocks : [ testBlocks ]; const newBlocks = blocks.map( ( testBlock ) => From d85be3dbf0e6002c63617642a845460bd4f07653 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Fri, 21 Jul 2023 12:25:32 +0400 Subject: [PATCH 3/4] Update comment --- packages/block-library/src/footnotes/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/footnotes/index.js b/packages/block-library/src/footnotes/index.js index f95f964177a5af..db9782cb567677 100644 --- a/packages/block-library/src/footnotes/index.js +++ b/packages/block-library/src/footnotes/index.js @@ -22,7 +22,7 @@ export const settings = { }; export const init = () => { - // Would be good to remove the format and HoR if the block is unregistered. + // Would be good to remove the format if the block is unregistered. registerFormatType( formatName, format ); initBlock( { name, metadata, settings } ); }; From 35877605521329ae837ee27f3a96f0a4c662a396 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 24 Jul 2023 13:03:06 +1200 Subject: [PATCH 4/4] unregister core formats on test end --- packages/block-library/src/cover/test/edit.js | 8 +---- .../helpers/integration-test-editor.js | 33 ++++++++++--------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/packages/block-library/src/cover/test/edit.js b/packages/block-library/src/cover/test/edit.js index 8cd521b4975c10..e399f379e21553 100644 --- a/packages/block-library/src/cover/test/edit.js +++ b/packages/block-library/src/cover/test/edit.js @@ -39,13 +39,7 @@ const disabledColorSettings = { async function setup( attributes, useCoreBlocks, customSettings ) { const testBlock = { name: 'core/cover', attributes }; const settings = customSettings || defaultSettings; - const skippedBlocks = [ 'core/footnotes' ]; - return initializeEditor( - testBlock, - useCoreBlocks, - settings, - skippedBlocks - ); + return initializeEditor( testBlock, useCoreBlocks, settings ); } async function createAndSelectBlock() { diff --git a/test/integration/helpers/integration-test-editor.js b/test/integration/helpers/integration-test-editor.js index 6bcf2a263a0789..7d5b3d9120509c 100644 --- a/test/integration/helpers/integration-test-editor.js +++ b/test/integration/helpers/integration-test-editor.js @@ -18,10 +18,7 @@ import { ObserveTyping, } from '@wordpress/block-editor'; import { Popover, SlotFillProvider } from '@wordpress/components'; -import { - registerCoreBlocks, - __experimentalGetCoreBlocks, -} from '@wordpress/block-library'; +import { registerCoreBlocks } from '@wordpress/block-library'; import { ShortcutProvider } from '@wordpress/keyboard-shortcuts'; import '@wordpress/format-library'; import { @@ -29,6 +26,8 @@ import { unregisterBlockType, getBlockTypes, } from '@wordpress/blocks'; +import { store as richTextStore } from '@wordpress/rich-text'; +import { useSelect, useDispatch } from '@wordpress/data'; /** * Internal dependencies @@ -63,13 +62,22 @@ export async function selectBlock( name ) { export function Editor( { testBlocks, settings = {} } ) { const [ currentBlocks, updateBlocks ] = useState( testBlocks ); + // Some blocks may register formats, eg. core/footnotes, so we need to also + // unregister those formats along with core blocks when the test is complete. + const availableFormats = useSelect( + ( select ) => select( richTextStore ).getFormatTypes(), + [] + ); + const { removeFormatTypes } = useDispatch( richTextStore ); + useEffect( () => { return () => { - getBlockTypes().forEach( ( { name } ) => - unregisterBlockType( name ) - ); + getBlockTypes().forEach( ( { name } ) => { + unregisterBlockType( name ); + } ); + removeFormatTypes( availableFormats.map( ( { name } ) => name ) ); }; - }, [] ); + }, [ availableFormats, removeFormatTypes ] ); return ( @@ -103,19 +111,14 @@ export function Editor( { testBlocks, settings = {} } ) { * @param {Object | Array} testBlocks Block or array of block settings for blocks to be tested. * @param {boolean} useCoreBlocks Defaults to true. If false, core blocks will not be registered. * @param {Object} settings Any additional editor settings to be passed to the editor. - * @param {Array} skippedBlocks Blocks that should be skipped during registration. */ export async function initializeEditor( testBlocks, useCoreBlocks = true, - settings, - skippedBlocks = [] + settings ) { if ( useCoreBlocks ) { - const coreBlocks = __experimentalGetCoreBlocks().filter( - ( { name } ) => ! skippedBlocks.includes( name ) - ); - registerCoreBlocks( coreBlocks ); + registerCoreBlocks(); } const blocks = Array.isArray( testBlocks ) ? testBlocks : [ testBlocks ]; const newBlocks = blocks.map( ( testBlock ) =>