Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/make sure block anchor stays unique #35757

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions packages/block-editor/src/hooks/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,48 @@ export function addSaveProps( extraProps, blockType, attributes ) {
return extraProps;
}

/**
* Make sure the anchor continues to stay unique when the block is cloned
*
* @param {string} blockType type of the block
* @param {Object} block cloned block object
* @return {Object} modified cloned block object
*/
export function cloneAttribute( blockType, block ) {
if (
hasBlockSupport( blockType, 'anchor' ) &&
has( block, 'attributes.anchor' )
) {
// check wether the anchor already ends with a `-${number}`
const index = block.attributes.anchor.lastIndexOf( '-' );
const result = block.attributes.anchor.substring( index + 1 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it would work when the anchor has numbers at the end in its original text. For instance, a heading with the anchor id top-100 will be incremented to top-101.


// store the anchor without the `-${number}`
const shortendAnchor =
index > 0
? block.attributes.anchor.substring( 0, index )
: block.attributes.anchor;

// generate the suffix number
let suffix = 1;
if ( result ) {
const number = parseInt( result );

if ( number ) {
suffix = number + 1;
}
}

// add the suffix to the anchor
block.attributes.anchor = `${ shortendAnchor }-${ suffix }`;
}

// return the modified cloned block
return block;
}

addFilter( 'blocks.registerBlockType', 'core/anchor/attribute', addAttribute );
addFilter( 'blocks.cloneBlock', 'core/anchor/clone', cloneAttribute );
addFilter(
'editor.BlockEdit',
'core/editor/anchor/with-inspector-control',
Expand Down
11 changes: 9 additions & 2 deletions packages/blocks/src/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,14 @@ export function __experimentalCloneSanitizedBlock(
}
);

return {
/**
* Filters the cloned block.
* The entire new block object is passed and can be modified
*
* @param {string} blockType block type of the block being transformed.
* @param {Object} block cloned block object
*/
return applyFilters( 'blocks.cloneBlock', block.name, {
...block,
clientId,
attributes: sanitizedAttributes,
Expand All @@ -127,7 +134,7 @@ export function __experimentalCloneSanitizedBlock(
block.innerBlocks.map( ( innerBlock ) =>
__experimentalCloneSanitizedBlock( innerBlock )
),
};
} );
}

/**
Expand Down