facebook / lexical

Lexical is an extensible text editor framework that provides excellent reliability, accessibility and performance.
https://lexical.dev
MIT License
17.37k stars 1.43k forks source link

Slack Dropzone Plugin #5898

Open hazirmagron opened 2 weeks ago

hazirmagron commented 2 weeks ago

The Slack editor allows file drag and drop (see 1st image below). I have implemented something similar (see 2nd image below) using a combination of Dropzone and Attachments plugin.

Slack editor

Screenshot 2024-04-16 at 10 40 39

My custom editor Screenshot 2024-04-16 at 10 43 40

The way my custom plugins work:

  1. User drags a file
  2. My Dropzone Plugin listens to drag events and creates a Dropzone Node where the file can be dropped
  3. The Dropzone Node dispatches an INSERT_ATTACHMENT command
  4. My Attachments plugin creates an attachments node when INSERT_ATTACHMENT is dispatched which shows the file previews (shown in the image). This node is appended as the very last child of the root node

Everything works great but this approach introduces behaviours that are unintended:

  1. When a file is attached the placeholder isn't shown when the editor is empty since root has the Attachments node. The expected behaviour is for the placeholder to be shown but I haven't found a good way of achieving this. See image below for reference.
  2. When user "selects all" (using cmd+a) and deletes the Attachments node is also deleted.

Essentially, what I want to achieve is for the Attachments node to be treated as separate from the root children. One approach is to extract the Dropzone functionality from the editor but ideally it is part of the editor state since the shared state simplifies managing the attached files.

Screenshot 2024-04-16 at 11 04 04 When deleting the only paragraph the placeholder isn't shown as the root has the Attachments node. The user would expect a placeholder here though.

acywatson commented 2 weeks ago

One approach is to extract the Dropzone functionality from the editor but ideally it is part of the editor state since the shared state simplifies managing the attached files.

I would advise you to do this outside of the editor if you don't care about the position of this attachment within a document. The tradeoffs of trying to shoehorn this into the editor state model are just not worth it (imo) if all you need is to attach some images to the document.

hazirmagron commented 1 week ago

One approach is to extract the Dropzone functionality from the editor but ideally it is part of the editor state since the shared state simplifies managing the attached files.

I would advise you to do this outside of the editor if you don't care about the position of this attachment within a document. The tradeoffs of trying to shoehorn this into the editor state model are just not worth it (imo) if all you need is to attach some images to the document.

I ended up following your advise. I feel like it is a compromise to keep the Dropzone functionality non-integrated since I have to manage the state for file uploads separately from everything else. It works well though so I'm somewhat satisfied.

Here is what I ended up doing for anyone who wants to implement similar functionality:

Would appreciate your feedback on this approach @acywatson - for myself and future readers