-
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
Enhance support for SVG in RichText
#40496
base: trunk
Are you sure you want to change the base?
Conversation
Size Change: +43 B (0%) Total Size: 1.62 MB
ℹ️ View Unchanged
|
I am not able to do a code review but I can confirm that the svg's are displaying in the Site Editor. |
I wonder if this can get another look; SVGs inside buttons do not display in the block editor at the moment and this is confusing for editors. Thanks! |
I rebased this |
3781dea
to
2196f5e
Compare
@ellatrix @draganescu what do you think about this? |
packages/rich-text/src/to-dom.js
Outdated
child.setAttribute( key, attributes[ key ] ); | ||
} | ||
if ( 'type' in child ) { | ||
child = contextuallyCreate( element, child ); |
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 would leave the original code there and add one special case for if type === 'svg'
. I thinlk the contextuallyCreate
does not properly serve the situation: almost all the time we're just dealing with regular DOM elements.
For special types such as SVG or MathML we should create one function for each createSVG
, createMathML
etc.
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 tried this in 5c4be5b. I didn't make a createSVG
function because I forgot about that part of your comment until now. Not sure if it's necessary at this stage but we can do that if you like.
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 think allowing SVG inline in rich text should be part of a more thorrough approach, not just allowing it.
The use case of the original issue is more in the real of blocks supporting vectors, not in the realm of rich text allowing inline SVG. Just to illustrate, applying this PR results in this weird UX:
svg-in-rich-text.mp4
Should rich text allow MathML authoring as well? Probably not. It's just like ... it's own thing. What is the best UX for interacting with vectors inside rich text? It's definitely not text :)
2196f5e
to
5c4be5b
Compare
I thought this should be harmless enough as a first step but maybe a bit more is needed. It'd probably be good if everything inside the SVG could be inert in the visual editor. The caret shouldn't enter it. |
5c4be5b
to
376cc58
Compare
This comment was marked as outdated.
This comment was marked as outdated.
start: 0, | ||
end: 0, | ||
formats: [ [ svg ] ], | ||
replacements: [ path ], |
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 like this. I don't understand why we need all this. Why not just reuse the existing logic we have for contenteditable false, and store innerHTML?
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.
Do we even need to think about namespaces if we just set innerHTML?
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.
Thanks for having another look!
Why not just reuse the existing logic we have for contenteditable false, and store innerHTML?
Outdated reason: this approach is three years old #24582 and back then I couldn't find innerHTML used in to-dom.js so I figured it was off-limits for some reason. Now, I will gladly explore this and very much appreciate the hint.
Do we even need to think about namespaces if we just set innerHTML?
I would sure love it if that's the case 🤞.
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.
Do we even need to think about namespaces if we just set innerHTML?
Right, namespaces are taken care of when setting innerHTML
. I've learned that SVG in a format works already as long as it's nested inside. Still, I'd like if we can remove the need for a wrapper element. I've reduced this PR to be what I think is the minimal way to support that—let formats declare a namespace.
The latest commit is largely a revert, to see the cleanest diff just view changes from all commits.
RichText
RichText
376cc58
to
3bb9d57
Compare
What?
Enables support for SVG (or other namespace-requiring MLs like MathML) as rich text Formats and adds a unit test to cover the expected behavior.
Why?
To enable SVG in rich text formats without wrapper elements. Presently, a format can use SVG as long as it’s within an HTML wrapper element.
How?
Adds
namespace
toWPFormat
allowing authors to specify the namespace a format’stagName
should be created with. Also adds it to the return offromFormat
and uses that in creating elements inappend
.Testing Instructions
Snippet to run in the console to add an SVG format.
Screenshots or screencast
bird-format.mp4