Repository navigation
Navigation Link: Keep the text formatting when editing in the sidebar - #85223
Conversation
The sidebar Text field stripped the label's markup for display and wrote the plain string back on every keystroke, so the first keystroke dropped bold, italic, and any other inline format. Add `applyPlainTextEdit`, which diffs the old and new plain text, replaces only the edited span in the rich text value, and gives typed characters the format of the character before them, as typing on the canvas does. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DaQQeap7U6o932CHso3b4C
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DaQQeap7U6o932CHso3b4C
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the 📦 Bundle sizeSize Change: +432 B (+0.01%) Total Size: 8.56 MB 📦 View Changed
⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information. Backspace in
|
mikachan
left a comment
There was a problem hiding this comment.
Thanks for this, it's a nice fix and reusing @wordpress/rich-text rather than a customised parser feels better. I've left a couple of questions.
The menu's blocks are controlled inner blocks, so the call is scoped to the navigation block rather than the top of the tree. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EtGfAZnXUVsD6CvXeULph5
…nreleased The new TypeScript test needs a dev project, so the build project moves to tsconfig.build.json and tsconfig.json becomes the dev project, registered in the root tsconfig.json. The changelog entry was filed under the published 12.0.0 version. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015tAAXENkcdrTwiyH7K8vCc
mikachan
left a comment
There was a problem hiding this comment.
I found one more potential change to address before bringing this in (sorry!)
| * @return The text with the markup removed. | ||
| */ | ||
| export function getPlainText( html?: string ): string { | ||
| return html ? create( { html } ).text : ''; |
There was a problem hiding this comment.
create().text isn't always something a plain input can show. Two cases:
<br>becomes\n, and<input type="text">strips line breaks from its value. A label ofShop<br><em>now</em>shows asShopnow, butapplyPlainTextEditstill diffs againstShop\nnow. Typing!at the end sendsShopnow!. The prefix stops at index 4 (\nvsn) and the suffix is 0, so the whole\nnowspan is replaced and takes the format ofp.- Inline images: the Navigation Link's
RichTextdoesn't setallowedFormats, socore/imageis allowed. An<img>becomesU+FFFCin.text, and the field then shows a replacement-character box.stripHTMLused to drop the image from the field value entirely.
Suggestion: normalise the value in one place before both the display and the diff. For example, run replace( value, /\n/g, '' ) (or turn the break into a space), and decide what to show for \ufffc. Then the field text and oldText actually are the same string.
There was a problem hiding this comment.
Thanks for catching this! I tried something a little beyond just having a space preserve the non-text content. I'm unsure about this, but I'm using a symbol to represent it. An image gets 🖼️ and a line break is ↵.
I tested it with undo and typing around it, and deleting it, and it all works correctly. One known issue (but I think is acceptable) is that you can't copy/paste the symbol. That's because, if you add a new 🖼️ symbol, we treat it as that symbol and not a representation of an image. Same with ↵. If we had all ↵ symbols become line breaks, then you couldn't type a ↵ symbol in the input.
There was a problem hiding this comment.
I like the idea of using a symbol here! I can't think of a better alternative.
My Claude found a potential issue with this fix: toFieldText iterates value.text with for…of, which steps by code point, but value.formats and the start/end passed to insert are UTF-16 indices. So if the label contains an emoji, the offsets are off by one. For example, typing ! at the end of "😀 Shop" saves "😀 Sho!p", and Backspace saves "😀 Shp". Looping by index should fix it:
for ( let index = 0; index < value.text.length; index++ ) {
const character = value.text[ index ];
offsets.push( text.length );
text += FIELD_SYMBOLS[ character ] ?? character;
}
We could also add a test using an emoji to catch this, although I'm sure this isn't a common case.
There was a problem hiding this comment.
Good catch! updating.
There was a problem hiding this comment.
There was another related issue too. The formats for the inserted text were built with Array.from( inserted.text, … ), which also steps by code point. So typing an emoji after bold text only bolded half of it: Shop😀 came out as Shop��.
Both are fixed now. I added tests for typing and deleting after an emoji, and for typing an emoji into formatted text.
A single-line input drops the newline of a line break and shows an inline image's object replacement character as a box, so the field text and the text the edit was diffed against did not match. Typing at the end of `Shop<br><em>now</em>` replaced the whole `\nnow` span. The field now shows `↵` and `🖼️` for them, and each edit is mapped back onto the value so the break, image and formatting outside the edit survive. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S1u1vgxiwiRMeiSuiiRF5T
…nk-text-field-keep-formats # Conflicts: # packages/block-library/CHANGELOG.md # packages/block-library/tsconfig.build.json # packages/block-library/tsconfig.json # routes/guidelines/tsconfig.json # tsconfig.json
Extend the line-break e2e test with steps for an inline image shown as a symbol, typing beside it, deleting the symbol to remove the image, and undoing that deletion. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S1u1vgxiwiRMeiSuiiRF5T
Iterating the label by code point put edits after an emoji one place off, and gave a typed emoji's formats only half its length. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S1u1vfugcM4cEV6dj8F4yW
mikachan
left a comment
There was a problem hiding this comment.
All the latest updates look good! I think this is ready as soon as CI is happy.
…nk-text-field-keep-formats # Conflicts: # packages/block-library/CHANGELOG.md
What?
Editing a Navigation Link or Navigation Submenu's text in the sidebar Text field no longer drops the formatting of the text. Bold, italic, and other inline formats outside the edited characters survive, and typed characters take the format of the character before them, which differs from the canvas default of dropping the format at a boundary, because a plain input has no way to override active formats.
Why?
The sidebar field showed
stripHTML( label )and wrote the plain string back on every keystroke, so the first keystroke turned<strong>Shop</strong> nowintoShop now. Raised while reviewing #84854, where the Button block gets the same sidebar field; the Button can adopt this helper once both land.How?
A new
applyPlainTextEdit( html, newText )helper inpackages/block-library/src/utils/plain-text-edit.ts:create( { html } )from@wordpress/rich-text.insert, giving the inserted characters the formats of the character before them (or the first character's formats when typing at the start).toHTMLString.getPlainText( html )gives the field its value from the same parse, so the text the user sees and the text the diff runs against are the same string.Known limit: a label with a
<br>loses it on edit, because a single-line input cannot hold a newline. The old code lost it too.Testing Instructions
Unit tests cover the helper in
packages/block-library/src/utils/test/plain-text-edit.jsdom.test.ts, andnavigation.spec.jshas a new test, "keeps the label formatting when editing the sidebar Text field".🤖 Generated with Claude Code
https://claude.ai/code/session_01DaQQeap7U6o932CHso3b4C