ueberdosis / tiptap

The headless rich text editor framework for web artisans.
https://tiptap.dev
MIT License
27.37k stars 2.27k forks source link

[Bug]: this.editor is undefined in `addAttributes` #4421

Closed piszczu4 closed 3 months ago

piszczu4 commented 1 year ago

Which packages did you experience the bug in?

core

What Tiptap version are you using?

2.0.3

What’s the bug you are facing?

editor in addAttributes() method is always undefined. I have an extension and inside addAttributes() I defined the following logic:

    addAttributes() {
        let { editor } = this;
        return {
            revealed: {
                default: false,
                renderHTML: (attributes) => {
                    return {
                        class:
                            "spoiler" +
                            (attributes.revealed && editor?.isEditable ? " is-visible" : ""),
                        "data-spoiler": _t("nodes.spoiler_reveal_text"),
                    };
                },
            },
        };
    },

Unfortunately editor is undefined and hence I can't use isEditable prop. I think that the solution would be to update getAttributesFromExtensions() method and pass editor there.

What browser are you using?

Chrome

Code example

No response

What did you expect to happen?

editor not being undefined

Anything to add? (optional)

No response

Did you update your dependencies?

Are you sponsoring us?

piszczu4 commented 9 months ago

any updates?

edymusajev commented 6 months ago

Still an issue.

nperez0111 commented 3 months ago

addAttributes cannot always have the editor instance, because in order to make the editor instance, it needs the attributes to create the schema to create the editor instance.

In your case, you should not be using the editor.isEditable prop anyway, because changing the schema just because an option was provided is not advised, if you want to do that you can use an option via a configure or addOptions method.

Or you can juse use CSS since all this is doing is a conditional classname.