Bug: $generateNodesFromDOM doesnt refer to the theme when doing conversion
Hi I am using Lexical in React with Tailwind, and configured Lexical to use Tailwind classes in the theme
theme = {
...
text: {
bold: "text-bold",
italic: "italic",
underline: "underline",
strikethrough: "line-through",
},
}
I want to convert the content of the editor to HTML and vice versa
$generateHtmlFromNodes - work as expected and the Tailwind class appears in the HTML string $generateNodesFromDOM - doesn't work as expected, for example the text node doesn't have the strikethrough format
I found out that the convertSpanElement(LexicalTextNode.ts) doesn't refer to the theme (I am not sure if it's by design or not) also, there is no way to pass custom converters to createEditor (LexicalEditor.ts) which is used under the hood by @lexical/react
Lexical version: latest
for example
<span class="line-through">foo</span>
should be converted to TextNode with format strikethrough
please let me know if you are open to PR that converts TextNode with the theme in mind
thanks!
Also experiencing this issue. Is there a sandbox the lexical team would recommend using to demonstrate failures with the serialization process?
There was a PR to do something like this a while back - the issue we had was that it was applying formatting styles to editors that didn't support formatting (aka plain text editors). If we base it on the theme, I think we can get around that by not applying the formatting if the associated theme classes are undefined. That being said, this would tightly couple the theme and the format, which may not be ideal. We'd basically be saying that the theme controls text formatting in HTML deserialization, which is sort of weird in that the HTML deserialization API should arguably not be dependent on the theme.
What we are ultimately trying to do instead is to make it easier for you to customize node deserialization. What you can do now is register a new TextPasteNode or similar that has an importDOM with a higher priority that will interpret span tags the way you want it to:
https://lexical.dev/docs/concepts/serialization#lexicalnodeimportdom
With the newer DOMRenderExtension this is customizable enough that I think we can call it closed #8368 #8353