lexical icon indicating copy to clipboard operation
lexical copied to clipboard

Bug: $generateNodesFromDOM doesnt refer to the theme when doing conversion

Open tomer-yechiel opened this issue 4 years ago • 3 comments

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!

tomer-yechiel avatar Nov 10 '22 12:11 tomer-yechiel

Also experiencing this issue. Is there a sandbox the lexical team would recommend using to demonstrate failures with the serialization process?

bigtimecriminal avatar Nov 10 '22 17:11 bigtimecriminal

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

acywatson avatar Nov 14 '22 16:11 acywatson

With the newer DOMRenderExtension this is customizable enough that I think we can call it closed #8368 #8353

etrepum avatar Apr 23 '26 18:04 etrepum