-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Conversation
Size Change: +119 B (0%) Total Size: 1.07 MB
ℹ️ View Unchanged
|
IMHO we should avoid introducing new hooks in JavaScript whenever possible. It's hard to trace in devtools and makes things implicit. It's sorta like mixins in React which is now deprecated and discouraged. This might be controversial though 😅 . |
) { | ||
// check wether the anchor already ends with a `-${number}` | ||
const index = block.attributes.anchor.lastIndexOf( '-' ); | ||
const result = block.attributes.anchor.substring( index + 1 ); |
There was a problem hiding this comment.
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
.
Hey @kevin940726 😀 thanks for the reply here. I wasn't aware of that issue with the hooks. So thanks for letting me know :) Is there an alternate approach that would allow for similar functionality that doesn't have that limitation? I'm just wondering whether there are other approaches to making things extendable 🤔 I agree with the goal to have nice tracing in devtools etc. I also think that software should be extendible and would love to find a way to combine the to :) |
I think the 'internal' role for attributes might resolve parts of the issue - #34750. |
I'm not really familiar with this unfortunately. My best guess would be something like |
This is something that could be handled by some other proposals that are discussed in other places. There is a related proposal to have an |
We recently added automatic anchors for headings, and it includes things like appending |
Description
This PR introduces a new filter called
blocks.cloneBlock
which allows you to hook into thecloneBlock
function and modify the cloned block object.The rationale behind this new filter is the need to keep the anchor attribute unique and when blocks are duplicated that uniqueness is lost. So this filter is now being used to make the anchor unique by adding a
-1
(automatically increasing the number if you keep cloning an already cloned block)Closes #35755
How has this been tested?
Locally using the
wp-env
Using the
core/heading
block and duplicating it with no anchor, a simple single word anchor and an anchor that has special characters including-
in it.Screenshots
Types of changes
New feature: Make sure anchors stay unique when blocks are duplicated
New API: Introduce
blocks.cloneBlock
filterChecklist:
*.native.js
files for terms that need renaming or removal).