Hello,
How can I insert an SVG in the editor ?
Thanks.
Hello,
How can I insert an SVG in the editor ?
Thanks.
There are lots of ways you could insert an SVG, depending on what youâre wanting to do.
If you just want to insert a specific image or one of a set of images, you could define some type of icon
node with an attribute for specifying the icon type to display. It would behave like any other leaf node in your document without any text content. It could be inline or block or whatever you want.
If you want to embed a user-provided image, take a look at the ProseMirror image upload example.
If you want to embed an SVG editor within the text editor, things get a bit more complicated, but itâs still very doable. You would basically define a node which renders to the DOM via ProseMirrorâs NodeView mechanism. That NodeView could be an entire editor with all sorts of actions that result in changes getting dispatched to the ProseMirror view, modifying the ProseMirror state. This would in some ways be simpler than the âembedded code editorâ example.
Hopefully this gets you pointed in the right direction. If not, youâll need to describe what you want in more detail so we can help.
Hello, thank you for your response, I have been experimenting the different solutions and the solution that worked best for my case is to create a SVG node, then to store all the node markup (its children) on the node attribute and then spit out the markup inside (toDOM function) .
Everything worked correctly and I could see the SVG inserted in the browser DOM, but the problem is that it does not render correctly, for example, here I tried a really simple SVG (an ellipse), the DOM has the SVG of the ellipse but not rendering it correctly (blank).
Note: The SVG getâs displayed correctly outside prosemirror
This is my simple svg Node
This is how it gets rendered on the browser
Thanks
My guess is that under the hood ProseMirror is only using Document.createElement
rather than Document.createElementNS
(cf. https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS). Creating SVG elements without the correct namespace will result in DOM that looks plausible but does not actually work as an SVG.
I would argue that ProseMirror is doing the correct thing in not bothering with different namespaces. If you really want to be able to render a document like this, you should look into using a ProseMirror NodeView
so that you get full control over how that node gets rendered to DOM.
However, also consider that using ProseMirror to directly manipulate SVG nodes might be the wrong solution. Itâs technically possible, but it seems like a poor match for ProseMirror as a toolkit for creating web-based rich text editors. If you want to embed an SVG asset, consider a simple leaf node with well-defined attributes which can then render the SVGs via something simpler like an <img>
tag. If you want to embed an SVG editor, again use a simple leaf node which uses a NodeView
to render an editor, handle inputs, and dispatch changes as appropriate.
ProseMirror is robust/flexible enough to enable some crazy things, but if you can take the time to articulate exactly what behavior you are trying to support, you can save yourself a lot of unnecessary technical work.
For future reference, it should be possible to create SVGs by using the namespace followed by a space as shown in ProseMirrorâs renderSpec logic, here:
So, I think you would have to do: return ["http://www.w3.org/2000/svg svg", { ..