diff --git a/packages/block-editor/src/store/defaults.js b/packages/block-editor/src/store/defaults.js index a51091dd8e6485..3f5d83bc609309 100644 --- a/packages/block-editor/src/store/defaults.js +++ b/packages/block-editor/src/store/defaults.js @@ -156,6 +156,7 @@ export const SETTINGS_DEFAULTS = { __experimentalBlockPatterns: [], __experimentalBlockPatternCategories: [], __experimentalSpotlightEntityBlocks: [], + __experimentalGenerateAnchors: false, __unstableGalleryWithImageBlocks: false, // gradients setting is not used anymore now defaults are passed from theme.json on the server and core has its own defaults. diff --git a/packages/block-library/src/heading/edit.js b/packages/block-library/src/heading/edit.js index d52c069a94f8a7..1629f10cc5a775 100644 --- a/packages/block-library/src/heading/edit.js +++ b/packages/block-library/src/heading/edit.js @@ -8,7 +8,7 @@ import classnames from 'classnames'; */ import { __ } from '@wordpress/i18n'; import { useEffect, Platform } from '@wordpress/element'; -import { useDispatch } from '@wordpress/data'; +import { useDispatch, useSelect } from '@wordpress/data'; import { createBlock, getDefaultBlockName } from '@wordpress/blocks'; import { AlignmentControl, @@ -41,6 +41,14 @@ function HeadingEdit( { style, } ); + const { canGenerateAnchors } = useSelect( ( select ) => { + const settings = select( blockEditorStore ).getSettings(); + + return { + canGenerateAnchors: !! settings.__experimentalGenerateAnchors, + }; + }, [] ); + const { __unstableMarkNextChangeAsNotPersistent } = useDispatch( blockEditorStore ); @@ -48,6 +56,10 @@ function HeadingEdit( { // Initially set anchor for headings that have content but no anchor set. // This is used when transforming a block to heading, or for legacy anchors. useEffect( () => { + if ( ! canGenerateAnchors ) { + return; + } + if ( ! anchor && content ) { // This side-effect should not create an undo level. __unstableMarkNextChangeAsNotPersistent(); @@ -64,9 +76,10 @@ function HeadingEdit( { const onContentChange = ( value ) => { const newAttrs = { content: value }; if ( - ! anchor || - ! value || - generateAnchor( clientId, content ) === anchor + canGenerateAnchors && + ( ! anchor || + ! value || + generateAnchor( clientId, content ) === anchor ) ) { newAttrs.anchor = generateAnchor( clientId, value ); } diff --git a/packages/editor/src/components/provider/use-block-editor-settings.js b/packages/editor/src/components/provider/use-block-editor-settings.js index 888d72c8e3d31b..2bc9e836c51ead 100644 --- a/packages/editor/src/components/provider/use-block-editor-settings.js +++ b/packages/editor/src/components/provider/use-block-editor-settings.js @@ -104,6 +104,7 @@ function useBlockEditorSettings( settings, hasTemplate ) { '__experimentalFeatures', '__experimentalPreferredStyleVariations', '__experimentalSetIsInserterOpened', + '__experimentalGenerateAnchors', '__unstableGalleryWithImageBlocks', 'alignWide', 'allowedBlockTypes',