editor icon indicating copy to clipboard operation
editor copied to clipboard

[BUG] missing import statements when adding a jsx element inside a nested editor

Open lebalz opened this issue 10 months ago • 6 comments

  • [x] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • [x] I have read the documentation and cannot find an answer.

Describe the bug When a jsx component is added from inside a nested lexical editor, the import statement is ignored. The issue comes from the NestedLexicalEditor which prevents adding import statements when calling exportLexicalTreeToMdast with addImportStatements=false. It makes sense to not import directly inside the nested editor, but the import statement on the primary root seems not to be added.

Reproduction See PR #734 where this issue is mentioned. The user story story=jsx--import-statements-nested can be used to reproduce the issue by focusing the admonition content and then clicking inside the toolbar Zazz. The inserted component does not trigger an import statement. Is this enough or do you need an explicit repro? :)

lebalz avatar Mar 17 '25 12:03 lebalz

I will examine this in more details, but does not it make sense for those import statements to get hoisted up to the root markdown? This might need some change to the code structure, but if I understand you correctly, seems like the right thing to do?

petyosi avatar Mar 17 '25 13:03 petyosi

Yes this absolutely makes sense to hoist them up to the root markdown 👍. The linked PR keeps the current behaviour "as is" but ensures that when someone wrote by hand

A MDX-Document

:::tip[Let's use Components]
import Demo from '@foo'

<Demo />
:::

Then the linked PR would not remove/hoist the import but keep it where it sits... In this case, i think the user really intended to have the import there, so my suggestion is to let it live there in this case...

lebalz avatar Mar 17 '25 13:03 lebalz

Your approach in 734 is quite elaborate, but I'm very afraid of hidden decorator nodes, as they can cause issues with copy/paste or any kind of rich text-editor violent user actions :).

The problem you highlight is real. I do understand that correcting user-supplied markdown is not great, but I'm also hoping that there's a solution that does not cause unintended behavior.

petyosi avatar Mar 17 '25 13:03 petyosi

Merged.

petyosi avatar Mar 20 '25 07:03 petyosi

i don't think this bug is fixed with #734 (734 was not intended to fix it but when i tested #734, i run into this bug and reported it...)

https://github.com/user-attachments/assets/288fbb60-dea4-4a22-9828-d74a567b37d5

--> see that when inserting <Zazz/> inside the admonition, the import is not added, while when added to the root document, the import is added

lebalz avatar Mar 20 '25 08:03 lebalz

Got-it, re-opening.

petyosi avatar Mar 20 '25 08:03 petyosi