Skip to content

Commit

Permalink
Block Editor: Add settings to enable/disable auto anchor generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Feb 14, 2022
1 parent 037c087 commit 56f4b01
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/block-editor/src/store/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 17 additions & 4 deletions packages/block-library/src/heading/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -41,13 +41,25 @@ function HeadingEdit( {
style,
} );

const { canGenerateAnchors } = useSelect( ( select ) => {
const settings = select( blockEditorStore ).getSettings();

return {
canGenerateAnchors: !! settings.__experimentalGenerateAnchors,
};
}, [] );

const { __unstableMarkNextChangeAsNotPersistent } = useDispatch(
blockEditorStore
);

// 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();
Expand All @@ -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 );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ function useBlockEditorSettings( settings, hasTemplate ) {
'__experimentalFeatures',
'__experimentalPreferredStyleVariations',
'__experimentalSetIsInserterOpened',
'__experimentalGenerateAnchors',
'__unstableGalleryWithImageBlocks',
'alignWide',
'allowedBlockTypes',
Expand Down

0 comments on commit 56f4b01

Please sign in to comment.