GithubHelp home page GithubHelp logo

Comments (7)

acywatson avatar acywatson commented on May 5, 2024

You're missing the importDOM function on your custom node. This is what tells lexical how to interpret that node from HTML.

from lexical.

Fillipee avatar Fillipee commented on May 5, 2024

Thank you for the reply. I tried it, but I can't seem to make it work correctly. I don't see any option to add classs to the node, just styles. Here is the code.

static importDOM(): DOMConversionMap | null {
        const importers = TextNode.importDOM();
        return {
            ...importers,
            span: (node: HTMLElement) => {
                if (isMainTextColourNode(node as HTMLElement)) {
                    return {
                        conversion: styleConversion(importers?.span),
                        priority: 1,
                    };
                }
                return null;
            },
        };
    }
function styleConversion(
    originalDOMConverter?: (node: HTMLElement) => DOMConversion | null
): (node: HTMLElement) => DOMConversionOutput | null {
    return (node) => {
        const original = originalDOMConverter?.(node);
        if (!original) {
            return null;
        }

        const originalOutput = original.conversion(node);
        if (!originalOutput) {
            return originalOutput;
        }

        const backgroundColor = "red";
        const color = node.style.color;
        const fontFamily = node.style.fontFamily;
        const fontWeight = node.style.fontWeight;
        const fontSize = node.style.fontSize;
        const textDecoration = node.style.textDecoration;
        const className = node.className;

        return {
            ...originalOutput,
            forChild: (lexicalNode, parent) => {
                const originalForChild = originalOutput?.forChild ?? ((x) => x);
                const result = originalForChild(lexicalNode, parent);
                if ($isTextNode(result)) {
                    const style = [
                        backgroundColor ? `background-color: ${backgroundColor}` : null,
                        color ? `color: ${color}` : null,
                        fontFamily ? `font-family: ${fontFamily}` : null,
                        fontWeight ? `font-weight: ${fontWeight}` : null,
                        fontSize ? `font-size: ${fontSize}` : null,
                        textDecoration ? `text-decoration: ${textDecoration}` : null,
                    ]
                        .filter((value) => value != null)
                        .join("; ");

                    //Need to set className

                    if (style.length) {
                        return result.setStyle(style);
                    }
                }
                return result;
            },
        };
    };
}

function isMainTextColourNode(node: HTMLSpanElement): node is HTMLSpanElement {
    return node.classList.contains("editor-mainTextColour");
}

from lexical.

acywatson avatar acywatson commented on May 5, 2024

How about

node.classList.add('my-class')

?

from lexical.

Fillipee avatar Fillipee commented on May 5, 2024

But I need to add the class to the result, right? I want it to have the class in the editor.
So I want something like

result.classList.add('my-class')
//or
result.setClassName('my-class')

but that does not exist.

from lexical.

acywatson avatar acywatson commented on May 5, 2024

But I need to add the class to the result, right? I want it to have the class in the editor. So I want something like

result.classList.add('my-class')
//or
result.setClassName('my-class')

but that does not exist.

Oh, sorry - yes, of course - I misunderstood.

In that case you can just add it in the node's createDOM method, right? I mean, you have a custom node so if you always want to add that class, you can just do it in createDOM, otherwise if there's some condition you can add a property on the custom node class, set that in importDOM, and read it in createDOM, right?

from lexical.

Fillipee avatar Fillipee commented on May 5, 2024

But I am already doing that in createDOM.

createDOM(_config: EditorConfig, _editor: LexicalEditor): HTMLElement {
        const element = super.createDOM(_config);
        element.className = _config.theme.mainTextColour;
        return element;
    }

from lexical.

Fillipee avatar Fillipee commented on May 5, 2024

So I think I finally figured it out by doing this:

static importDOM(): DOMConversionMap | null {
        const importers = TextNode.importDOM();
        return {
            ...importers,
            span: (node: HTMLElement) => {
                if (isMainTextColourNode(node as HTMLElement)) {
                    return {
                        conversion: (domNode: HTMLElement) => {
                            return {
                                node: $createMainTextColourNode(domNode.textContent || ""),
                            };
                        },
                        priority: 1,
                    };
                }
                return null;
            },
        };
    }

from lexical.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.