Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Inserting an inline directive wraps it in a block element #403

Open
2 tasks done
xanthipe opened this issue Apr 1, 2024 · 2 comments
Open
2 tasks done

[BUG] Inserting an inline directive wraps it in a block element #403

xanthipe opened this issue Apr 1, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@xanthipe
Copy link

xanthipe commented Apr 1, 2024

If you want to ask for support or request features, sponsor the project and contact me over email.

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

Describe the bug
For a project I'm working on, I need to be able to provide my end-user with the ability to apply some basic text formatting (particular combinations of colour, decoration etc). I've got most of it working with a custom editor, but even though I'm setting things up as an inline text directive it keeps adding a linebrak tag after the editor.

Reproduction
Start from https://codesandbox.io/p/sandbox/mdx-editor-base-q8s7zr?file=/src/App.tsx to reproduce the problem so that I can observe the issue on my side and make sure that the fix reliably addresses it.

To Reproduce
Steps to reproduce the behavior:

  1. Go to https://codesandbox.io/p/sandbox/mdx-editor-base-forked-8z8fjx
  2. Add some restricted text via the Res button
  3. Attempt to continue typing on the same line - the editor inserts a linebreak despite it being based around a TextDirective.

Expected behavior
As it's a text directive, I'd expect to be able to continue typing inline - I can do this with the generic editor, but then I can't apply the span formatting I need to display.

Screenshots
If applicable, add screenshots to help explain your problem.

image

Desktop (please complete the following information):

  • OS: Windows
  • Browser [e.g. Chrome, Safari]: Safari

Additional context
I am very pleased that it's taken me getting into the more fiddly bits of the code before running into an issue.

I've also noticed (and you can see this in the Sandbox) that there's no direct way to get the types for the Directives that I can find - locally I'm importing as import type { TextDirective } from 'mdast-util-directive' which feels a bit roundabout?

@xanthipe xanthipe added the bug Something isn't working label Apr 1, 2024
@petyosi
Copy link
Contributor

petyosi commented Apr 2, 2024

Your directive editor is actually working correctly. If you start the editor with markdown that includes the directive, it should work as expected. the problem is that insertDirective wraps the inline nodes into a paragraph. See the implementation as a reference.

A workaround I may suggest is to bypass the insertDirective and use the Lexical API directly for the insertion. A proper solution is to implement an explicit insertInlineDecoratorNode -> insertInlineDirective API.

I will leave this open, I hope I can get to it at some point. If you're interested, I'm happy to accept a PR.

Regarding the types - accessing the TextDirective type is actually fine I think, but I might be wrong. Re-exporting the numerous APIs of Lexical and mdast is close to impossible.

@petyosi petyosi changed the title [BUG?] Cannot create a custom TextDirective that works inline [BUG] Inserting an inline directive wraps it in a block element Apr 2, 2024
@xanthipe
Copy link
Author

Ah, that all makes sense - I'll use the existing insertDirective tag for now on the project to get the ball rolling, and hopefully (although as you can probably guess by the two week reply delay I don't have a lot of time to work on this) I can have a look at doing a PR for you to sort it properly.

And yes, I can see how doing a full re-export would be a pain in the backside - possibly just a note in the current example? Although I was also getting some type conversion issues as well, but that's probably a side-effect of the same inline-vs-block problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants